|
1 | 1 | import { sql } from 'drizzle-orm'; |
| 2 | +import { cockroachTable } from 'drizzle-orm/cockroach-core'; |
2 | 3 | import { |
3 | 4 | AnyMsSqlColumn, |
4 | 5 | check, |
@@ -1671,6 +1672,45 @@ test('fk multistep #2', async () => { |
1671 | 1672 | expect(pst3).toStrictEqual([]); |
1672 | 1673 | }); |
1673 | 1674 |
|
| 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 | + |
1674 | 1714 | test('add check', async () => { |
1675 | 1715 | const schema1 = { |
1676 | 1716 | table: mssqlTable('table', { |
@@ -2379,3 +2419,46 @@ test('index duplicate name', async (t) => { |
2379 | 2419 | await expect(diff({}, to, [])).rejects.toThrowError(); |
2380 | 2420 | await expect(push({ db, to })).rejects.toThrowError(); |
2381 | 2421 | }); |
| 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 | +}); |
0 commit comments