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

Commit b0a1615c by Trey Thomas

Change migration functions to pass through all provided options to query

1 parent 22ad5d93
Showing with 40 additions and 50 deletions
...@@ -20,13 +20,13 @@ var QueryInterface = function(sequelize) { ...@@ -20,13 +20,13 @@ var QueryInterface = function(sequelize) {
QueryInterface.prototype.createSchema = function(schema, options) { QueryInterface.prototype.createSchema = function(schema, options) {
options = options || {}; options = options || {};
var sql = this.QueryGenerator.createSchema(schema); var sql = this.QueryGenerator.createSchema(schema);
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction}); return this.sequelize.query(sql, options);
}; };
QueryInterface.prototype.dropSchema = function(schema, options) { QueryInterface.prototype.dropSchema = function(schema, options) {
options = options || {}; options = options || {};
var sql = this.QueryGenerator.dropSchema(schema); var sql = this.QueryGenerator.dropSchema(schema);
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction}); return this.sequelize.query(sql, options);
}; };
QueryInterface.prototype.dropAllSchemas = function(options) { QueryInterface.prototype.dropAllSchemas = function(options) {
...@@ -34,10 +34,10 @@ QueryInterface.prototype.dropAllSchemas = function(options) { ...@@ -34,10 +34,10 @@ QueryInterface.prototype.dropAllSchemas = function(options) {
var self = this; var self = this;
if (!this.QueryGenerator._dialect.supports.schemas) { if (!this.QueryGenerator._dialect.supports.schemas) {
return this.sequelize.drop({logging: options.logging, transaction: options.transaction}); return this.sequelize.drop(options);
} else { } else {
return this.showAllSchemas(options).map(function(schemaName) { return this.showAllSchemas(options).map(function(schemaName) {
return self.dropSchema(schemaName, { logging: options.logging, transaction: options.transaction }); return self.dropSchema(schemaName, options);
}); });
} }
}; };
...@@ -45,7 +45,7 @@ QueryInterface.prototype.dropAllSchemas = function(options) { ...@@ -45,7 +45,7 @@ QueryInterface.prototype.dropAllSchemas = function(options) {
QueryInterface.prototype.showAllSchemas = function(options) { QueryInterface.prototype.showAllSchemas = function(options) {
var self = this; var self = this;
options = Utils._.extend({ options = _.assign({
raw: true, raw: true,
type: this.sequelize.QueryTypes.SELECT, type: this.sequelize.QueryTypes.SELECT,
logging: false logging: false
...@@ -95,7 +95,8 @@ QueryInterface.prototype.createTable = function(tableName, attributes, options, ...@@ -95,7 +95,8 @@ QueryInterface.prototype.createTable = function(tableName, attributes, options,
if (attributes[keys[i]].type instanceof DataTypes.ENUM) { if (attributes[keys[i]].type instanceof DataTypes.ENUM) {
sql = self.QueryGenerator.pgListEnums(tableName, attributes[keys[i]].field || keys[i], options); sql = self.QueryGenerator.pgListEnums(tableName, attributes[keys[i]].field || keys[i], options);
promises.push(self.sequelize.query( promises.push(self.sequelize.query(
sql, { plain: true, raw: true, type: QueryTypes.SELECT, logging: options.logging, transaction: options.transaction } sql,
_.assign({ plain: true, raw: true, type: QueryTypes.SELECT }, options)
)); ));
} }
} }
...@@ -110,7 +111,8 @@ QueryInterface.prototype.createTable = function(tableName, attributes, options, ...@@ -110,7 +111,8 @@ QueryInterface.prototype.createTable = function(tableName, attributes, options,
if (!results[enumIdx]) { if (!results[enumIdx]) {
sql = self.QueryGenerator.pgEnum(tableName, attributes[keys[i]].field || keys[i], attributes[keys[i]], options); sql = self.QueryGenerator.pgEnum(tableName, attributes[keys[i]].field || keys[i], attributes[keys[i]], options);
promises.push(self.sequelize.query( promises.push(self.sequelize.query(
sql, { raw: true, logging: options.logging, transaction: options.transaction } sql,
_.assign({ raw: true }, options)
)); ));
} else if (!!results[enumIdx] && !!model) { } else if (!!results[enumIdx] && !!model) {
var enumVals = self.QueryGenerator.fromArray(results[enumIdx].enum_value) var enumVals = self.QueryGenerator.fromArray(results[enumIdx].enum_value)
...@@ -199,7 +201,7 @@ QueryInterface.prototype.dropTable = function(tableName, options) { ...@@ -199,7 +201,7 @@ QueryInterface.prototype.dropTable = function(tableName, options) {
if (instanceTable.rawAttributes[keys[i]].type instanceof DataTypes.ENUM) { if (instanceTable.rawAttributes[keys[i]].type instanceof DataTypes.ENUM) {
sql = self.QueryGenerator.pgEnumDrop(getTableName, keys[i]); sql = self.QueryGenerator.pgEnumDrop(getTableName, keys[i]);
options.supportsSearchPath = false; options.supportsSearchPath = false;
promises.push(self.sequelize.query(sql, { logging: options.logging, raw: true, transaction : options.transaction })); promises.push(self.sequelize.query(sql, _.assign({ raw: true }, options)));
} }
} }
} }
...@@ -218,13 +220,13 @@ QueryInterface.prototype.dropAllTables = function(options) { ...@@ -218,13 +220,13 @@ QueryInterface.prototype.dropAllTables = function(options) {
return Promise.each(tableNames, function(tableName) { return Promise.each(tableNames, function(tableName) {
// if tableName is not in the Array of tables names then dont drop it // if tableName is not in the Array of tables names then dont drop it
if (skip.indexOf(tableName.tableName || tableName) === -1) { if (skip.indexOf(tableName.tableName || tableName) === -1) {
return self.dropTable(tableName, { cascade: true, logging: options.logging, transaction: options.transaction }); return self.dropTable(tableName, _.assign({ cascade: true }, options) );
} }
}); });
}; };
var skip = options.skip || []; var skip = options.skip || [];
return self.showAllTables({logging: options.logging, transaction: options.transaction}).then(function(tableNames) { return self.showAllTables(options).then(function(tableNames) {
if (self.sequelize.options.dialect === 'sqlite') { if (self.sequelize.options.dialect === 'sqlite') {
return self.sequelize.query('PRAGMA foreign_keys;', options).then(function(result) { return self.sequelize.query('PRAGMA foreign_keys;', options).then(function(result) {
var foreignKeysAreEnabled = result.foreign_keys === 1; var foreignKeysAreEnabled = result.foreign_keys === 1;
...@@ -240,7 +242,7 @@ QueryInterface.prototype.dropAllTables = function(options) { ...@@ -240,7 +242,7 @@ QueryInterface.prototype.dropAllTables = function(options) {
} }
}); });
} else { } else {
return self.getForeignKeysForTables(tableNames, {logging: options.logging, transaction: options.transaction}).then(function(foreignKeys) { return self.getForeignKeysForTables(tableNames, options).then(function(foreignKeys) {
var promises = []; var promises = [];
tableNames.forEach(function(tableName) { tableNames.forEach(function(tableName) {
...@@ -275,11 +277,11 @@ QueryInterface.prototype.dropAllEnums = function(options) { ...@@ -275,11 +277,11 @@ QueryInterface.prototype.dropAllEnums = function(options) {
return this.sequelize.query( return this.sequelize.query(
sql, sql,
{ plain: false, raw: true, type: QueryTypes.SELECT, logging: options.logging, transaction: options.transaction } _.assign({ plain: false, raw: true, type: QueryTypes.SELECT }, options)
).map(function(result) { ).map(function(result) {
return self.sequelize.query( return self.sequelize.query(
self.QueryGenerator.pgEnumDrop(null, null, self.QueryGenerator.pgEscapeAndQuote(result.enum_name)), self.QueryGenerator.pgEnumDrop(null, null, self.QueryGenerator.pgEscapeAndQuote(result.enum_name)),
{logging: options.logging, transaction: options.transaction, raw: true} _.assign({ raw: true }, options)
); );
}); });
}; };
...@@ -287,12 +289,12 @@ QueryInterface.prototype.dropAllEnums = function(options) { ...@@ -287,12 +289,12 @@ QueryInterface.prototype.dropAllEnums = function(options) {
QueryInterface.prototype.renameTable = function(before, after, options) { QueryInterface.prototype.renameTable = function(before, after, options) {
options = options || {}; options = options || {};
var sql = this.QueryGenerator.renameTableQuery(before, after); var sql = this.QueryGenerator.renameTableQuery(before, after);
return this.sequelize.query(sql, { logging: options.logging, transaction: options.transaction }); return this.sequelize.query(sql, options);
}; };
QueryInterface.prototype.showAllTables = function(options) { QueryInterface.prototype.showAllTables = function(options) {
var self = this; var self = this;
options = Utils._.extend({ options = _.assign({
raw: true, raw: true,
type: QueryTypes.SHOWTABLES type: QueryTypes.SHOWTABLES
}, options || {}); }, options || {});
...@@ -321,11 +323,10 @@ QueryInterface.prototype.describeTable = function(tableName, options) { ...@@ -321,11 +323,10 @@ QueryInterface.prototype.describeTable = function(tableName, options) {
var sql = this.QueryGenerator.describeTableQuery(tableName, schema, schemaDelimiter); var sql = this.QueryGenerator.describeTableQuery(tableName, schema, schemaDelimiter);
return this.sequelize.query(sql, { return this.sequelize.query(
type: QueryTypes.DESCRIBE, sql,
logging: options && options.logging, _.assign({ type: QueryTypes.DESCRIBE }, options || {})
transaction: options && options.transaction ).then(function(data) {
}).then(function(data) {
// If no data is returned from the query, then the table name may be wrong. // 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, // 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). // it will not throw an error like built-ins do (e.g. DESCRIBE on MySql).
...@@ -344,17 +345,17 @@ QueryInterface.prototype.addColumn = function(table, key, attribute, options) { ...@@ -344,17 +345,17 @@ QueryInterface.prototype.addColumn = function(table, key, attribute, options) {
options = options || {}; options = options || {};
attribute = this.sequelize.normalizeAttribute(attribute); attribute = this.sequelize.normalizeAttribute(attribute);
return this.sequelize.query(this.QueryGenerator.addColumnQuery(table, key, attribute), { logging: options.logging, transaction: options.transaction }); return this.sequelize.query(this.QueryGenerator.addColumnQuery(table, key, attribute), options);
}; };
QueryInterface.prototype.removeColumn = function(tableName, attributeName, options) { QueryInterface.prototype.removeColumn = function(tableName, attributeName, options) {
options = options || {}; options = options || {};
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.removeColumn.call(this, tableName, attributeName, {logging: options.logging, transaction: options.transaction}); return SQLiteQueryInterface.removeColumn.call(this, tableName, attributeName, options);
} else { } else {
var sql = this.QueryGenerator.removeColumnQuery(tableName, attributeName); var sql = this.QueryGenerator.removeColumnQuery(tableName, attributeName);
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction}); return this.sequelize.query(sql, options);
} }
}; };
...@@ -372,12 +373,12 @@ QueryInterface.prototype.changeColumn = function(tableName, attributeName, dataT ...@@ -372,12 +373,12 @@ QueryInterface.prototype.changeColumn = function(tableName, attributeName, dataT
if (this.sequelize.options.dialect === 'sqlite') { if (this.sequelize.options.dialect === 'sqlite') {
// sqlite needs some special treatment as it cannot change a column // sqlite needs some special treatment as it cannot change a column
return SQLiteQueryInterface.changeColumn.call(this, tableName, attributes, {logging: options.logging, transaction: options.transaction}); return SQLiteQueryInterface.changeColumn.call(this, tableName, attributes, options);
} else { } else {
var query = this.QueryGenerator.attributesToSQL(attributes) var query = this.QueryGenerator.attributesToSQL(attributes)
, sql = this.QueryGenerator.changeColumnQuery(tableName, query); , sql = this.QueryGenerator.changeColumnQuery(tableName, query);
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction}); return this.sequelize.query(sql, options);
} }
}; };
...@@ -402,14 +403,14 @@ QueryInterface.prototype.renameColumn = function(tableName, attrNameBefore, attr ...@@ -402,14 +403,14 @@ QueryInterface.prototype.renameColumn = function(tableName, attrNameBefore, attr
if (this.sequelize.options.dialect === 'sqlite') { if (this.sequelize.options.dialect === 'sqlite') {
// sqlite needs some special treatment as it cannot rename a column // sqlite needs some special treatment as it cannot rename a column
return SQLiteQueryInterface.renameColumn.call(this, tableName, attrNameBefore, attrNameAfter, {logging: options.logging, transaction: options.transaction}); return SQLiteQueryInterface.renameColumn.call(this, tableName, attrNameBefore, attrNameAfter, options);
} else { } else {
var sql = this.QueryGenerator.renameColumnQuery( var sql = this.QueryGenerator.renameColumnQuery(
tableName, tableName,
attrNameBefore, attrNameBefore,
this.QueryGenerator.attributesToSQL(_options) this.QueryGenerator.attributesToSQL(_options)
); );
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction}); return this.sequelize.query(sql, options);
} }
}.bind(this)); }.bind(this));
}; };
...@@ -431,17 +432,12 @@ QueryInterface.prototype.addIndex = function(tableName, attributes, options, raw ...@@ -431,17 +432,12 @@ QueryInterface.prototype.addIndex = function(tableName, attributes, options, raw
options = options || {}; options = options || {};
options.fields = attributes; options.fields = attributes;
var sql = this.QueryGenerator.addIndexQuery(tableName, options, rawTablename); var sql = this.QueryGenerator.addIndexQuery(tableName, options, rawTablename);
return this.sequelize.query(sql, { logging: options.logging, transaction: options.transaction, supportsSearchPath: false }); return this.sequelize.query(sql, _.assign({ supportsSearchPath: false }, options));
}; };
QueryInterface.prototype.showIndex = function(tableName, options) { QueryInterface.prototype.showIndex = function(tableName, options) {
var sql = this.QueryGenerator.showIndexesQuery(tableName, options); var sql = this.QueryGenerator.showIndexesQuery(tableName, options);
options = options || {}; return this.sequelize.query(sql, _.assign({ type: QueryTypes.SHOWINDEXES }, options || {}));
return this.sequelize.query(sql, {
transaction: options.transaction,
logging: options.logging,
type: QueryTypes.SHOWINDEXES
});
}; };
QueryInterface.prototype.nameIndexes = function(indexes, rawTablename) { QueryInterface.prototype.nameIndexes = function(indexes, rawTablename) {
...@@ -457,7 +453,7 @@ QueryInterface.prototype.getForeignKeysForTables = function(tableNames, options) ...@@ -457,7 +453,7 @@ QueryInterface.prototype.getForeignKeysForTables = function(tableNames, options)
} }
return Promise.map(tableNames, function(tableName) { return Promise.map(tableNames, function(tableName) {
return self.sequelize.query(self.QueryGenerator.getForeignKeysQuery(tableName, self.sequelize.config.database), {logging: options.logging, transaction: options.transaction}).get(0); return self.sequelize.query(self.QueryGenerator.getForeignKeysQuery(tableName, self.sequelize.config.database), options).get(0);
}).then(function(results) { }).then(function(results) {
var result = {}; var result = {};
...@@ -478,7 +474,7 @@ QueryInterface.prototype.getForeignKeysForTables = function(tableNames, options) ...@@ -478,7 +474,7 @@ QueryInterface.prototype.getForeignKeysForTables = function(tableNames, options)
QueryInterface.prototype.removeIndex = function(tableName, indexNameOrAttributes, options) { QueryInterface.prototype.removeIndex = function(tableName, indexNameOrAttributes, options) {
options = options || {}; options = options || {};
var sql = this.QueryGenerator.removeIndexQuery(tableName, indexNameOrAttributes); var sql = this.QueryGenerator.removeIndexQuery(tableName, indexNameOrAttributes);
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction}); return this.sequelize.query(sql, options);
}; };
QueryInterface.prototype.insert = function(instance, tableName, values, options) { QueryInterface.prototype.insert = function(instance, tableName, values, options) {
...@@ -628,10 +624,7 @@ QueryInterface.prototype.delete = function(instance, tableName, identifier, opti ...@@ -628,10 +624,7 @@ QueryInterface.prototype.delete = function(instance, tableName, identifier, opti
} }
return Promise.each(cascades, function (cascade) { return Promise.each(cascades, function (cascade) {
return instance[cascade]({ return instance[cascade](options).then(function (instances) {
transaction: options.transaction,
logging: options.logging
}).then(function (instances) {
// Check for hasOne relationship with non-existing associate ("has zero") // Check for hasOne relationship with non-existing associate ("has zero")
if (!instances) { if (!instances) {
return Promise.resolve(); return Promise.resolve();
...@@ -640,10 +633,7 @@ QueryInterface.prototype.delete = function(instance, tableName, identifier, opti ...@@ -640,10 +633,7 @@ QueryInterface.prototype.delete = function(instance, tableName, identifier, opti
if (!Array.isArray(instances)) instances = [instances]; if (!Array.isArray(instances)) instances = [instances];
return Promise.each(instances, function (instance) { return Promise.each(instances, function (instance) {
return instance.destroy({ return instance.destroy(options);
transaction: options.transaction,
logging: options.logging
});
}); });
}); });
}).then(function () { }).then(function () {
...@@ -729,7 +719,7 @@ QueryInterface.prototype.createTrigger = function(tableName, triggerName, timing ...@@ -729,7 +719,7 @@ QueryInterface.prototype.createTrigger = function(tableName, triggerName, timing
var sql = this.QueryGenerator.createTrigger(tableName, triggerName, timingType, fireOnArray, functionName, functionParams, optionsArray); var sql = this.QueryGenerator.createTrigger(tableName, triggerName, timingType, fireOnArray, functionName, functionParams, optionsArray);
options = options || {}; options = options || {};
if (sql) { if (sql) {
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction}); return this.sequelize.query(sql, options);
} else { } else {
return Promise.resolve(); return Promise.resolve();
} }
...@@ -740,7 +730,7 @@ QueryInterface.prototype.dropTrigger = function(tableName, triggerName, options) ...@@ -740,7 +730,7 @@ QueryInterface.prototype.dropTrigger = function(tableName, triggerName, options)
options = options || {}; options = options || {};
if (sql) { if (sql) {
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction}); return this.sequelize.query(sql, options);
} else { } else {
return Promise.resolve(); return Promise.resolve();
} }
...@@ -751,7 +741,7 @@ QueryInterface.prototype.renameTrigger = function(tableName, oldTriggerName, new ...@@ -751,7 +741,7 @@ QueryInterface.prototype.renameTrigger = function(tableName, oldTriggerName, new
options = options || {}; options = options || {};
if (sql) { if (sql) {
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction}); return this.sequelize.query(sql, options);
} else { } else {
return Promise.resolve(); return Promise.resolve();
} }
...@@ -762,7 +752,7 @@ QueryInterface.prototype.createFunction = function(functionName, params, returnT ...@@ -762,7 +752,7 @@ QueryInterface.prototype.createFunction = function(functionName, params, returnT
options = options || {}; options = options || {};
if (sql) { if (sql) {
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction}); return this.sequelize.query(sql, options);
} else { } else {
return Promise.resolve(); return Promise.resolve();
} }
...@@ -773,7 +763,7 @@ QueryInterface.prototype.dropFunction = function(functionName, params, options) ...@@ -773,7 +763,7 @@ QueryInterface.prototype.dropFunction = function(functionName, params, options)
options = options || {}; options = options || {};
if (sql) { if (sql) {
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction}); return this.sequelize.query(sql, options);
} else { } else {
return Promise.resolve(); return Promise.resolve();
} }
...@@ -784,7 +774,7 @@ QueryInterface.prototype.renameFunction = function(oldFunctionName, params, newF ...@@ -784,7 +774,7 @@ QueryInterface.prototype.renameFunction = function(oldFunctionName, params, newF
options = options || {}; options = options || {};
if (sql) { if (sql) {
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction}); return this.sequelize.query(sql, options);
} else { } else {
return Promise.resolve(); return Promise.resolve();
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!