Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sync): pass options to all query methods #12208

Merged
merged 1 commit into from May 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 14 additions & 19 deletions lib/model.js
Expand Up @@ -1284,23 +1284,25 @@ class Model {
if (options.force) {
await this.drop(options);
}
await this.QueryInterface.createTable(this.getTableName(options), attributes, options, this);

const tableName = this.getTableName(options);

await this.QueryInterface.createTable(tableName, attributes, options, this);

if (options.alter) {
const tableInfos = await Promise.all([
this.QueryInterface.describeTable(this.getTableName(options)),
this.QueryInterface.getForeignKeyReferencesForTable(this.getTableName(options))
this.QueryInterface.describeTable(tableName, options),
this.QueryInterface.getForeignKeyReferencesForTable(tableName, options)
]);
const columns = tableInfos[0];
// Use for alter foreign keys
const foreignKeyReferences = tableInfos[1];

const removedConstraints = {};

for (const columnName in attributes) {
if (!Object.prototype.hasOwnProperty.call(attributes, columnName)) continue;
if (!columns[columnName] && !columns[attributes[columnName].field]) {
await this.QueryInterface.addColumn(this.getTableName(options), attributes[columnName].field || columnName, attributes[columnName]);
await this.QueryInterface.addColumn(tableName, attributes[columnName].field || columnName, attributes[columnName], options);
}
}

Expand All @@ -1309,7 +1311,7 @@ class Model {
if (!Object.prototype.hasOwnProperty.call(columns, columnName)) continue;
const currentAttribute = rawAttributes[columnName];
if (!currentAttribute) {
await this.QueryInterface.removeColumn(this.getTableName(options), columnName, options);
await this.QueryInterface.removeColumn(tableName, columnName, options);
continue;
}
if (currentAttribute.primaryKey) continue;
Expand All @@ -1329,16 +1331,16 @@ class Model {
&& (schema ? foreignKeyReference.referencedTableSchema === schema : true)
&& !removedConstraints[constraintName]) {
// Remove constraint on foreign keys.
await this.QueryInterface.removeConstraint(this.getTableName(options), constraintName, options);
await this.QueryInterface.removeConstraint(tableName, constraintName, options);
removedConstraints[constraintName] = true;
}
}
}
await this.QueryInterface.changeColumn(this.getTableName(options), columnName, currentAttribute);
await this.QueryInterface.changeColumn(tableName, columnName, currentAttribute, options);
}
}
}
let indexes = await this.QueryInterface.showIndex(this.getTableName(options), options);
let indexes = await this.QueryInterface.showIndex(tableName, options);
indexes = this._indexes.filter(item1 =>
!indexes.some(item2 => item1.name === item2.name)
).sort((index1, index2) => {
Expand All @@ -1352,20 +1354,13 @@ class Model {
});

for (const index of indexes) {
await this.QueryInterface.addIndex(
this.getTableName(options),
Object.assign({
logging: options.logging,
benchmark: options.benchmark,
transaction: options.transaction,
schema: options.schema
}, index),
this.tableName
);
await this.QueryInterface.addIndex(tableName, Object.assign({}, options, index));
}

if (options.hooks) {
await this.runHooks('afterSync', options);
}

return this;
}

Expand Down