diff --git a/drizzle-kit/src/serializer/pgSerializer.ts b/drizzle-kit/src/serializer/pgSerializer.ts index b0faa5ea8..82a59e38a 100644 --- a/drizzle-kit/src/serializer/pgSerializer.ts +++ b/drizzle-kit/src/serializer/pgSerializer.ts @@ -6,6 +6,7 @@ import { getTableConfig, getViewConfig, IndexedColumn, + PgArray, PgColumn, PgDialect, PgEnum, @@ -97,6 +98,19 @@ export function buildArrayString(array: any[], sqlType: string): string { return `{${values}}`; } +const getTypeSchema = (column: PgColumn): string | undefined => { + if (is(column, PgArray)) { + const { baseColumn } = column; + if (is(baseColumn, PgEnumColumn)) { + return baseColumn.enum.schema || 'public'; + } + } + + if (is(column, PgEnumColumn)) { + return column.enum.schema || 'public'; + } +}; + export const generatePgSnapshot = ( tables: AnyPgTable[], enums: PgEnum[], @@ -158,7 +172,7 @@ export const generatePgSnapshot = ( const primaryKey: boolean = column.primary; const sqlTypeLowered = column.getSQLType().toLowerCase(); - const typeSchema = is(column, PgEnumColumn) ? column.enum.schema || 'public' : undefined; + const typeSchema = getTypeSchema(column); const generated = column.generated; const identity = column.generatedIdentity; @@ -174,7 +188,7 @@ export const generatePgSnapshot = ( const columnToSet: Column = { name, type: column.getSQLType(), - typeSchema: typeSchema, + typeSchema, primaryKey, notNull, generated: generated diff --git a/drizzle-kit/tests/pg-array.test.ts b/drizzle-kit/tests/pg-array.test.ts index e6c06d535..300355ce2 100644 --- a/drizzle-kit/tests/pg-array.test.ts +++ b/drizzle-kit/tests/pg-array.test.ts @@ -329,6 +329,7 @@ test('array #11: enum array default', async (t) => { primaryKey: false, notNull: false, default: '\'{"a","b","c"}\'', + typeSchema: 'public', }, }); }); @@ -363,6 +364,7 @@ test('array #12: enum empty array default', async (t) => { primaryKey: false, notNull: false, default: "'{}'", + typeSchema: 'public', }, }); }); diff --git a/drizzle-kit/tests/pg-enums.test.ts b/drizzle-kit/tests/pg-enums.test.ts index 2af691d46..d6b002887 100644 --- a/drizzle-kit/tests/pg-enums.test.ts +++ b/drizzle-kit/tests/pg-enums.test.ts @@ -577,6 +577,31 @@ test('enums #21', async () => { ]); }); +test('enums #22 - enum array in schema', async () => { + const schema = pgSchema('custom_schema'); + const myEnum = schema.enum('my_enum', ['one', 'two', 'three']); + + const from = { + myEnum, + table: schema.table('table', { + id: serial('id').primaryKey(), + }), + }; + + const to = { + myEnum, + table: schema.table('table', { + id: serial('id').primaryKey(), + col1: myEnum('col1').array(), + }), + }; + + const { sqlStatements } = await diffTestSchemas(from, to, []); + + expect(sqlStatements.length).toBe(1); + expect(sqlStatements[0]).toBe('ALTER TABLE "custom_schema"."table" ADD COLUMN "col1" "custom_schema"."my_enum"[];'); +}); + test('drop enum value', async () => { const enum1 = pgEnum('enum', ['value1', 'value2', 'value3']);