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

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 = { ...@@ -120,7 +120,7 @@ var QueryInterface = module.exports = {
}.bind(this)) }.bind(this))
}, },
dropAllTables: function() { dropAllTables: function(tablesToSkip) {
var self = this var self = this
return new Utils.CustomEventEmitter(function(dropAllTablesEmitter) { return new Utils.CustomEventEmitter(function(dropAllTablesEmitter) {
...@@ -149,7 +149,10 @@ var QueryInterface = module.exports = { ...@@ -149,7 +149,10 @@ var QueryInterface = module.exports = {
queries.push('PRAGMA foreign_keys = OFF') queries.push('PRAGMA foreign_keys = OFF')
tableNames.forEach(function(tableName) { tableNames.forEach(function(tableName) {
// 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(self.QueryGenerator.dropTableQuery(tableName).replace(';', ''))
}
}) })
queries.push('PRAGMA foreign_keys = ON') queries.push('PRAGMA foreign_keys = ON')
......
...@@ -234,12 +234,15 @@ module.exports = (function() { ...@@ -234,12 +234,15 @@ module.exports = (function() {
}).run() }).run()
} }
QueryInterface.prototype.dropAllTables = function() { QueryInterface.prototype.dropAllTables = function(tablesToSkip) {
var self = this var self = this
if (!tablesToSkip) {
tablesToSkip = [];
}
if (this.sequelize.options.dialect === 'sqlite') { if (this.sequelize.options.dialect === 'sqlite') {
// sqlite needs some special treatment as it cannot drop a column // sqlite needs some special treatment as it cannot drop a column
return SQLiteQueryInterface.dropAllTables.call(this) return SQLiteQueryInterface.dropAllTables.call(this, tablesToSkip)
} else { } else {
return new Utils.CustomEventEmitter(function(dropAllTablesEmitter) { return new Utils.CustomEventEmitter(function(dropAllTablesEmitter) {
var events = [] var events = []
...@@ -262,7 +265,10 @@ module.exports = (function() { ...@@ -262,7 +265,10 @@ module.exports = (function() {
// add the table removal query to the chainer // add the table removal query to the chainer
tableNames.forEach(function(tableName) { tableNames.forEach(function(tableName) {
// 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.add(self, 'dropTable', [ tableName, { cascade: true } ])
}
}) })
chainer chainer
......
...@@ -45,6 +45,24 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () { ...@@ -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() { describe('indexes', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!