Skip to content

Commit 635addf

Browse files
committed
Merge remote-tracking branch 'origin/alternation-engine' into alternation-engine
2 parents 3394cdb + cacd22b commit 635addf

File tree

8 files changed

+380
-17
lines changed

8 files changed

+380
-17
lines changed

.github/workflows/release-feature-branch.yaml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -273,23 +273,6 @@ jobs:
273273
*) echo "Unknown shard: ${{matrix.shard}}"; exit 1 ;;
274274
esac
275275
276-
- name: Stop DBs
277-
if: always() && ${{ matrix.dbs && join(matrix.dbs, ',') != '' }}
278-
shell: bash
279-
run: |
280-
set -euxo pipefail
281-
for db in ${{ join(matrix.dbs, ' ') }}; do
282-
case "$db" in
283-
postgres) docker compose -f compose/postgres.yml down -v ;;
284-
postgres-postgis) docker compose -f compose/postgres-postgis.yml up -d ;;
285-
mysql) docker compose -f compose/mysql.yml down -v ;;
286-
singlestore) docker compose -f compose/singlestore.yml down -v ;;
287-
singlestore-many) docker compose -f compose/singlestore-many.yml down -v ;;
288-
mssql) docker compose -f compose/mssql.yml down -v ;;
289-
cockroach) docker compose -f compose/cockroach.yml down -v ;;
290-
esac
291-
done
292-
293276
attw:
294277
needs: [prepare]
295278
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,45 @@ test.concurrent('fk multistep #2', async ({ dbc: db }) => {
16221622
expect(pst3).toStrictEqual([]);
16231623
});
16241624

1625+
// https://github.com/drizzle-team/drizzle-orm/issues/4456#issuecomment-3076042688
1626+
test('fk multistep #4', async ({ dbc: db }) => {
1627+
const foo = cockroachTable('foo', {
1628+
id: int4().primaryKey(),
1629+
});
1630+
1631+
const bar = cockroachTable('bar', {
1632+
id: int4().primaryKey(),
1633+
fooId: int4().references(() => foo.id),
1634+
});
1635+
1636+
const schema1 = { foo, bar };
1637+
1638+
const { sqlStatements: st1, next: n1 } = await diff({}, schema1, []);
1639+
const { sqlStatements: pst1 } = await push({ db, to: schema1 });
1640+
const expectedSt1 = [
1641+
'CREATE TABLE "foo" (\n\t"id" integer PRIMARY KEY\n);\n',
1642+
'CREATE TABLE "bar" (\n\t"id" integer PRIMARY KEY,\n\t"fooId" integer\n);\n',
1643+
'ALTER TABLE "bar" ADD CONSTRAINT "bar_fooId_foo_id_fkey" FOREIGN KEY ("fooId") REFERENCES "foo"("id");',
1644+
];
1645+
expect(st1).toStrictEqual(expectedSt1);
1646+
expect(pst1).toStrictEqual(expectedSt1);
1647+
1648+
const schema2 = {
1649+
bar: cockroachTable('bar', {
1650+
id: int4().primaryKey(),
1651+
fooId: int4(),
1652+
}),
1653+
};
1654+
const { sqlStatements: st2 } = await diff(n1, schema2, []);
1655+
const { sqlStatements: pst2 } = await push({ db, to: schema2 });
1656+
const expectedSt2 = [
1657+
'ALTER TABLE "bar" DROP CONSTRAINT "bar_fooId_foo_id_fkey";',
1658+
'DROP TABLE "foo";',
1659+
];
1660+
expect(st2).toStrictEqual(expectedSt2);
1661+
expect(pst2).toStrictEqual(expectedSt2);
1662+
});
1663+
16251664
test.concurrent('unique duplicate name', async ({ dbc: db }) => {
16261665
const from = {
16271666
users: cockroachTable('users', {
@@ -1821,3 +1860,46 @@ test.concurrent('alter pk test #3', async ({ dbc: db }) => {
18211860
expect(sqlStatements).toStrictEqual(st0);
18221861
expect(pst).toStrictEqual(st0);
18231862
});
1863+
1864+
// https://github.com/drizzle-team/drizzle-orm/issues/4456
1865+
test('drop column with pk and add pk to another column #1', async ({ dbc: db }) => {
1866+
const schema1 = {
1867+
authors: cockroachTable('authors', {
1868+
publicationId: varchar('publication_id', { length: 64 }),
1869+
authorID: varchar('author_id', { length: 10 }),
1870+
}, (table) => [
1871+
primaryKey({ columns: [table.publicationId, table.authorID] }),
1872+
]),
1873+
};
1874+
1875+
const { sqlStatements: st1, next: n1 } = await diff({}, schema1, []);
1876+
const { sqlStatements: pst1 } = await push({ db, to: schema1 });
1877+
const expectedSt1 = [
1878+
'CREATE TABLE "authors" (\n\t"publication_id" varchar(64),\n\t"author_id" varchar(10),'
1879+
+ '\n\tCONSTRAINT "authors_pkey" PRIMARY KEY("publication_id","author_id")\n);\n',
1880+
];
1881+
expect(st1).toStrictEqual(expectedSt1);
1882+
expect(pst1).toStrictEqual(expectedSt1);
1883+
1884+
const schema2 = {
1885+
authors: cockroachTable('authors', {
1886+
publicationId: varchar('publication_id', { length: 64 }),
1887+
authorID: varchar('author_id', { length: 10 }),
1888+
orcidId: varchar('orcid_id', { length: 64 }),
1889+
}, (table) => [
1890+
primaryKey({ columns: [table.publicationId, table.authorID, table.orcidId] }),
1891+
]),
1892+
};
1893+
1894+
const { sqlStatements: st2 } = await diff(n1, schema2, []);
1895+
const { sqlStatements: pst2 } = await push({ db, to: schema2 });
1896+
1897+
const expectedSt2: string[] = [
1898+
'ALTER TABLE "authors" ADD COLUMN "orcid_id" varchar(64);',
1899+
'ALTER TABLE "authors" DROP CONSTRAINT "authors_pkey";',
1900+
'ALTER TABLE "authors" ADD PRIMARY KEY ("publication_id","author_id","orcid_id");',
1901+
];
1902+
1903+
expect(st2).toStrictEqual(expectedSt2);
1904+
expect(pst2).toStrictEqual(expectedSt2);
1905+
});

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,37 @@ test.concurrent('drop table + rename schema #1', async ({ dbc: db }) => {
760760
expect(pst).toStrictEqual(st0);
761761
});
762762

763+
test('drop tables with fk constraint', async ({ dbc: db }) => {
764+
const table1 = cockroachTable('table1', {
765+
column1: int4().primaryKey(),
766+
});
767+
const table2 = cockroachTable('table2', {
768+
column1: int4().primaryKey(),
769+
column2: int4().references(() => table1.column1),
770+
});
771+
const schema1 = { table1, table2 };
772+
773+
const { sqlStatements: st1, next: n1 } = await diff({}, schema1, []);
774+
const { sqlStatements: pst1 } = await push({ db, to: schema1 });
775+
const expectedSt1 = [
776+
'CREATE TABLE "table1" (\n\t"column1" integer PRIMARY KEY\n);\n',
777+
'CREATE TABLE "table2" (\n\t"column1" integer PRIMARY KEY,\n\t"column2" integer\n);\n',
778+
'ALTER TABLE "table2" ADD CONSTRAINT "table2_column2_table1_column1_fkey" FOREIGN KEY ("column2") REFERENCES "table1"("column1");',
779+
];
780+
expect(st1).toStrictEqual(expectedSt1);
781+
expect(pst1).toStrictEqual(expectedSt1);
782+
783+
const { sqlStatements: st2 } = await diff(n1, {}, []);
784+
const { sqlStatements: pst2 } = await push({ db, to: {} });
785+
786+
const expectedSt2 = [
787+
'DROP TABLE "table2";',
788+
'DROP TABLE "table1";',
789+
];
790+
expect(st2).toStrictEqual(expectedSt2);
791+
expect(pst2).toStrictEqual(expectedSt2);
792+
});
793+
763794
test.concurrent('create table with tsvector', async ({ dbc: db }) => {
764795
const from = {};
765796
const to = {

drizzle-kit/tests/mssql/constraints.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { sql } from 'drizzle-orm';
2+
import { cockroachTable } from 'drizzle-orm/cockroach-core';
23
import {
34
AnyMsSqlColumn,
45
check,
@@ -1671,6 +1672,45 @@ test('fk multistep #2', async () => {
16711672
expect(pst3).toStrictEqual([]);
16721673
});
16731674

1675+
// https://github.com/drizzle-team/drizzle-orm/issues/4456#issuecomment-3076042688
1676+
test('fk multistep #3', async () => {
1677+
const foo = mssqlTable('foo', {
1678+
id: int().primaryKey(),
1679+
});
1680+
1681+
const bar = mssqlTable('bar', {
1682+
id: int().primaryKey(),
1683+
fooId: int().references(() => foo.id),
1684+
});
1685+
1686+
const schema1 = { foo, bar };
1687+
1688+
const { sqlStatements: st1, next: n1 } = await diff({}, schema1, []);
1689+
const { sqlStatements: pst1 } = await push({ db, to: schema1 });
1690+
const expectedSt1 = [
1691+
'CREATE TABLE [foo] (\n\t[id] int PRIMARY KEY\n);\n',
1692+
'CREATE TABLE [bar] (\n\t[id] int PRIMARY KEY,\n\t[fooId] int\n);\n',
1693+
'ALTER TABLE [bar] ADD CONSTRAINT [bar_fooId_foo_id_fkey] FOREIGN KEY ([fooId]) REFERENCES [foo]([id]);',
1694+
];
1695+
expect(st1).toStrictEqual(expectedSt1);
1696+
expect(pst1).toStrictEqual(expectedSt1);
1697+
1698+
const schema2 = {
1699+
bar: mssqlTable('bar', {
1700+
id: int().primaryKey(),
1701+
fooId: int(),
1702+
}),
1703+
};
1704+
const { sqlStatements: st2 } = await diff(n1, schema2, []);
1705+
const { sqlStatements: pst2 } = await push({ db, to: schema2 });
1706+
const expectedSt2 = [
1707+
'ALTER TABLE [bar] DROP CONSTRAINT [bar_fooId_foo_id_fkey];',
1708+
'DROP TABLE [foo];',
1709+
];
1710+
expect(st2).toStrictEqual(expectedSt2);
1711+
expect(pst2).toStrictEqual(expectedSt2);
1712+
});
1713+
16741714
test('add check', async () => {
16751715
const schema1 = {
16761716
table: mssqlTable('table', {
@@ -2379,3 +2419,46 @@ test('index duplicate name', async (t) => {
23792419
await expect(diff({}, to, [])).rejects.toThrowError();
23802420
await expect(push({ db, to })).rejects.toThrowError();
23812421
});
2422+
2423+
// https://github.com/drizzle-team/drizzle-orm/issues/4456
2424+
test('drop column with pk and add pk to another column #1', async () => {
2425+
const schema1 = {
2426+
authors: mssqlTable('authors', {
2427+
publicationId: varchar('publication_id', { length: 64 }),
2428+
authorID: varchar('author_id', { length: 10 }),
2429+
}, (table) => [
2430+
primaryKey({ columns: [table.publicationId, table.authorID] }),
2431+
]),
2432+
};
2433+
2434+
const { sqlStatements: st1, next: n1 } = await diff({}, schema1, []);
2435+
const { sqlStatements: pst1 } = await push({ db, to: schema1 });
2436+
const expectedSt1 = [
2437+
'CREATE TABLE [authors] (\n\t[publication_id] varchar(64),\n\t[author_id] varchar(10),'
2438+
+ '\n\tCONSTRAINT [authors_pkey] PRIMARY KEY([publication_id],[author_id])\n);\n',
2439+
];
2440+
expect(st1).toStrictEqual(expectedSt1);
2441+
expect(pst1).toStrictEqual(expectedSt1);
2442+
2443+
const schema2 = {
2444+
authors: cockroachTable('authors', {
2445+
publicationId: varchar('publication_id', { length: 64 }),
2446+
authorID: varchar('author_id', { length: 10 }),
2447+
orcidId: varchar('orcid_id', { length: 64 }),
2448+
}, (table) => [
2449+
primaryKey({ columns: [table.publicationId, table.authorID, table.orcidId] }),
2450+
]),
2451+
};
2452+
2453+
const { sqlStatements: st2 } = await diff(n1, schema2, []);
2454+
const { sqlStatements: pst2 } = await push({ db, to: schema2 });
2455+
2456+
const expectedSt2: string[] = [
2457+
'ALTER TABLE [authors] ADD COLUMN [orcid_id] varchar(64);',
2458+
'ALTER TABLE [authors] DROP CONSTRAINT [authors_pkey];',
2459+
'ALTER TABLE [authors] ADD PRIMARY KEY ([publication_id],[author_id],[orcid_id]);',
2460+
];
2461+
2462+
expect(st2).toStrictEqual(expectedSt2);
2463+
expect(pst2).toStrictEqual(expectedSt2);
2464+
});

drizzle-kit/tests/mssql/tables.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,37 @@ test('drop table + rename schema #1', async () => {
609609
})).rejects.toThrowError(); // no folder2.users to drop
610610
});
611611

612+
test('drop tables with fk constraint', async () => {
613+
const table1 = mssqlTable('table1', {
614+
column1: int().primaryKey(),
615+
});
616+
const table2 = mssqlTable('table2', {
617+
column1: int().primaryKey(),
618+
column2: int().references(() => table1.column1),
619+
});
620+
const schema1 = { table1, table2 };
621+
622+
const { sqlStatements: st1, next: n1 } = await diff({}, schema1, []);
623+
const { sqlStatements: pst1 } = await push({ db, to: schema1 });
624+
const expectedSt1 = [
625+
'CREATE TABLE [table1] (\n\t[column1] integer PRIMARY KEY\n);\n',
626+
'CREATE TABLE [table2] (\n\t[column1] integer PRIMARY KEY,\n\t[column2] integer\n);\n',
627+
'ALTER TABLE [table2] ADD CONSTRAINT [table2_column2_table1_column1_fkey] FOREIGN KEY ([column2]) REFERENCES [table1]([column1]);',
628+
];
629+
expect(st1).toStrictEqual(expectedSt1);
630+
expect(pst1).toStrictEqual(expectedSt1);
631+
632+
const { sqlStatements: st2 } = await diff(n1, {}, []);
633+
const { sqlStatements: pst2 } = await push({ db, to: {} });
634+
635+
const expectedSt2 = [
636+
'DROP TABLE [table2];',
637+
'DROP TABLE [table1];',
638+
];
639+
expect(st2).toStrictEqual(expectedSt2);
640+
expect(pst2).toStrictEqual(expectedSt2);
641+
});
642+
612643
test('composite primary key', async () => {
613644
const from = {};
614645
const to = {

drizzle-kit/tests/mysql/constraints.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,45 @@ test('fk name is too long', async () => {
593593
expect(pst).toStrictEqual(expectedSt);
594594
});
595595

596+
// https://github.com/drizzle-team/drizzle-orm/issues/4456#issuecomment-3076042688
597+
test('fk multistep #1', async () => {
598+
const foo = mysqlTable('foo', {
599+
id: int().primaryKey(),
600+
});
601+
602+
const bar = mysqlTable('bar', {
603+
id: int().primaryKey(),
604+
fooId: int().references(() => foo.id),
605+
});
606+
607+
const schema1 = { foo, bar };
608+
609+
const { sqlStatements: st1, next: n1 } = await diff({}, schema1, []);
610+
const { sqlStatements: pst1 } = await push({ db, to: schema1 });
611+
const expectedSt1 = [
612+
'CREATE TABLE `foo` (\n\t`id` int PRIMARY KEY\n);\n',
613+
'CREATE TABLE `bar` (\n\t`id` int PRIMARY KEY,\n\t`fooId` int\n);\n',
614+
'ALTER TABLE `bar` ADD CONSTRAINT `bar_fooId_foo_id_fkey` FOREIGN KEY (`fooId`) REFERENCES `foo`(`id`);',
615+
];
616+
expect(st1).toStrictEqual(expectedSt1);
617+
expect(pst1).toStrictEqual(expectedSt1);
618+
619+
const schema2 = {
620+
bar: mysqlTable('bar', {
621+
id: int().primaryKey(),
622+
fooId: int(),
623+
}),
624+
};
625+
const { sqlStatements: st2 } = await diff(n1, schema2, []);
626+
const { sqlStatements: pst2 } = await push({ db, to: schema2 });
627+
const expectedSt2 = [
628+
'ALTER TABLE `bar` DROP CONSTRAINT `bar_fooId_foo_id_fkey`;',
629+
'DROP TABLE `foo`;',
630+
];
631+
expect(st2).toStrictEqual(expectedSt2);
632+
expect(pst2).toStrictEqual(expectedSt2);
633+
});
634+
596635
// https://github.com/drizzle-team/drizzle-orm/issues/265
597636
// https://github.com/drizzle-team/drizzle-orm/issues/3293
598637
// https://github.com/drizzle-team/drizzle-orm/issues/2018

0 commit comments

Comments
 (0)