不要怂,就是干,撸起袖子干!

Commit 4123629b by Mick Hansen

Merge pull request #1280 from bulkan/feature/skip_tables_dropAllTables

Optional array argument of table names to skip
2 parents d3dd52d3 fddc9e9c
......@@ -120,9 +120,15 @@ var QueryInterface = module.exports = {
}.bind(this))
},
dropAllTables: function() {
dropAllTables: function(options) {
var self = this
if (!options) {
options = {}
}
var skip = options.skip || [];
return new Utils.CustomEventEmitter(function(dropAllTablesEmitter) {
var events = []
, chainer = new Utils.QueryChainer()
......@@ -149,7 +155,10 @@ var QueryInterface = module.exports = {
queries.push('PRAGMA foreign_keys = OFF')
tableNames.forEach(function(tableName) {
queries.push(self.QueryGenerator.dropTableQuery(tableName).replace(';', ''))
// if tableName is not in the Array of tables names then dont drop it
if (skip.indexOf(tableName) === -1) {
queries.push(self.QueryGenerator.dropTableQuery(tableName).replace(';', ''))
}
})
queries.push('PRAGMA foreign_keys = ON')
......
......@@ -234,12 +234,18 @@ module.exports = (function() {
}).run()
}
QueryInterface.prototype.dropAllTables = function() {
QueryInterface.prototype.dropAllTables = function(options) {
var self = this
if (!options) {
options = {}
}
var skip = options.skip || [];
if (this.sequelize.options.dialect === 'sqlite') {
// sqlite needs some special treatment as it cannot drop a column
return SQLiteQueryInterface.dropAllTables.call(this)
return SQLiteQueryInterface.dropAllTables.call(this, options)
} else {
return new Utils.CustomEventEmitter(function(dropAllTablesEmitter) {
var events = []
......@@ -262,7 +268,10 @@ module.exports = (function() {
// add the table removal query to the chainer
tableNames.forEach(function(tableName) {
chainer.add(self, 'dropTable', [ tableName, { cascade: true } ])
// if tableName is not in the Array of tables names then dont drop it
if (skip.indexOf(tableName) === -1) {
chainer.add(self, 'dropTable', [ tableName, { cascade: true } ])
}
})
chainer
......
......@@ -45,6 +45,24 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
})
})
})
it('should be able to skip given tables', function(done){
var self = this
self.queryInterface.createTable('skipme', {
name: DataTypes.STRING,
}).success(function() {
self.queryInterface.dropAllTables({skip: ['skipme']}).complete(function(err){
expect(err).to.be.null
self.queryInterface.showAllTables().complete(function(err, tableNames) {
expect(err).to.be.null
expect(tableNames).to.contain('skipme');
done();
})
})
})
})
})
describe('indexes', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!