Skip to content

Commit

Permalink
perf(db-postgres): do not push database schema if not changed (#10155)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
r1tsuu authored Dec 27, 2024
1 parent 374b79d commit 98666eb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
32 changes: 31 additions & 1 deletion packages/drizzle/src/utilities/pushDevSchema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { deepStrictEqual } from 'assert'
import prompts from 'prompts'

import type { BasePostgresAdapter } from '../postgres/types.js'
import type { DrizzleAdapter, PostgresDB } from '../types.js'
import type { DrizzleAdapter, PostgresDB, RawTable } from '../types.js'

const previousSchema: {
localeCodes: null | string[]
rawTables: null | Record<string, RawTable>
} = {
localeCodes: null,
rawTables: null,
}

/**
* Pushes the development schema to the database using Drizzle.
Expand All @@ -10,6 +19,27 @@ import type { DrizzleAdapter, PostgresDB } from '../types.js'
* @returns {Promise<void>} - A promise that resolves once the schema push is complete.
*/
export const pushDevSchema = async (adapter: DrizzleAdapter) => {
if (process.env.PAYLOAD_FORCE_DRIZZLE_PUSH !== 'true') {
const localeCodes =
adapter.payload.config.localization && adapter.payload.config.localization.localeCodes

try {
deepStrictEqual(previousSchema, {
localeCodes,
rawTables: adapter.rawTables,
})

if (adapter.logger) {
adapter.payload.logger.info('No changes detected in schema, skipping schema push.')
}

return
} catch {
previousSchema.localeCodes = localeCodes
previousSchema.rawTables = adapter.rawTables
}
}

const { pushSchema } = adapter.requireDrizzleKit()

const { extensions = {}, tablesFilter } = adapter as BasePostgresAdapter
Expand Down
4 changes: 4 additions & 0 deletions test/database/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,10 @@ describe('database', () => {
})

describe('drizzle: schema hooks', () => {
beforeAll(() => {
process.env.PAYLOAD_FORCE_DRIZZLE_PUSH = 'true'
})

it('should add tables with hooks', async () => {
// eslint-disable-next-line jest/no-conditional-in-test
if (payload.db.name === 'mongoose') {
Expand Down

0 comments on commit 98666eb

Please sign in to comment.