Skip to content

Commit

Permalink
Fix for MySQL and SQLite
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Mario564 committed Oct 9, 2024
1 parent 1836466 commit 38d1af3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
8 changes: 5 additions & 3 deletions drizzle-kit/src/serializer/mysqlSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export const generateMySqlSnapshot = (
}

if (column.isUnique) {
const existingUnique = uniqueConstraintObject[column.uniqueName!];
const originalColumnName = getColumnCasing(column, undefined);
const uniqueName = column.uniqueName!.replace(originalColumnName, name);
const existingUnique = uniqueConstraintObject[uniqueName];
if (typeof existingUnique !== 'undefined') {
console.log(
`\n${
Expand All @@ -115,8 +117,8 @@ export const generateMySqlSnapshot = (
);
process.exit(1);
}
uniqueConstraintObject[column.uniqueName!] = {
name: column.uniqueName!,
uniqueConstraintObject[uniqueName] = {
name: uniqueName,
columns: [columnToSet.name],
};
}
Expand Down
8 changes: 5 additions & 3 deletions drizzle-kit/src/serializer/sqliteSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export const generateSqliteSnapshot = (
columnsObject[name] = columnToSet;

if (column.isUnique) {
const existingUnique = indexesObject[column.uniqueName!];
const originalColumnName = getColumnCasing(column, undefined);
const uniqueName = column.uniqueName!.replace(originalColumnName, name);
const existingUnique = indexesObject[uniqueName];
if (typeof existingUnique !== 'undefined') {
console.log(
`\n${
Expand All @@ -116,8 +118,8 @@ export const generateSqliteSnapshot = (
);
process.exit(1);
}
indexesObject[column.uniqueName!] = {
name: column.uniqueName!,
indexesObject[uniqueName] = {
name: uniqueName,
columns: [columnToSet.name],
isUnique: true,
};
Expand Down
6 changes: 6 additions & 0 deletions drizzle-kit/tests/mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ test('optional db aliases (snake case)', async () => {
t1Col3: int().notNull(),
t2Ref: int().notNull().references(() => t2.t2Id),
t1Uni: int().notNull(),
t1NamelessUnique: int().notNull().unique(),
t1UniIdx: int().notNull(),
t1Idx: int().notNull(),
},
Expand Down Expand Up @@ -627,9 +628,11 @@ test('optional db aliases (snake case)', async () => {
\`t1_col3\` int NOT NULL,
\`t2_ref\` int NOT NULL,
\`t1_uni\` int NOT NULL,
\`t1_nameless_unique\` int NOT NULL,
\`t1_uni_idx\` int NOT NULL,
\`t1_idx\` int NOT NULL,
CONSTRAINT \`t1_t1_id1\` PRIMARY KEY(\`t1_id1\`),
CONSTRAINT \`t1_t1_nameless_unique_unique\` UNIQUE(\`t1_nameless_unique\`),
CONSTRAINT \`t1_uni\` UNIQUE(\`t1_uni\`),
CONSTRAINT \`t1_uni_idx\` UNIQUE(\`t1_uni_idx\`)
);
Expand Down Expand Up @@ -670,6 +673,7 @@ test('optional db aliases (camel case)', async () => {
t1_col3: int().notNull(),
t2_ref: int().notNull().references(() => t2.t2_id),
t1_uni: int().notNull(),
t1_nameless_unique: int().notNull().unique(),
t1_uni_idx: int().notNull(),
t1_idx: int().notNull(),
},
Expand Down Expand Up @@ -718,9 +722,11 @@ test('optional db aliases (camel case)', async () => {
\`t1Col3\` int NOT NULL,
\`t2Ref\` int NOT NULL,
\`t1Uni\` int NOT NULL,
\`t1NamelessUnique\` int NOT NULL,
\`t1UniIdx\` int NOT NULL,
\`t1Idx\` int NOT NULL,
CONSTRAINT \`t1_t1Id1\` PRIMARY KEY(\`t1Id1\`),
CONSTRAINT \`t1_t1NamelessUnique_unique\` UNIQUE(\`t1NamelessUnique\`),
CONSTRAINT \`t1Uni\` UNIQUE(\`t1Uni\`),
CONSTRAINT \`t1UniIdx\` UNIQUE(\`t1UniIdx\`)
);
Expand Down
32 changes: 20 additions & 12 deletions drizzle-kit/tests/sqlite-tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ test('optional db aliases (snake case)', async () => {
t2Ref: int().notNull().references(() => t2.t2Id),
t1Uni: int().notNull(),
t1UniIdx: int().notNull(),
t1NamelessUnique: int().notNull().unique(),
t1Idx: int().notNull(),
},
(table) => ({
Expand Down Expand Up @@ -468,31 +469,34 @@ test('optional db aliases (snake case)', async () => {
\`t2_ref\` integer NOT NULL,
\`t1_uni\` integer NOT NULL,
\`t1_uni_idx\` integer NOT NULL,
\`t1_nameless_unique\` integer NOT NULL,
\`t1_idx\` integer NOT NULL,
FOREIGN KEY (\`t2_ref\`) REFERENCES \`t2\`(\`t2_id\`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (\`t1_col2\`,\`t1_col3\`) REFERENCES \`t3\`(\`t3_id1\`,\`t3_id2\`) ON UPDATE no action ON DELETE no action
);
`;

const st2 = `CREATE UNIQUE INDEX \`t1_uni_idx\` ON \`t1\` (\`t1_uni_idx\`);`;
const st2 = `CREATE UNIQUE INDEX \`t1_t1_nameless_unique_unique\` ON \`t1\` (\`t1_nameless_unique\`);`;

const st3 = `CREATE INDEX \`t1_idx\` ON \`t1\` (\`t1_idx\`);`;
const st3 = `CREATE UNIQUE INDEX \`t1_uni_idx\` ON \`t1\` (\`t1_uni_idx\`);`;

const st4 = `CREATE UNIQUE INDEX \`t1_uni\` ON \`t1\` (\`t1_uni\`);`;
const st4 = `CREATE INDEX \`t1_idx\` ON \`t1\` (\`t1_idx\`);`;

const st5 = `CREATE TABLE \`t2\` (
const st5 = `CREATE UNIQUE INDEX \`t1_uni\` ON \`t1\` (\`t1_uni\`);`;

const st6 = `CREATE TABLE \`t2\` (
\`t2_id\` integer PRIMARY KEY AUTOINCREMENT NOT NULL
);
`;

const st6 = `CREATE TABLE \`t3\` (
const st7 = `CREATE TABLE \`t3\` (
\`t3_id1\` integer,
\`t3_id2\` integer,
PRIMARY KEY(\`t3_id1\`, \`t3_id2\`)
);
`;

expect(sqlStatements).toStrictEqual([st1, st2, st3, st4, st5, st6]);
expect(sqlStatements).toStrictEqual([st1, st2, st3, st4, st5, st6, st7]);
});

test('optional db aliases (camel case)', async () => {
Expand All @@ -506,6 +510,7 @@ test('optional db aliases (camel case)', async () => {
t1_col3: int().notNull(),
t2_ref: int().notNull().references(() => t2.t2_id),
t1_uni: int().notNull(),
t1_nameless_unique: int().notNull().unique(),
t1_uni_idx: int().notNull(),
t1_idx: int().notNull(),
},
Expand Down Expand Up @@ -554,30 +559,33 @@ test('optional db aliases (camel case)', async () => {
\`t1Col3\` integer NOT NULL,
\`t2Ref\` integer NOT NULL,
\`t1Uni\` integer NOT NULL,
\`t1NamelessUnique\` integer NOT NULL,
\`t1UniIdx\` integer NOT NULL,
\`t1Idx\` integer NOT NULL,
FOREIGN KEY (\`t2Ref\`) REFERENCES \`t2\`(\`t2Id\`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (\`t1Col2\`,\`t1Col3\`) REFERENCES \`t3\`(\`t3Id1\`,\`t3Id2\`) ON UPDATE no action ON DELETE no action
);
`;

const st2 = `CREATE UNIQUE INDEX \`t1UniIdx\` ON \`t1\` (\`t1UniIdx\`);`;
const st2 = `CREATE UNIQUE INDEX \`t1_t1NamelessUnique_unique\` ON \`t1\` (\`t1NamelessUnique\`);`;

const st3 = `CREATE UNIQUE INDEX \`t1UniIdx\` ON \`t1\` (\`t1UniIdx\`);`;

const st3 = `CREATE INDEX \`t1Idx\` ON \`t1\` (\`t1Idx\`);`;
const st4 = `CREATE INDEX \`t1Idx\` ON \`t1\` (\`t1Idx\`);`;

const st4 = `CREATE UNIQUE INDEX \`t1Uni\` ON \`t1\` (\`t1Uni\`);`;
const st5 = `CREATE UNIQUE INDEX \`t1Uni\` ON \`t1\` (\`t1Uni\`);`;

const st5 = `CREATE TABLE \`t2\` (
const st6 = `CREATE TABLE \`t2\` (
\`t2Id\` integer PRIMARY KEY AUTOINCREMENT NOT NULL
);
`;

const st6 = `CREATE TABLE \`t3\` (
const st7 = `CREATE TABLE \`t3\` (
\`t3Id1\` integer,
\`t3Id2\` integer,
PRIMARY KEY(\`t3Id1\`, \`t3Id2\`)
);
`;

expect(sqlStatements).toStrictEqual([st1, st2, st3, st4, st5, st6]);
expect(sqlStatements).toStrictEqual([st1, st2, st3, st4, st5, st6, st7]);
});

0 comments on commit 38d1af3

Please sign in to comment.