Skip to content

Commit ec2323d

Browse files
[psql]: kit pk tests
1 parent f96c3a4 commit ec2323d

File tree

8 files changed

+270
-83
lines changed

8 files changed

+270
-83
lines changed

drizzle-kit/src/dialects/postgres/convertor.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,10 @@ const dropPrimaryKeyConvertor = convertor('drop_pk', (st) => {
544544
});
545545
546546
const recreatePrimaryKeyConvertor = convertor('alter_pk', (it) => {
547-
const drop = dropPrimaryKeyConvertor.convert({ pk: it.pk }) as string;
548-
const create = addPrimaryKeyConvertor.convert({ pk: it.pk }) as string;
549-
return [drop, create];
547+
const st: string[] = [];
548+
if (!it.deleted) st.push(dropPrimaryKeyConvertor.convert({ pk: it.pk }) as string);
549+
st.push(addPrimaryKeyConvertor.convert({ pk: it.pk }) as string);
550+
return st;
550551
});
551552
552553
const renameConstraintConvertor = convertor('rename_constraint', (st) => {

drizzle-kit/src/dialects/postgres/diff.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import type {
2727
} from './ddl';
2828
import { createDDL, tableFromDDL } from './ddl';
2929
import { defaults, defaultsCommutative } from './grammar';
30-
import type { JsonRecreateIndex, JsonStatement } from './statements';
30+
import type { JsonAlterPrimaryKey, JsonRecreateIndex, JsonStatement } from './statements';
3131
import { prepareStatement } from './statements';
3232

3333
export const ddlDiffDry = async (ddlFrom: PostgresDDL, ddlTo: PostgresDDL, mode: 'default' | 'push') => {
@@ -728,7 +728,10 @@ export const ddlDiff = async (
728728
const jsonAddColumnsStatemets = columnsToCreate.filter(tablesFilter('created')).map((it) =>
729729
prepareStatement('add_column', {
730730
column: it,
731-
isPK: ddl2.pks.one({ schema: it.schema, table: it.table, columns: [it.name] }) !== null,
731+
// if pk existed before and new column now has pk, this will trigger alter_pk, that will automatically add pk
732+
// this flag is needed for column recreation (generated)
733+
// see tests: "drizzle-kit/tests/postgres/pg-constraints.test.ts" => "remove/add pk" and below
734+
isPK: false, // ddl2.pks.one({ schema: it.schema, table: it.table, columns: [it.name] }) !== null,
732735
isCompositePK: ddl2.pks.one({ schema: it.schema, table: it.table, columns: { CONTAINS: it.name } }) !== null,
733736
})
734737
);
@@ -845,7 +848,11 @@ export const ddlDiff = async (
845848
});
846849

847850
const alteredChecks = alters.filter((it) => it.entityType === 'checks');
848-
const jsonAlteredPKs = alteredPKs.map((it) => prepareStatement('alter_pk', { diff: it, pk: it.$right }));
851+
const jsonAlteredPKs: JsonAlterPrimaryKey[] = alteredPKs.map((it) => {
852+
const deleted = columnsToDelete.some((x) => it.columns?.from.includes(x.name));
853+
854+
return prepareStatement('alter_pk', { diff: it, pk: it.$right, deleted });
855+
});
849856

850857
const jsonRecreateFKs = alters.filter((it) => it.entityType === 'fks').filter((x) => {
851858
if (
@@ -1220,13 +1227,17 @@ export const ddlDiff = async (
12201227
jsonStatements.push(...jsonDropIndexes);
12211228
jsonStatements.push(...jsonDropPrimaryKeys);
12221229

1223-
jsonStatements.push(...jsonAddPrimaryKeys);
1224-
jsonStatements.push(...jsonRenamePrimaryKey);
12251230
jsonStatements.push(...jsonRenameReferences);
12261231
jsonStatements.push(...jsonAddColumnsStatemets);
1232+
jsonStatements.push(...jsonAddPrimaryKeys);
1233+
jsonStatements.push(...jsonRenamePrimaryKey);
12271234
jsonStatements.push(...recreateEnums);
12281235
jsonStatements.push(...jsonRecreateColumns);
1236+
1237+
jsonStatements.push(...jsonDropColumnsStatemets);
1238+
jsonStatements.push(...jsonAlteredPKs);
12291239
jsonStatements.push(...jsonAlterColumns);
1240+
12301241
jsonStatements.push(...jsonRecreateIndex);
12311242

12321243
jsonStatements.push(...jsonRenamedUniqueConstraints);
@@ -1237,9 +1248,6 @@ export const ddlDiff = async (
12371248
jsonStatements.push(...jsonCreateFKs);
12381249
jsonStatements.push(...jsonRecreateFKs);
12391250

1240-
jsonStatements.push(...jsonDropColumnsStatemets);
1241-
jsonStatements.push(...jsonAlteredPKs);
1242-
12431251
jsonStatements.push(...jsonCreatedCheckConstraints);
12441252

12451253
jsonStatements.push(...jsonAlterCheckConstraints);

drizzle-kit/src/dialects/postgres/statements.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ export interface JsonAlterPrimaryKey {
278278
type: 'alter_pk';
279279
pk: PrimaryKey;
280280
diff: DiffEntities['pks'];
281+
deleted?: boolean;
281282
}
282283

283284
export interface JsonMoveTable {

drizzle-kit/tests/cockroach/identity.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ test.concurrent('drop identity from a column - no params', async ({ db }) => {
228228
const { sqlStatements: pst } = await push({
229229
db,
230230
to,
231-
log: 'statements',
232231
});
233232

234233
const st0 = [

drizzle-kit/tests/cockroach/views.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ test.concurrent('create materialized view', async ({ db }) => {
148148
const { sqlStatements: pst } = await push({
149149
db,
150150
to: schema2,
151-
log: 'statements',
152151
});
153152

154153
const st0: string[] = [

drizzle-kit/tests/postgres/pg-columns.test.ts

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -342,75 +342,6 @@ test('alter table add composite pk', async (t) => {
342342
expect(pst).toStrictEqual(st0);
343343
});
344344

345-
// https://github.com/drizzle-team/drizzle-orm/issues/3496
346-
test('remove/add pk', async (t) => {
347-
const Step = pgTable('Step', {
348-
id: bigint({ mode: 'number' }).primaryKey(),
349-
});
350-
const schema1 = {
351-
Step1: Step,
352-
Branch: pgTable('Branch', {
353-
id: bigint({ mode: 'number' }).primaryKey(),
354-
stepId: bigint({ mode: 'number' }).references(() => Step.id, { onDelete: 'cascade' }),
355-
}),
356-
};
357-
358-
const { next: n1 } = await diff({}, schema1, []);
359-
await push({ db, to: schema1 });
360-
361-
const schema2 = {
362-
Step,
363-
Branch: pgTable('Branch', {
364-
stepId: bigint({ mode: 'number' }).primaryKey().references(() => Step.id, { onDelete: 'cascade' }),
365-
}),
366-
};
367-
368-
const { sqlStatements: st2 } = await diff(n1, schema2, []);
369-
const { sqlStatements: pst2 } = await push({ db, to: schema2, log: 'statements' });
370-
371-
const expectedSt2 = [
372-
'ALTER TABLE "Branch" DROP COLUMN "id";',
373-
'ALTER TABLE "Branch" ADD PRIMARY KEY ("stepId");',
374-
];
375-
expect(st2).toStrictEqual(expectedSt2);
376-
expect(pst2).toStrictEqual(expectedSt2);
377-
});
378-
test('remove/add pk #2', async (t) => {
379-
const Step = pgTable('Step', {
380-
id: bigint({ mode: 'number' }).primaryKey(),
381-
});
382-
const schema1 = {
383-
Step1: Step,
384-
Branch: pgTable('Branch', {
385-
id: bigint({ mode: 'number' }).primaryKey(),
386-
stepId: bigint({ mode: 'number' }).references(() => Step.id, { onDelete: 'cascade' }),
387-
}),
388-
};
389-
390-
const { next: n1 } = await diff({}, schema1, []);
391-
await push({ db, to: schema1 });
392-
393-
const schema2 = {
394-
Step,
395-
Branch: pgTable('Branch', {
396-
stepId: bigint({ mode: 'number' }).references(() => Step.id, { onDelete: 'cascade' }),
397-
stepId2: bigint({ mode: 'number' }).primaryKey(),
398-
}),
399-
};
400-
401-
const { sqlStatements: st2 } = await diff(n1, schema2, []);
402-
const { sqlStatements: pst2 } = await push({ db, to: schema2, log: 'statements' });
403-
404-
const expectedSt2 = [
405-
'ALTER TABLE "Branch" ADD COLUMN "stepId2" bigint PRIMARY KEY;',
406-
'ALTER TABLE "Branch" DROP CONSTRAINT "Branch_pkey";',
407-
'ALTER TABLE "Branch" ADD PRIMARY KEY ("stepId2");',
408-
'ALTER TABLE "Branch" DROP COLUMN "id";',
409-
];
410-
expect(st2).toStrictEqual(expectedSt2);
411-
expect(pst2).toStrictEqual(expectedSt2);
412-
});
413-
414345
test('rename table rename column #1', async (t) => {
415346
const schema1 = {
416347
users: pgTable('users', {

0 commit comments

Comments
 (0)