Update alterTable to actually drop constraints

This commit is contained in:
Matthew Dickinson 2016-09-17 18:29:38 -04:00
parent 1324333ffd
commit 782959cf20
1 changed files with 16 additions and 6 deletions

View File

@ -129,6 +129,7 @@ function mixinMigration(MySQL, mysql) {
var newFkNames = Object.keys(newFks).filter(function(name) {
return !!m.settings.foreignKeys[name];
});
var dropConstraintSql = [];
var sql = [];
var ai = {};
@ -185,7 +186,7 @@ function mixinMigration(MySQL, mysql) {
}
if (needsToDrop) {
sql.push('DROP FOREIGN KEY ' + fk.fkName);
dropConstraintSql.push('DROP FOREIGN KEY ' + fk.fkName);
removedFks.push(fk); //keep track that we removed these
}
});
@ -342,13 +343,22 @@ function mixinMigration(MySQL, mysql) {
});
}
if (sql.length) {
var query = 'ALTER TABLE ' + self.tableEscaped(model) + ' ' +
sql.join(',\n');
if (sql.length || dropConstraintSql.length) {
var stmtList = [dropConstraintSql, sql]
.filter(function(stmts) { return stmts.length; })
.map(function(statements) {
return 'ALTER TABLE ' + self.tableEscaped(model) +
' ' + statements.join(',\n');
});
if (checkOnly) {
done(null, true, {statements: sql, query: query});
done(null, true, {statements: stmtList, query: stmtList.join(';')});
} else {
this.execute(query, done);
async.eachSeries(stmtList, function(stmt, execDone) {
self.execute(stmt, execDone);
}, function(err) {
done(err);
});
}
} else {
done();