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

Commit be4ffc9b by Martin Aspeli

Support for dropping tables with constraints

1 parent f293a111
...@@ -496,6 +496,16 @@ module.exports = (function() { ...@@ -496,6 +496,16 @@ module.exports = (function() {
return fields return fields
}, },
enableForeignKeyConstraintsQuery: function() {
var sql = "SET FOREIGN_KEY_CHECKS = 1;"
return Utils._.template(sql, {})
},
disableForeignKeyConstraintsQuery: function() {
var sql = "SET FOREIGN_KEY_CHECKS = 0;"
return Utils._.template(sql, {})
},
addQuotes: function(s, quoteChar) { addQuotes: function(s, quoteChar) {
return Utils.addTicks(s, quoteChar) return Utils.addTicks(s, quoteChar)
}, },
......
...@@ -78,7 +78,7 @@ module.exports = (function() { ...@@ -78,7 +78,7 @@ module.exports = (function() {
dropTableQuery: function(tableName, options) { dropTableQuery: function(tableName, options) {
options = options || {} options = options || {}
var query = "DROP TABLE IF EXISTS <%= table %>;" var query = "DROP TABLE IF EXISTS <%= table %> CASCADE;"
return Utils._.template(query)({ return Utils._.template(query)({
table: QueryGenerator.addQuotes(tableName) table: QueryGenerator.addQuotes(tableName)
}) })
...@@ -602,8 +602,16 @@ module.exports = (function() { ...@@ -602,8 +602,16 @@ module.exports = (function() {
return fields return fields
}, },
enableForeignKeyConstraintsQuery: function() {
return false // not supported by dialect
},
disableForeignKeyConstraintsQuery: function() {
return false // not supported by dialect
},
databaseConnectionUri: function(config) { databaseConnectionUri: function(config) {
var template = '<%= protocol %>://<%= user %>:<%= password %>@<%= host %><% if(port) { %>:<%= port %><% } %>/<%= database %>'; var template = '<%= protocol %>://<%= user %>:<%= password %>@<%= host %><% if(port) { %>:<%= port %><% } %>/<%= database %>'
return Utils._.template(template)({ return Utils._.template(template)({
user: encodeURIComponent(config.username), user: encodeURIComponent(config.username),
......
...@@ -229,7 +229,22 @@ module.exports = (function() { ...@@ -229,7 +229,22 @@ module.exports = (function() {
*/ */
findAutoIncrementField: function(factory) { findAutoIncrementField: function(factory) {
throwMethodUndefined('findAutoIncrementField') throwMethodUndefined('findAutoIncrementField')
},
/*
Globally enable foreign key constraints
*/
enableForeignKeyConstraintsQuery: function() {
throwMethodUndefined('enableForeignKeyConstraintsQuery')
},
/*
Globally disable foreign key constraints
*/
disableForeignKeyConstraintsQuery: function() {
throwMethodUndefined('disableForeignKeyConstraintsQuery')
} }
} }
var throwMethodUndefined = function(methodName) { var throwMethodUndefined = function(methodName) {
......
...@@ -265,6 +265,16 @@ module.exports = (function() { ...@@ -265,6 +265,16 @@ module.exports = (function() {
return fields return fields
}, },
enableForeignKeyConstraintsQuery: function() {
var sql = "PRAGMA foreign_keys = ON;"
return Utils._.template(sql, {})
},
disableForeignKeyConstraintsQuery: function() {
var sql = "PRAGMA foreign_keys = OFF;"
return Utils._.template(sql, {})
},
hashToWhereConditions: function(hash) { hashToWhereConditions: function(hash) {
for (var key in hash) { for (var key in hash) {
if (hash.hasOwnProperty(key)) { if (hash.hasOwnProperty(key)) {
......
...@@ -91,11 +91,17 @@ module.exports = (function() { ...@@ -91,11 +91,17 @@ module.exports = (function() {
var chainer = new Utils.QueryChainer() var chainer = new Utils.QueryChainer()
self.showAllTables().success(function(tableNames) { self.showAllTables().success(function(tableNames) {
chainer.add(self, 'disableForeignKeyConstraints', [])
tableNames.forEach(function(tableName) { tableNames.forEach(function(tableName) {
chainer.add(self.dropTable(tableName)) chainer.add(self, 'dropTable', [tableName])
}) })
chainer.add(self, 'enableForeignKeyConstraints', [])
chainer chainer
.run() .runSerially()
.success(function() { .success(function() {
self.emit('dropAllTables', null) self.emit('dropAllTables', null)
emitter.emit('success', null) emitter.emit('success', null)
...@@ -313,6 +319,30 @@ module.exports = (function() { ...@@ -313,6 +319,30 @@ module.exports = (function() {
}).run() }).run()
} }
QueryInterface.prototype.enableForeignKeyConstraints = function() {
var sql = this.QueryGenerator.enableForeignKeyConstraintsQuery()
if(sql) {
return queryAndEmit.call(this, sql, 'enableForeignKeyConstraints')
} else {
return new Utils.CustomEventEmitter(function(emitter) {
this.emit('enableForeignKeyConstraints', null)
emitter.emit('success')
}).run()
}
}
QueryInterface.prototype.disableForeignKeyConstraints = function() {
var sql = this.QueryGenerator.disableForeignKeyConstraintsQuery()
if(sql){
return queryAndEmit.call(this, sql, 'disableForeignKeyConstraints')
} else {
return new Utils.CustomEventEmitter(function(emitter) {
this.emit('disableForeignKeyConstraints', null)
emitter.emit('success')
}).run()
}
}
// private // private
var queryAndEmit = function(sqlOrQueryParams, methodName, options, emitter) { var queryAndEmit = function(sqlOrQueryParams, methodName, options, emitter) {
......
...@@ -96,11 +96,11 @@ describe('QueryGenerator', function() { ...@@ -96,11 +96,11 @@ describe('QueryGenerator', function() {
dropTableQuery: [ dropTableQuery: [
{ {
arguments: ['myTable'], arguments: ['myTable'],
expectation: "DROP TABLE IF EXISTS \"myTable\";" expectation: "DROP TABLE IF EXISTS \"myTable\" CASCADE;"
}, },
{ {
arguments: ['mySchema.myTable'], arguments: ['mySchema.myTable'],
expectation: "DROP TABLE IF EXISTS \"mySchema\".\"myTable\";" expectation: "DROP TABLE IF EXISTS \"mySchema\".\"myTable\" CASCADE;"
} }
], ],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!