Repository navigation
read-only transactions for use on read-replicas - #7324
Conversation
sushantdhiman
left a comment
There was a problem hiding this comment.
A few comments about tests, otherwise it will be a good feature to have
| return this.User.findAll(); | ||
| }); | ||
|
|
||
| function setupTransactionTestOptions() { |
There was a problem hiding this comment.
Rather than creating connection with master / salve once again, you can use already configured connection this.sequelize and this.User and test with them
| var connectStub = sandbox.stub(sequelize.connectionManager, '_connect').returns(resolvedPromise); | ||
|
|
||
| return sequelize.transaction({readOnly: true}, (transaction) => {}) | ||
| .catch(() => { // Database doesn't exist, so query *will* fail |
There was a problem hiding this comment.
Use this.sequelize backed connection and assert with readOnly: true read replica gets utilized
| * @param {String} options.type=true Sets the type of the transaction. | ||
| * @param {String} options.isolationLevel=true Sets the isolation level of the transaction. | ||
| * @param {String} options.deferrable Sets the constraints to be deferred or immediately checked. | ||
| * @param {String} options.readOnly=true Sets the read-only property of the transaction, allowing |
There was a problem hiding this comment.
options.readOnly=false Sets read-only property of transaction, such transaction will use read replicas when available
Codecov Report
Continue to review full report at Codecov.
|
|
@sushantdhiman Thanks for the comments. The PR is amended as suggested |
|
|
||
| it('should run read-only transactions on the replica', () => { | ||
| return this.sequelize.transaction({readOnly: true}, (transaction) => {}) | ||
| .then(expectReadCalls); |
There was a problem hiding this comment.
Read something here, just to be sure User.findAll call will do
| let connectionPromise; | ||
|
|
||
| return Utils.Promise.resolve(this.parent ? this.parent.connection : this.sequelize.connectionManager.getConnection({ uuid: this.id })) | ||
| if (this.parent) { |
There was a problem hiding this comment.
I feel this can be improved a whole lot
const acquireOptions = { uuid: this.id };
if (this.options.readOnly) {
acquireOptions.type = 'SELECT';
}
return Utils.Promise.resolve(
this.parent.connection
? this.parent.connection
: this.sequelize.connectionManager.getConnection(acquireOptions)
)| const DataTypes = require(__dirname + '/../../lib/data-types'); | ||
| const dialect = Support.getTestDialect(); | ||
| const sinon = require('sinon'); | ||
| const _ = require('lodash'); |
There was a problem hiding this comment.
No lodash needed in this code
|
Hi, changed as requested |
Closes #7323
Pull Request check-list
Please make sure to review and check all of these items:
npm run testornpm run test-DIALECTpass with this change (including linting)?Futurein the changelog?NOTE: these things are not required to open a PR and can be done afterwards / while the PR is open.
Description of change
transaction()accepts one more option, calledreadOnly. If true, the transaction is run on the read-replica.