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

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) {
QueryInterface.prototype.createSchema = function(schema, options) {
options = options || {};
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) {
options = options || {};
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) {
......@@ -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, transaction: options.transaction});
return this.sequelize.drop(options);
} else {
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) {
QueryInterface.prototype.showAllSchemas = function(options) {
var self = this;
options = Utils._.extend({
options = _.assign({
raw: true,
type: this.sequelize.QueryTypes.SELECT,
logging: false
......@@ -95,7 +95,8 @@ QueryInterface.prototype.createTable = function(tableName, attributes, options,
if (attributes[keys[i]].type instanceof DataTypes.ENUM) {
sql = self.QueryGenerator.pgListEnums(tableName, attributes[keys[i]].field || keys[i], options);
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,
if (!results[enumIdx]) {
sql = self.QueryGenerator.pgEnum(tableName, attributes[keys[i]].field || keys[i], attributes[keys[i]], options);
promises.push(self.sequelize.query(
sql, { raw: true, logging: options.logging, transaction: options.transaction }
sql,
_.assign({ raw: true }, options)
));
} else if (!!results[enumIdx] && !!model) {
var enumVals = self.QueryGenerator.fromArray(results[enumIdx].enum_value)
......@@ -199,7 +201,7 @@ QueryInterface.prototype.dropTable = function(tableName, options) {
if (instanceTable.rawAttributes[keys[i]].type instanceof DataTypes.ENUM) {
sql = self.QueryGenerator.pgEnumDrop(getTableName, keys[i]);
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) {
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, transaction: options.transaction });
return self.dropTable(tableName, _.assign({ cascade: true }, options) );
}
});
};
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') {
return self.sequelize.query('PRAGMA foreign_keys;', options).then(function(result) {
var foreignKeysAreEnabled = result.foreign_keys === 1;
......@@ -240,7 +242,7 @@ QueryInterface.prototype.dropAllTables = function(options) {
}
});
} else {
return self.getForeignKeysForTables(tableNames, {logging: options.logging, transaction: options.transaction}).then(function(foreignKeys) {
return self.getForeignKeysForTables(tableNames, options).then(function(foreignKeys) {
var promises = [];
tableNames.forEach(function(tableName) {
......@@ -275,11 +277,11 @@ QueryInterface.prototype.dropAllEnums = function(options) {
return this.sequelize.query(
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) {
return self.sequelize.query(
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) {
QueryInterface.prototype.renameTable = function(before, after, options) {
options = options || {};
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) {
var self = this;
options = Utils._.extend({
options = _.assign({
raw: true,
type: QueryTypes.SHOWTABLES
}, options || {});
......@@ -321,11 +323,10 @@ 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,
transaction: options && options.transaction
}).then(function(data) {
return this.sequelize.query(
sql,
_.assign({ type: QueryTypes.DESCRIBE }, options || {})
).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).
......@@ -344,17 +345,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, transaction: options.transaction });
return this.sequelize.query(this.QueryGenerator.addColumnQuery(table, key, attribute), options);
};
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, transaction: options.transaction});
return SQLiteQueryInterface.removeColumn.call(this, tableName, attributeName, options);
} else {
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
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, transaction: options.transaction});
return SQLiteQueryInterface.changeColumn.call(this, tableName, attributes, options);
} else {
var query = this.QueryGenerator.attributesToSQL(attributes)
, 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
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, transaction: options.transaction});
return SQLiteQueryInterface.renameColumn.call(this, tableName, attrNameBefore, attrNameAfter, options);
} else {
var sql = this.QueryGenerator.renameColumnQuery(
tableName,
attrNameBefore,
this.QueryGenerator.attributesToSQL(_options)
);
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
return this.sequelize.query(sql, options);
}
}.bind(this));
};
......@@ -431,17 +432,12 @@ 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, transaction: options.transaction, supportsSearchPath: false });
return this.sequelize.query(sql, _.assign({ supportsSearchPath: false }, options));
};
QueryInterface.prototype.showIndex = function(tableName, options) {
var sql = this.QueryGenerator.showIndexesQuery(tableName, options);
options = options || {};
return this.sequelize.query(sql, {
transaction: options.transaction,
logging: options.logging,
type: QueryTypes.SHOWINDEXES
});
return this.sequelize.query(sql, _.assign({ type: QueryTypes.SHOWINDEXES }, options || {}));
};
QueryInterface.prototype.nameIndexes = function(indexes, rawTablename) {
......@@ -457,7 +453,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, transaction: options.transaction}).get(0);
return self.sequelize.query(self.QueryGenerator.getForeignKeysQuery(tableName, self.sequelize.config.database), options).get(0);
}).then(function(results) {
var result = {};
......@@ -478,7 +474,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, transaction: options.transaction});
return this.sequelize.query(sql, options);
};
QueryInterface.prototype.insert = function(instance, tableName, values, options) {
......@@ -628,10 +624,7 @@ QueryInterface.prototype.delete = function(instance, tableName, identifier, opti
}
return Promise.each(cascades, function (cascade) {
return instance[cascade]({
transaction: options.transaction,
logging: options.logging
}).then(function (instances) {
return instance[cascade](options).then(function (instances) {
// Check for hasOne relationship with non-existing associate ("has zero")
if (!instances) {
return Promise.resolve();
......@@ -640,10 +633,7 @@ QueryInterface.prototype.delete = function(instance, tableName, identifier, opti
if (!Array.isArray(instances)) instances = [instances];
return Promise.each(instances, function (instance) {
return instance.destroy({
transaction: options.transaction,
logging: options.logging
});
return instance.destroy(options);
});
});
}).then(function () {
......@@ -729,7 +719,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, transaction: options.transaction});
return this.sequelize.query(sql, options);
} else {
return Promise.resolve();
}
......@@ -740,7 +730,7 @@ QueryInterface.prototype.dropTrigger = function(tableName, triggerName, options)
options = options || {};
if (sql) {
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
return this.sequelize.query(sql, options);
} else {
return Promise.resolve();
}
......@@ -751,7 +741,7 @@ QueryInterface.prototype.renameTrigger = function(tableName, oldTriggerName, new
options = options || {};
if (sql) {
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
return this.sequelize.query(sql, options);
} else {
return Promise.resolve();
}
......@@ -762,7 +752,7 @@ QueryInterface.prototype.createFunction = function(functionName, params, returnT
options = options || {};
if (sql) {
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
return this.sequelize.query(sql, options);
} else {
return Promise.resolve();
}
......@@ -773,7 +763,7 @@ QueryInterface.prototype.dropFunction = function(functionName, params, options)
options = options || {};
if (sql) {
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
return this.sequelize.query(sql, options);
} else {
return Promise.resolve();
}
......@@ -784,7 +774,7 @@ QueryInterface.prototype.renameFunction = function(oldFunctionName, params, newF
options = options || {};
if (sql) {
return this.sequelize.query(sql, {logging: options.logging, transaction: options.transaction});
return this.sequelize.query(sql, options);
} 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!