Skip to content

Commit aa44649

Browse files
authored
fix(drizzle): ensure getPrimaryDb is used for all write operations (#16240)
PR #16083 added `getPrimaryDb` logic to fix the broken behaviour with the `readReplica` config option but didn't include it to some other operations like with versions or jobs. This PR ensures that all the write operations use it.
1 parent 289e2b6 commit aa44649

12 files changed

Lines changed: 189 additions & 9 deletions

File tree

packages/db-postgres/src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import type {
99
import type { DrizzleConfig } from 'drizzle-orm'
1010
import type { NodePgDatabase } from 'drizzle-orm/node-postgres'
1111
import type { PgSchema, PgTableFn, PgTransactionConfig, PgWithReplicas } from 'drizzle-orm/pg-core'
12+
import type pg from 'pg'
1213
import type { Pool, PoolConfig } from 'pg'
1314

14-
type PgDependency = typeof import('pg').default
15+
type PgDependency = typeof pg
1516

1617
export type Args = {
1718
/**

packages/drizzle/src/createGlobal.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import toSnakeCase from 'to-snake-case'
55
import type { DrizzleAdapter } from './types.js'
66

77
import { upsertRow } from './upsertRow/index.js'
8+
import { getPrimaryDb } from './utilities/getPrimaryDb.js'
89
import { getTransaction } from './utilities/getTransaction.js'
910

1011
export async function createGlobal<T extends Record<string, unknown>>(
@@ -17,7 +18,7 @@ export async function createGlobal<T extends Record<string, unknown>>(
1718

1819
data.createdAt = new Date().toISOString()
1920

20-
const db = await getTransaction(this, req)
21+
const db = getPrimaryDb(this, await getTransaction(this, req))
2122

2223
const result = await upsertRow<{ globalType: string } & T>({
2324
adapter: this,

packages/drizzle/src/createGlobalVersion.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import toSnakeCase from 'to-snake-case'
88
import type { DrizzleAdapter } from './types.js'
99

1010
import { upsertRow } from './upsertRow/index.js'
11+
import { getPrimaryDb } from './utilities/getPrimaryDb.js'
1112
import { getTransaction } from './utilities/getTransaction.js'
1213

1314
export async function createGlobalVersion<T extends JsonObject = JsonObject>(
@@ -29,7 +30,7 @@ export async function createGlobalVersion<T extends JsonObject = JsonObject>(
2930

3031
const tableName = this.tableNameMap.get(`_${toSnakeCase(global.slug)}${this.versionsSuffix}`)
3132

32-
const db = await getTransaction(this, req)
33+
const db = getPrimaryDb(this, await getTransaction(this, req))
3334

3435
const result = await upsertRow<TypeWithVersion<T>>({
3536
adapter: this,

packages/drizzle/src/createVersion.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import toSnakeCase from 'to-snake-case'
88
import type { DrizzleAdapter } from './types.js'
99

1010
import { upsertRow } from './upsertRow/index.js'
11+
import { getPrimaryDb } from './utilities/getPrimaryDb.js'
1112
import { getTransaction } from './utilities/getTransaction.js'
1213

1314
export async function createVersion<T extends JsonObject = JsonObject>(
@@ -52,7 +53,7 @@ export async function createVersion<T extends JsonObject = JsonObject>(
5253
version,
5354
}
5455

55-
const db = await getTransaction(this, req)
56+
const db = getPrimaryDb(this, await getTransaction(this, req))
5657

5758
const result = await upsertRow<TypeWithVersion<T>>({
5859
adapter: this,

packages/drizzle/src/deleteMany.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ export const deleteMany: DeleteMany = async function deleteMany(
5555
)
5656
}
5757

58+
// No getPrimaryDb needed: db is only used for deleteWhere (a write, always routed to primary
59+
// by drizzle's withReplicas). findMany resolves its own db via getTransaction, which returns
60+
// the transaction db (always primary) or falls back to shouldReadFromPrimary.
5861
const db = await getTransaction(this, req)
5962

6063
await this.deleteWhere({

packages/drizzle/src/deleteOne.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { buildFindManyArgs } from './find/buildFindManyArgs.js'
99
import { buildQuery } from './queries/buildQuery.js'
1010
import { selectDistinct } from './queries/selectDistinct.js'
1111
import { transform } from './transform/read/index.js'
12+
import { getPrimaryDb } from './utilities/getPrimaryDb.js'
1213
import { getTransaction } from './utilities/getTransaction.js'
1314
import { markWrite } from './utilities/readAfterWrite.js'
1415

@@ -30,7 +31,7 @@ export const deleteOne: DeleteOne = async function deleteOne(
3031
where: whereArg,
3132
})
3233

33-
const db = await getTransaction(this, req)
34+
const db = getPrimaryDb(this, await getTransaction(this, req))
3435

3536
const selectDistinctResult = await selectDistinct({
3637
adapter: this,

packages/drizzle/src/deleteVersions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ export const deleteVersions: DeleteVersions = async function deleteVersion(
5252
})
5353

5454
if (ids.length > 0) {
55+
// No getPrimaryDb needed: db is only used for deleteWhere (a write, always routed to primary
56+
// by drizzle's withReplicas). findMany resolves its own db via getTransaction, which returns
57+
// the transaction db (always primary) or falls back to shouldReadFromPrimary.
5558
const db = await getTransaction(this, req)
5659

5760
await this.deleteWhere({

packages/drizzle/src/updateGlobal.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import toSnakeCase from 'to-snake-case'
55
import type { DrizzleAdapter } from './types.js'
66

77
import { upsertRow } from './upsertRow/index.js'
8+
import { getPrimaryDb } from './utilities/getPrimaryDb.js'
89
import { getTransaction } from './utilities/getTransaction.js'
910

1011
export async function updateGlobal<T extends Record<string, unknown>>(
@@ -14,7 +15,7 @@ export async function updateGlobal<T extends Record<string, unknown>>(
1415
const globalConfig = this.payload.globals.config.find((config) => config.slug === slug)
1516
const tableName = this.tableNameMap.get(toSnakeCase(globalConfig.slug))
1617

17-
const db = await getTransaction(this, req)
18+
const db = getPrimaryDb(this, await getTransaction(this, req))
1819
const existingGlobal = await db.query[tableName].findFirst({})
1920

2021
const result = await upsertRow<{ globalType: string } & T>({

packages/drizzle/src/updateGlobalVersion.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { DrizzleAdapter } from './types.js'
1212

1313
import { buildQuery } from './queries/buildQuery.js'
1414
import { upsertRow } from './upsertRow/index.js'
15+
import { getPrimaryDb } from './utilities/getPrimaryDb.js'
1516
import { getTransaction } from './utilities/getTransaction.js'
1617

1718
export async function updateGlobalVersion<T extends JsonObject = JsonObject>(
@@ -46,7 +47,7 @@ export async function updateGlobalVersion<T extends JsonObject = JsonObject>(
4647
where: whereToUse,
4748
})
4849

49-
const db = await getTransaction(this, req)
50+
const db = getPrimaryDb(this, await getTransaction(this, req))
5051

5152
const result = await upsertRow<TypeWithVersion<T>>({
5253
id,

packages/drizzle/src/updateJobs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const updateJobs: UpdateJobs = async function updateMany(
6767
return []
6868
}
6969

70-
const db = await getTransaction(this, req)
70+
const db = getPrimaryDb(this, await getTransaction(this, req))
7171

7272
const results = []
7373

0 commit comments

Comments
 (0)