perf(db-postgres): do not push database schema if not changed (#10155)
Based on https://github.com/payloadcms/payload/pull/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.
This commit is contained in:
@@ -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.
|
||||
@@ -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
|
||||
|
||||
@@ -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') {
|
||||
|
||||
Reference in New Issue
Block a user