Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Enum Array Column Types for PostgreSQL Migrations #3759

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions drizzle-kit/src/serializer/pgSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getTableConfig,
getViewConfig,
IndexedColumn,
PgArray,
PgColumn,
PgDialect,
PgEnum,
Expand Down Expand Up @@ -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<any>[],
Expand Down Expand Up @@ -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;

Expand All @@ -174,7 +188,7 @@ export const generatePgSnapshot = (
const columnToSet: Column = {
name,
type: column.getSQLType(),
typeSchema: typeSchema,
typeSchema,
primaryKey,
notNull,
generated: generated
Expand Down
2 changes: 2 additions & 0 deletions drizzle-kit/tests/pg-array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ test('array #11: enum array default', async (t) => {
primaryKey: false,
notNull: false,
default: '\'{"a","b","c"}\'',
typeSchema: 'public',
},
});
});
Expand Down Expand Up @@ -363,6 +364,7 @@ test('array #12: enum empty array default', async (t) => {
primaryKey: false,
notNull: false,
default: "'{}'",
typeSchema: 'public',
},
});
});
25 changes: 25 additions & 0 deletions drizzle-kit/tests/pg-enums.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand Down