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

Commit 303b453a by Andy Edwards Committed by GitHub

refactor(transaction): asyncify methods (#12156)

1 parent 80de9f60
Showing with 33 additions and 31 deletions
...@@ -64,7 +64,7 @@ class Transaction { ...@@ -64,7 +64,7 @@ class Transaction {
} finally { } finally {
this.finished = 'commit'; this.finished = 'commit';
if (!this.parent) { if (!this.parent) {
await this.cleanup(); this.cleanup();
} }
for (const hook of this._afterCommitHooks) { for (const hook of this._afterCommitHooks) {
await hook.apply(this, [this]); await hook.apply(this, [this]);
...@@ -77,30 +77,30 @@ class Transaction { ...@@ -77,30 +77,30 @@ class Transaction {
* *
* @returns {Promise} * @returns {Promise}
*/ */
rollback() { async rollback() {
if (this.finished) { if (this.finished) {
return Promise.reject(new Error(`Transaction cannot be rolled back because it has been finished with state: ${this.finished}`)); throw new Error(`Transaction cannot be rolled back because it has been finished with state: ${this.finished}`);
} }
if (!this.connection) { if (!this.connection) {
return Promise.reject(new Error('Transaction cannot be rolled back because it never started')); throw new Error('Transaction cannot be rolled back because it never started');
} }
this._clearCls(); this._clearCls();
return this try {
return await this
.sequelize .sequelize
.getQueryInterface() .getQueryInterface()
.rollbackTransaction(this, this.options) .rollbackTransaction(this, this.options);
.finally(() => { } finally {
if (!this.parent) { if (!this.parent) {
return this.cleanup(); this.cleanup();
}
} }
return this;
});
} }
prepareEnvironment(useCLS) { async prepareEnvironment(useCLS) {
let connectionPromise; let connectionPromise;
if (useCLS === undefined) { if (useCLS === undefined) {
...@@ -117,47 +117,49 @@ class Transaction { ...@@ -117,47 +117,49 @@ class Transaction {
connectionPromise = this.sequelize.connectionManager.getConnection(acquireOptions); connectionPromise = this.sequelize.connectionManager.getConnection(acquireOptions);
} }
return connectionPromise let result;
.then(connection => { const connection = await connectionPromise;
this.connection = connection; this.connection = connection;
this.connection.uuid = this.id; this.connection.uuid = this.id;
})
.then(() => { try {
return this.begin() await this.begin();
.then(() => this.setDeferrable()) result = await this.setDeferrable();
.catch(setupErr => this.rollback().finally(() => { } catch (setupErr) {
throw setupErr; try {
})); result = await this.rollback();
}) } finally {
.then(result => { throw setupErr; // eslint-disable-line no-unsafe-finally
}
}
if (useCLS && this.sequelize.constructor._cls) { if (useCLS && this.sequelize.constructor._cls) {
this.sequelize.constructor._cls.set('transaction', this); this.sequelize.constructor._cls.set('transaction', this);
} }
return Promise.resolve(null).then(() => result);
}); return result;
} }
setDeferrable() { async setDeferrable() {
if (this.options.deferrable) { if (this.options.deferrable) {
return this return await this
.sequelize .sequelize
.getQueryInterface() .getQueryInterface()
.deferConstraints(this, this.options); .deferConstraints(this, this.options);
} }
} }
begin() { async begin() {
const queryInterface = this.sequelize.getQueryInterface(); const queryInterface = this.sequelize.getQueryInterface();
if ( this.sequelize.dialect.supports.settingIsolationLevelDuringTransaction ) { if ( this.sequelize.dialect.supports.settingIsolationLevelDuringTransaction ) {
return queryInterface.startTransaction(this, this.options).then(() => { await queryInterface.startTransaction(this, this.options);
return queryInterface.setIsolationLevel(this, this.options.isolationLevel, this.options); return queryInterface.setIsolationLevel(this, this.options.isolationLevel, this.options);
});
} }
return queryInterface.setIsolationLevel(this, this.options.isolationLevel, this.options).then(() => { await queryInterface.setIsolationLevel(this, this.options.isolationLevel, this.options);
return queryInterface.startTransaction(this, this.options); return queryInterface.startTransaction(this, this.options);
});
} }
cleanup() { cleanup() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!