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

Commit d1b1e6cb by Bulkan Evcimen

Optional array argument of table names to skip

dropAllTables now accepts optional array argument of tables names
to skip dropping these tables

implementation for sequelize/sequelize#1270
1 parent d3dd52d3
......@@ -120,7 +120,7 @@ var QueryInterface = module.exports = {
}.bind(this))
},
dropAllTables: function() {
dropAllTables: function(tablesToSkip) {
var self = this
return new Utils.CustomEventEmitter(function(dropAllTablesEmitter) {
......@@ -149,7 +149,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 (tablesToSkip.indexOf(tableName) == -1) {
queries.push(self.QueryGenerator.dropTableQuery(tableName).replace(';', ''))
}
})
queries.push('PRAGMA foreign_keys = ON')
......
......@@ -234,12 +234,15 @@ module.exports = (function() {
}).run()
}
QueryInterface.prototype.dropAllTables = function() {
QueryInterface.prototype.dropAllTables = function(tablesToSkip) {
var self = this
if (!tablesToSkip) {
tablesToSkip = [];
}
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, tablesToSkip)
} else {
return new Utils.CustomEventEmitter(function(dropAllTablesEmitter) {
var events = []
......@@ -262,7 +265,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 (tablesToSkip.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(['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!