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

Commit 22ad5d93 by Trey Thomas

Add transaction support to all migration queries

1 parent 8d752082
Showing with 30 additions and 26 deletions
......@@ -20,13 +20,13 @@ var QueryInterface = function(sequelize) {
QueryInterface.prototype.createSchema = function(schema, options) {
options = options || {};
var sql = this.QueryGenerator.createSchema(schema);
return this.sequelize.query(sql, {logging: options.logging});
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
};
QueryInterface.prototype.dropSchema = function(schema, options) {
options = options || {};
var sql = this.QueryGenerator.dropSchema(schema);
return this.sequelize.query(sql, {logging: options.logging});
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
};
QueryInterface.prototype.dropAllSchemas = function(options) {
......@@ -34,10 +34,10 @@ QueryInterface.prototype.dropAllSchemas = function(options) {
var self = this;
if (!this.QueryGenerator._dialect.supports.schemas) {
return this.sequelize.drop({ logging: options.logging });
return this.sequelize.drop({logging: options.logging, transaction: options.transaction});
} else {
return this.showAllSchemas(options).map(function(schemaName) {
return self.dropSchema(schemaName, { logging: options.logging });
return self.dropSchema(schemaName, { logging: options.logging, transaction: options.transaction });
});
}
};
......@@ -218,13 +218,13 @@ QueryInterface.prototype.dropAllTables = function(options) {
return Promise.each(tableNames, function(tableName) {
// if tableName is not in the Array of tables names then dont drop it
if (skip.indexOf(tableName.tableName || tableName) === -1) {
return self.dropTable(tableName, { cascade: true, logging: options.logging });
return self.dropTable(tableName, { cascade: true, logging: options.logging, transaction: options.transaction });
}
});
};
var skip = options.skip || [];
return self.showAllTables({logging: options.logging}).then(function(tableNames) {
return self.showAllTables({logging: options.logging, transaction: options.transaction}).then(function(tableNames) {
if (self.sequelize.options.dialect === 'sqlite') {
return self.sequelize.query('PRAGMA foreign_keys;', options).then(function(result) {
var foreignKeysAreEnabled = result.foreign_keys === 1;
......@@ -240,7 +240,7 @@ QueryInterface.prototype.dropAllTables = function(options) {
}
});
} else {
return self.getForeignKeysForTables(tableNames, {logging: options.logging}).then(function(foreignKeys) {
return self.getForeignKeysForTables(tableNames, {logging: options.logging, transaction: options.transaction}).then(function(foreignKeys) {
var promises = [];
tableNames.forEach(function(tableName) {
......@@ -279,7 +279,7 @@ QueryInterface.prototype.dropAllEnums = function(options) {
).map(function(result) {
return self.sequelize.query(
self.QueryGenerator.pgEnumDrop(null, null, self.QueryGenerator.pgEscapeAndQuote(result.enum_name)),
{logging: options.logging, raw: true}
{logging: options.logging, transaction: options.transaction, raw: true}
);
});
};
......@@ -287,7 +287,7 @@ QueryInterface.prototype.dropAllEnums = function(options) {
QueryInterface.prototype.renameTable = function(before, after, options) {
options = options || {};
var sql = this.QueryGenerator.renameTableQuery(before, after);
return this.sequelize.query(sql, { logging: options.logging });
return this.sequelize.query(sql, { logging: options.logging, transaction: options.transaction });
};
QueryInterface.prototype.showAllTables = function(options) {
......@@ -321,7 +321,11 @@ QueryInterface.prototype.describeTable = function(tableName, options) {
var sql = this.QueryGenerator.describeTableQuery(tableName, schema, schemaDelimiter);
return this.sequelize.query(sql, { type: QueryTypes.DESCRIBE, logging: options && options.logging }).then(function(data) {
return this.sequelize.query(sql, {
type: QueryTypes.DESCRIBE,
logging: options && options.logging,
transaction: options && options.transaction
}).then(function(data) {
// If no data is returned from the query, then the table name may be wrong.
// Query generators that use information_schema for retrieving table info will just return an empty result set,
// it will not throw an error like built-ins do (e.g. DESCRIBE on MySql).
......@@ -340,17 +344,17 @@ QueryInterface.prototype.addColumn = function(table, key, attribute, options) {
options = options || {};
attribute = this.sequelize.normalizeAttribute(attribute);
return this.sequelize.query(this.QueryGenerator.addColumnQuery(table, key, attribute), { logging: options.logging });
return this.sequelize.query(this.QueryGenerator.addColumnQuery(table, key, attribute), { logging: options.logging, transaction: options.transaction });
};
QueryInterface.prototype.removeColumn = function(tableName, attributeName, options) {
options = options || {};
if (this.sequelize.options.dialect === 'sqlite') {
// sqlite needs some special treatment as it cannot drop a column
return SQLiteQueryInterface.removeColumn.call(this, tableName, attributeName, {logging: options.logging});
return SQLiteQueryInterface.removeColumn.call(this, tableName, attributeName, {logging: options.logging, transaction: options.transaction});
} else {
var sql = this.QueryGenerator.removeColumnQuery(tableName, attributeName);
return this.sequelize.query(sql, {logging: options.logging});
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
}
};
......@@ -368,12 +372,12 @@ QueryInterface.prototype.changeColumn = function(tableName, attributeName, dataT
if (this.sequelize.options.dialect === 'sqlite') {
// sqlite needs some special treatment as it cannot change a column
return SQLiteQueryInterface.changeColumn.call(this, tableName, attributes, {logging: options.logging});
return SQLiteQueryInterface.changeColumn.call(this, tableName, attributes, {logging: options.logging, transaction: options.transaction});
} else {
var query = this.QueryGenerator.attributesToSQL(attributes)
, sql = this.QueryGenerator.changeColumnQuery(tableName, query);
return this.sequelize.query(sql, {logging: options.logging});
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
}
};
......@@ -398,14 +402,14 @@ QueryInterface.prototype.renameColumn = function(tableName, attrNameBefore, attr
if (this.sequelize.options.dialect === 'sqlite') {
// sqlite needs some special treatment as it cannot rename a column
return SQLiteQueryInterface.renameColumn.call(this, tableName, attrNameBefore, attrNameAfter, {logging: options.logging});
return SQLiteQueryInterface.renameColumn.call(this, tableName, attrNameBefore, attrNameAfter, {logging: options.logging, transaction: options.transaction});
} else {
var sql = this.QueryGenerator.renameColumnQuery(
tableName,
attrNameBefore,
this.QueryGenerator.attributesToSQL(_options)
);
return this.sequelize.query(sql, {logging: options.logging});
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
}
}.bind(this));
};
......@@ -427,7 +431,7 @@ QueryInterface.prototype.addIndex = function(tableName, attributes, options, raw
options = options || {};
options.fields = attributes;
var sql = this.QueryGenerator.addIndexQuery(tableName, options, rawTablename);
return this.sequelize.query(sql, { logging: options.logging, supportsSearchPath: false });
return this.sequelize.query(sql, { logging: options.logging, transaction: options.transaction, supportsSearchPath: false });
};
QueryInterface.prototype.showIndex = function(tableName, options) {
......@@ -453,7 +457,7 @@ QueryInterface.prototype.getForeignKeysForTables = function(tableNames, options)
}
return Promise.map(tableNames, function(tableName) {
return self.sequelize.query(self.QueryGenerator.getForeignKeysQuery(tableName, self.sequelize.config.database), {logging: options.logging}).get(0);
return self.sequelize.query(self.QueryGenerator.getForeignKeysQuery(tableName, self.sequelize.config.database), {logging: options.logging, transaction: options.transaction}).get(0);
}).then(function(results) {
var result = {};
......@@ -474,7 +478,7 @@ QueryInterface.prototype.getForeignKeysForTables = function(tableNames, options)
QueryInterface.prototype.removeIndex = function(tableName, indexNameOrAttributes, options) {
options = options || {};
var sql = this.QueryGenerator.removeIndexQuery(tableName, indexNameOrAttributes);
return this.sequelize.query(sql, {logging: options.logging});
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
};
QueryInterface.prototype.insert = function(instance, tableName, values, options) {
......@@ -725,7 +729,7 @@ QueryInterface.prototype.createTrigger = function(tableName, triggerName, timing
var sql = this.QueryGenerator.createTrigger(tableName, triggerName, timingType, fireOnArray, functionName, functionParams, optionsArray);
options = options || {};
if (sql) {
return this.sequelize.query(sql, {logging: options.logging});
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
} else {
return Promise.resolve();
}
......@@ -736,7 +740,7 @@ QueryInterface.prototype.dropTrigger = function(tableName, triggerName, options)
options = options || {};
if (sql) {
return this.sequelize.query(sql, {logging: options.logging});
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
} else {
return Promise.resolve();
}
......@@ -747,7 +751,7 @@ QueryInterface.prototype.renameTrigger = function(tableName, oldTriggerName, new
options = options || {};
if (sql) {
return this.sequelize.query(sql, {logging: options.logging});
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
} else {
return Promise.resolve();
}
......@@ -758,7 +762,7 @@ QueryInterface.prototype.createFunction = function(functionName, params, returnT
options = options || {};
if (sql) {
return this.sequelize.query(sql, {logging: options.logging});
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
} else {
return Promise.resolve();
}
......@@ -769,7 +773,7 @@ QueryInterface.prototype.dropFunction = function(functionName, params, options)
options = options || {};
if (sql) {
return this.sequelize.query(sql, {logging: options.logging});
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
} else {
return Promise.resolve();
}
......@@ -780,7 +784,7 @@ QueryInterface.prototype.renameFunction = function(oldFunctionName, params, newF
options = options || {};
if (sql) {
return this.sequelize.query(sql, {logging: options.logging});
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
} else {
return Promise.resolve();
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!