From 6888f13f27eb1cff0cec0c80baa0f42e548e66c2 Mon Sep 17 00:00:00 2001 From: Sasha <64744993+r1tsuu@users.noreply.github.com> Date: Thu, 29 May 2025 22:27:07 +0300 Subject: [PATCH] fix(db-postgres): properly escape the `'` character (#12590) Fixes the issue when `defaultValue` contains `'` it'd double the amount of `'` for the `DEFAULT` statement in the generated migration --- packages/drizzle/src/schema/withDefault.ts | 8 -------- test/database/config.ts | 5 +++++ test/database/int.spec.ts | 1 + test/database/payload-types.ts | 2 ++ .../{20250428_121536.json => 20250528_153134.json} | 2 +- .../migrations/{20250428_121536.ts => 20250528_153134.ts} | 2 +- test/database/up-down-migration/migrations/index.ts | 8 ++++---- 7 files changed, 14 insertions(+), 14 deletions(-) rename test/database/up-down-migration/migrations/{20250428_121536.json => 20250528_153134.json} (99%) rename test/database/up-down-migration/migrations/{20250428_121536.ts => 20250528_153134.ts} (98%) diff --git a/packages/drizzle/src/schema/withDefault.ts b/packages/drizzle/src/schema/withDefault.ts index 03d09453d..566b0339a 100644 --- a/packages/drizzle/src/schema/withDefault.ts +++ b/packages/drizzle/src/schema/withDefault.ts @@ -7,14 +7,6 @@ export const withDefault = (column: RawColumn, field: FieldAffectingData): RawCo return column } - if (typeof field.defaultValue === 'string' && field.defaultValue.includes("'")) { - const escapedString = field.defaultValue.replaceAll("'", "''") - return { - ...column, - default: escapedString, - } - } - return { ...column, default: field.defaultValue, diff --git a/test/database/config.ts b/test/database/config.ts index 76230f516..018e02ceb 100644 --- a/test/database/config.ts +++ b/test/database/config.ts @@ -260,6 +260,11 @@ export default buildConfigWithDefaults({ type: 'point', defaultValue: [10, 20], }, + { + name: 'escape', + type: 'text', + defaultValue: "Thanks, we're excited for you to join us.", + }, ], }, { diff --git a/test/database/int.spec.ts b/test/database/int.spec.ts index c59cec241..514f1bf92 100644 --- a/test/database/int.spec.ts +++ b/test/database/int.spec.ts @@ -1671,6 +1671,7 @@ describe('database', () => { expect(result.group.defaultValue).toStrictEqual('default value from database') expect(result.select).toStrictEqual('default') expect(result.point).toStrictEqual({ coordinates: [10, 20], type: 'Point' }) + expect(result.escape).toStrictEqual("Thanks, we're excited for you to join us.") }) }) diff --git a/test/database/payload-types.ts b/test/database/payload-types.ts index a872bd955..fe1a91c3a 100644 --- a/test/database/payload-types.ts +++ b/test/database/payload-types.ts @@ -239,6 +239,7 @@ export interface DefaultValue { * @maxItems 2 */ point?: [number, number] | null; + escape?: string | null; updatedAt: string; createdAt: string; } @@ -749,6 +750,7 @@ export interface DefaultValuesSelect { }; select?: T; point?: T; + escape?: T; updatedAt?: T; createdAt?: T; } diff --git a/test/database/up-down-migration/migrations/20250428_121536.json b/test/database/up-down-migration/migrations/20250528_153134.json similarity index 99% rename from test/database/up-down-migration/migrations/20250428_121536.json rename to test/database/up-down-migration/migrations/20250528_153134.json index 739b1f0dc..8e1af1ba2 100644 --- a/test/database/up-down-migration/migrations/20250428_121536.json +++ b/test/database/up-down-migration/migrations/20250528_153134.json @@ -1,5 +1,5 @@ { - "id": "36a35217-e468-4780-becb-9146c56e3e54", + "id": "945960df-cc37-406f-a343-58ea672a2286", "prevId": "00000000-0000-0000-0000-000000000000", "version": "7", "dialect": "postgresql", diff --git a/test/database/up-down-migration/migrations/20250428_121536.ts b/test/database/up-down-migration/migrations/20250528_153134.ts similarity index 98% rename from test/database/up-down-migration/migrations/20250428_121536.ts rename to test/database/up-down-migration/migrations/20250528_153134.ts index 5409567ee..086047bb6 100644 --- a/test/database/up-down-migration/migrations/20250428_121536.ts +++ b/test/database/up-down-migration/migrations/20250528_153134.ts @@ -1,4 +1,4 @@ -import type { MigrateDownArgs, MigrateUpArgs } from '@payloadcms/db-postgres' +import type { MigrateDownArgs, MigrateUpArgs} from '@payloadcms/db-postgres'; import { sql } from '@payloadcms/db-postgres' diff --git a/test/database/up-down-migration/migrations/index.ts b/test/database/up-down-migration/migrations/index.ts index eaea2e87a..9a9b0fa86 100644 --- a/test/database/up-down-migration/migrations/index.ts +++ b/test/database/up-down-migration/migrations/index.ts @@ -1,9 +1,9 @@ -import * as migration_20250428_121536 from './20250428_121536.js' +import * as migration_20250528_153134 from './20250528_153134.js' export const migrations = [ { - up: migration_20250428_121536.up, - down: migration_20250428_121536.down, - name: '20250428_121536', + up: migration_20250528_153134.up, + down: migration_20250528_153134.down, + name: '20250528_153134', }, ]