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

Commit be4ffc9b by Martin Aspeli

Support for dropping tables with constraints

1 parent f293a111
......@@ -496,6 +496,16 @@ module.exports = (function() {
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) {
return Utils.addTicks(s, quoteChar)
},
......
......@@ -78,7 +78,7 @@ module.exports = (function() {
dropTableQuery: function(tableName, options) {
options = options || {}
var query = "DROP TABLE IF EXISTS <%= table %>;"
var query = "DROP TABLE IF EXISTS <%= table %> CASCADE;"
return Utils._.template(query)({
table: QueryGenerator.addQuotes(tableName)
})
......@@ -602,8 +602,16 @@ module.exports = (function() {
return fields
},
enableForeignKeyConstraintsQuery: function() {
return false // not supported by dialect
},
disableForeignKeyConstraintsQuery: function() {
return false // not supported by dialect
},
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)({
user: encodeURIComponent(config.username),
......
......@@ -229,7 +229,22 @@ module.exports = (function() {
*/
findAutoIncrementField: function(factory) {
throwMethodUndefined('findAutoIncrementField')
},
/*
Globally enable foreign key constraints
*/
enableForeignKeyConstraintsQuery: function() {
throwMethodUndefined('enableForeignKeyConstraintsQuery')
},
/*
Globally disable foreign key constraints
*/
disableForeignKeyConstraintsQuery: function() {
throwMethodUndefined('disableForeignKeyConstraintsQuery')
}
}
var throwMethodUndefined = function(methodName) {
......
......@@ -265,6 +265,16 @@ module.exports = (function() {
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) {
for (var key in hash) {
if (hash.hasOwnProperty(key)) {
......
......@@ -91,11 +91,17 @@ module.exports = (function() {
var chainer = new Utils.QueryChainer()
self.showAllTables().success(function(tableNames) {
chainer.add(self, 'disableForeignKeyConstraints', [])
tableNames.forEach(function(tableName) {
chainer.add(self.dropTable(tableName))
chainer.add(self, 'dropTable', [tableName])
})
chainer.add(self, 'enableForeignKeyConstraints', [])
chainer
.run()
.runSerially()
.success(function() {
self.emit('dropAllTables', null)
emitter.emit('success', null)
......@@ -313,6 +319,30 @@ module.exports = (function() {
}).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
var queryAndEmit = function(sqlOrQueryParams, methodName, options, emitter) {
......
......@@ -96,11 +96,11 @@ describe('QueryGenerator', function() {
dropTableQuery: [
{
arguments: ['myTable'],
expectation: "DROP TABLE IF EXISTS \"myTable\";"
expectation: "DROP TABLE IF EXISTS \"myTable\" CASCADE;"
},
{
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!