Skip to content

Commit 98666eb

Browse files
authored
perf(db-postgres): do not push database schema if not changed (#10155)
Based on #10154 If the actual database schema is not changed (no new columns, enums, indexes, tables) - skip calling Drizzle push. This, potentially can significantly reduce overhead on reloads in development mode especially when using remote databases. If for whatever reason you need to preserve the current behavior you can use `PAYLOAD_FORCE_DRIZZLE_PUSH=true` env flag.
1 parent 374b79d commit 98666eb

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

packages/drizzle/src/utilities/pushDevSchema.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
import { deepStrictEqual } from 'assert'
12
import prompts from 'prompts'
23

34
import type { BasePostgresAdapter } from '../postgres/types.js'
4-
import type { DrizzleAdapter, PostgresDB } from '../types.js'
5+
import type { DrizzleAdapter, PostgresDB, RawTable } from '../types.js'
6+
7+
const previousSchema: {
8+
localeCodes: null | string[]
9+
rawTables: null | Record<string, RawTable>
10+
} = {
11+
localeCodes: null,
12+
rawTables: null,
13+
}
514

615
/**
716
* Pushes the development schema to the database using Drizzle.
@@ -10,6 +19,27 @@ import type { DrizzleAdapter, PostgresDB } from '../types.js'
1019
* @returns {Promise<void>} - A promise that resolves once the schema push is complete.
1120
*/
1221
export const pushDevSchema = async (adapter: DrizzleAdapter) => {
22+
if (process.env.PAYLOAD_FORCE_DRIZZLE_PUSH !== 'true') {
23+
const localeCodes =
24+
adapter.payload.config.localization && adapter.payload.config.localization.localeCodes
25+
26+
try {
27+
deepStrictEqual(previousSchema, {
28+
localeCodes,
29+
rawTables: adapter.rawTables,
30+
})
31+
32+
if (adapter.logger) {
33+
adapter.payload.logger.info('No changes detected in schema, skipping schema push.')
34+
}
35+
36+
return
37+
} catch {
38+
previousSchema.localeCodes = localeCodes
39+
previousSchema.rawTables = adapter.rawTables
40+
}
41+
}
42+
1343
const { pushSchema } = adapter.requireDrizzleKit()
1444

1545
const { extensions = {}, tablesFilter } = adapter as BasePostgresAdapter

test/database/int.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,10 @@ describe('database', () => {
952952
})
953953

954954
describe('drizzle: schema hooks', () => {
955+
beforeAll(() => {
956+
process.env.PAYLOAD_FORCE_DRIZZLE_PUSH = 'true'
957+
})
958+
955959
it('should add tables with hooks', async () => {
956960
// eslint-disable-next-line jest/no-conditional-in-test
957961
if (payload.db.name === 'mongoose') {

0 commit comments

Comments
 (0)