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

[Proof of concept] refactor: try to run adapter on raw strings #5611

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ async function prepare () {
})
env.DATABASE_ROUTING_RULES = JSON.stringify([
{ target: 'main', gqlOperationType: 'mutation' },
{ target: 'main', tableName: 'knex_.+' },
{ target: 'replicas', sqlOperationName: 'select' },
{ target: 'main' },
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,30 @@ class BalancingReplicaKnexAdapter extends KnexAdapter {

this.knex.client.runner = (builder) => {
try {
const sqlObject = builder.toSQL()
// const sqlObject = builder.toSQL()

// NOTE: Right now partial routing is not implemented.
// In real life there's no array cases at all, except few occurrences in migrations with length === 1
// So for safe behaviour we'll redirect any batched queries to default writable pool
if (Array.isArray(sqlObject)) {
return this._defaultPool.getQueryRunner(builder)
}
// if (Array.isArray(sqlObject)) {
// return this._defaultPool.getQueryRunner(builder)
// }

// NOTE: builder.toSQL() in single-query case will return object of shape:
// { method: "<knex-method>", sql: "select * from ... limit ?", bindings: [100] }
// parser cannot understand bindings, so we need to do some tricks, which Client_PG does under the hood
const sqlQueryWithPositionalBindings = this.knex.client.positionBindings(sqlObject.sql)
// const sqlQueryWithPositionalBindings = this.knex.client.positionBindings(sqlObject.sql)

const sqlQueryWithArgs = String(builder)

const selectedPool = this._selectTargetPool(sqlQueryWithPositionalBindings)
const selectedPool = this._selectTargetPool(sqlQueryWithArgs)

return selectedPool.getQueryRunner(builder)
} catch (err) {
const msg = err.message
if (msg && msg.includes('Unsupported operation provided. Expected to be one of the following: [insert, select, update, delete, show], but got:')) {
return this._defaultPool.getQueryRunner(builder)
}
logger.error({ msg: 'Unexpected error happened during SQL query routing', err })
throw new Error(`Unexpected error happened during SQL query routing: ${String(err)}`)
}
Expand Down
Loading