Skip to content

Commit 8710ec1

Browse files
committed
[drizzle-kit] updated mysql tests
1 parent 090c6e3 commit 8710ec1

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,3 +855,58 @@ test('drop column with pk and add pk to another column #2', async () => {
855855
expect(st2).toStrictEqual(expectedSt2);
856856
expect(pst2).toStrictEqual(expectedSt2);
857857
});
858+
859+
// https://github.com/drizzle-team/drizzle-orm/issues/4456
860+
test('drop column with pk and add pk to another column #3', async () => {
861+
const schema1 = {
862+
authors: mysqlTable(
863+
'authors',
864+
{
865+
publicationId: varchar('publication_id', { length: 64 }),
866+
authorID: varchar('author_id', { length: 10 }),
867+
},
868+
(table) => {
869+
return {
870+
pk: primaryKey(table.publicationId, table.authorID),
871+
};
872+
},
873+
),
874+
};
875+
876+
const { sqlStatements: st1, next: n1 } = await diff({}, schema1, []);
877+
const { sqlStatements: pst1 } = await push({ db, to: schema1 });
878+
const expectedSt1 = [
879+
'CREATE TABLE `authors` (\n\t`publication_id` varchar(64),\n\t`author_id` varchar(10),'
880+
+ '\n\tCONSTRAINT `PRIMARY` PRIMARY KEY(`publication_id`,`author_id`)\n);\n',
881+
];
882+
expect(st1).toStrictEqual(expectedSt1);
883+
expect(pst1).toStrictEqual(expectedSt1);
884+
885+
const schema2 = {
886+
authors: mysqlTable(
887+
'authors',
888+
{
889+
publicationId: varchar('publication_id', { length: 64 }),
890+
authorID: varchar('author_id', { length: 10 }),
891+
orcidId: varchar('orcid_id', { length: 64 }),
892+
},
893+
(table) => {
894+
return {
895+
pk: primaryKey(table.publicationId, table.authorID, table.orcidId),
896+
};
897+
},
898+
),
899+
};
900+
901+
const { sqlStatements: st2 } = await diff(n1, schema2, []);
902+
const { sqlStatements: pst2 } = await push({ db, to: schema2 });
903+
904+
const expectedSt2: string[] = [
905+
'ALTER TABLE `authors` DROP PRIMARY KEY;',
906+
'ALTER TABLE `authors` ADD `orcid_id` varchar(64);',
907+
'ALTER TABLE `authors` ADD PRIMARY KEY (`publication_id`,`author_id`,`orcid_id`);',
908+
];
909+
910+
expect(st2).toStrictEqual(expectedSt2);
911+
expect(pst2).toStrictEqual(expectedSt2);
912+
});

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,38 @@ test('drop + add table', async () => {
237237
expect(pst2).toStrictEqual(expectedSt2);
238238
});
239239

240+
// https://github.com/drizzle-team/drizzle-orm/issues/4456
241+
test('drop tables with fk constraint', async () => {
242+
const table1 = mysqlTable('table1', {
243+
column1: int().primaryKey(),
244+
});
245+
const table2 = mysqlTable('table1', {
246+
column1: int().primaryKey(),
247+
column2: int().references(() => table1.column1),
248+
});
249+
const schema1 = { table1, table2 };
250+
251+
const { sqlStatements: st1, next: n1 } = await diff({}, schema1, []);
252+
const { sqlStatements: pst1 } = await push({ db, to: schema1 });
253+
const expectedSt1 = [
254+
'CREATE TABLE `table1` (\n\t`column1` int PRIMARY KEY\n);\n',
255+
'CREATE TABLE `table2` (\n\t`column1` int PRIMARY KEY,\n\t`column2` int,'
256+
+ '\n\tCONSTRAINT `table1_column2_table1_column1_fkey` FOREIGN KEY (`column2`) REFERENCES `table1`(`column1`)\n);\n',
257+
];
258+
expect(st1).toStrictEqual(expectedSt1);
259+
expect(pst1).toStrictEqual(expectedSt1);
260+
261+
const { sqlStatements: st2 } = await diff(n1, {}, []);
262+
const { sqlStatements: pst2 } = await push({ db, to: {} });
263+
264+
const expectedSt2 = [
265+
'DROP TABLE `table2`;',
266+
'DROP TABLE `table1`;',
267+
];
268+
expect(st2).toStrictEqual(expectedSt2);
269+
expect(pst2).toStrictEqual(expectedSt2);
270+
});
271+
240272
test('add schema + table #1', async () => {
241273
const schema = mysqlSchema('folder');
242274

0 commit comments

Comments
 (0)