Skip to content
Merged
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
3 changes: 3 additions & 0 deletions changelogs/drizzle-kit/0.32.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### PostgreSQL bug fix

Foreign-key removals now use `DROP CONSTRAINT IF EXISTS`. This prevents generated migrations from failing when an earlier `DROP TABLE ... CASCADE` already removed the same constraint.
2 changes: 1 addition & 1 deletion drizzle-kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@drizzle-team/drizzle-kit",
"version": "0.32.1",
"version": "0.32.3",
"homepage": "https://orm.drizzle.team",
"keywords": [
"drizzle",
Expand Down
2 changes: 1 addition & 1 deletion drizzle-kit/src/sqlgenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3476,7 +3476,7 @@ class PgDeleteForeignKeyConvertor extends Convertor {
? `"${statement.schema}"."${tableFrom}"`
: `"${tableFrom}"`;

return `ALTER TABLE ${tableNameWithSchema} DROP CONSTRAINT "${name}";\n`;
return `ALTER TABLE ${tableNameWithSchema} DROP CONSTRAINT IF EXISTS "${name}";\n`;
}
}

Expand Down
22 changes: 22 additions & 0 deletions drizzle-kit/tests/pg-tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,28 @@ test('alter composite primary key', async () => {
]);
});

test('drop foreign key constraint idempotently', async () => {
const users = pgTable('users', {
id: integer('id').primaryKey(),
});
const postsFrom = pgTable('posts', {
userId: integer('user_id').references(() => users.id),
});
const postsTo = pgTable('posts', {
userId: integer('user_id'),
});

const { sqlStatements } = await diffTestSchemas(
{ users, posts: postsFrom },
{ users, posts: postsTo },
[],
);

expect(sqlStatements).toStrictEqual([
'ALTER TABLE "posts" DROP CONSTRAINT IF EXISTS "posts_user_id_users_id_fk";\n',
]);
});

test('add index with op', async () => {
const from = {
users: pgTable('users', {
Expand Down
Loading