diff --git a/packages/drizzle/src/utilities/pushDevSchema.ts b/packages/drizzle/src/utilities/pushDevSchema.ts index 619ca6ced65..46f2495aa0c 100644 --- a/packages/drizzle/src/utilities/pushDevSchema.ts +++ b/packages/drizzle/src/utilities/pushDevSchema.ts @@ -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 +} = { + 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} - 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 diff --git a/test/database/int.spec.ts b/test/database/int.spec.ts index 1cafbc8fd00..9286560d01f 100644 --- a/test/database/int.spec.ts +++ b/test/database/int.spec.ts @@ -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') {