Skip to content

Commit fdad45b

Browse files
[sqlite]: isssue + fk introspect bugs fixed
1 parent f3e95bd commit fdad45b

File tree

6 files changed

+70
-29
lines changed

6 files changed

+70
-29
lines changed

drizzle-kit/src/dialects/sqlite/convertor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ const createTable = convertor('create_table', (st) => {
9191
onUpdate,
9292
} = referenceData[i];
9393

94-
const onDeleteStatement = onDelete !== 'NO ACTION' ? ` ON DELETE ${onDelete}` : '';
95-
const onUpdateStatement = onUpdate !== 'NO ACTION' ? ` ON UPDATE ${onUpdate}` : '';
94+
const onDeleteStatement = onDelete !== 'no action' ? ` ON DELETE ${onDelete}` : '';
95+
const onUpdateStatement = onUpdate !== 'no action' ? ` ON UPDATE ${onUpdate}` : '';
9696
const fromColumnsString = columns.map((it) => `\`${it}\``).join(',');
9797
const toColumnsString = columnsTo.map((it) => `\`${it}\``).join(',');
9898

drizzle-kit/src/dialects/sqlite/drizzle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ export const fromDrizzleSchema = (
110110
const fks = tableConfigs.map((it) => {
111111
return it.config.foreignKeys.map((fk) => {
112112
const tableFrom = it.config.name;
113-
const onDelete = fk.onDelete ?? 'NO ACTION';
114-
const onUpdate = fk.onUpdate ?? 'NO ACTION';
113+
const onDelete = fk.onDelete ?? 'no action';
114+
const onUpdate = fk.onUpdate ?? 'no action';
115115
const reference = fk.reference();
116116

117117
const referenceFT = reference.foreignTable;

drizzle-kit/src/dialects/sqlite/introspect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,8 @@ export const fromDatabase = async (
535535
columns: columnsFrom,
536536
columnsTo,
537537
nameExplicit: true,
538-
onDelete: fk.onDelete ?? 'NO ACTION',
539-
onUpdate: fk.onUpdate ?? 'NO ACTION',
538+
onDelete: fk.onDelete?.toLowerCase() ?? 'no action',
539+
onUpdate: fk.onUpdate?.toLowerCase() ?? 'no action',
540540
});
541541
}
542542

drizzle-kit/src/dialects/sqlite/typescript.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const ddlToTypeScript = (
9090
if (it.entityType === 'tables') imports.add('sqliteTable');
9191
if (it.entityType === 'fks') {
9292
imports.add('foreignKey');
93-
if (it.columns.length > 1 || isCyclic(it) || isSelf(it)) imports.add('AnySQLiteColumn');
93+
if (it.columns.length > 1 || isCyclic(it) || isSelf(it)) imports.add('type AnySQLiteColumn');
9494
}
9595
}
9696

@@ -116,9 +116,9 @@ export const ddlToTypeScript = (
116116
statement += createTableColumns(columns, fks, pk, casing);
117117
statement += '}';
118118

119-
// more than 2 fields or self reference or cyclic
119+
// more than 2 fields
120120
const filteredFKs = fks.filter((it) => {
121-
return it.columns.length > 1 || isSelf(it) || isCyclic(it);
121+
return it.columns.length > 1;
122122
});
123123

124124
if (
@@ -278,26 +278,21 @@ const createTableColumns = (
278278
const references = fks.filter((fk) => fk.columns.length === 1 && fk.columns[0] === it.name);
279279

280280
for (const fk of references) {
281-
statement += `.references(() => ${withCasing(fk.tableTo, casing)}.${withCasing(fk.columnsTo[0], casing)})`;
281+
const typeSuffix = isCyclic(fk) ? ': AnySQLiteColumn' : '';
282282

283-
const onDelete = fk.onDelete && fk.onDelete !== 'no action' ? fk.onDelete : null;
284-
const onUpdate = fk.onUpdate && fk.onUpdate !== 'no action' ? fk.onUpdate : null;
285-
const params = { onDelete, onUpdate };
283+
statement += `.references(()${typeSuffix} => ${withCasing(fk.tableTo, casing)}.${
284+
withCasing(fk.columnsTo[0], casing)
285+
}`;
286286

287-
const typeSuffix = isCyclic(fk) ? ': AnySQLiteColumn' : '';
287+
const onDelete = (fk.onDelete && fk.onDelete !== 'no action') ? fk.onDelete : null;
288+
const onUpdate = (fk.onUpdate && fk.onUpdate !== 'no action') ? fk.onUpdate : null;
289+
const params = { onDelete, onUpdate };
288290

289291
const paramsStr = objToStatement2(params);
290292
if (paramsStr) {
291-
statement += `.references(()${typeSuffix} => ${withCasing(fk.tableTo, casing)}.${
292-
withCasing(fk.columnsTo[0], casing)
293-
}, ${paramsStr} )`;
293+
statement += `, ${paramsStr} )`;
294294
} else {
295-
statement += `.references(()${typeSuffix} => ${
296-
withCasing(
297-
fk.tableTo,
298-
casing,
299-
)
300-
}.${withCasing(fk.columnsTo[0], casing)})`;
295+
statement += `)`;
301296
}
302297
}
303298
statement += ',\n';
@@ -412,7 +407,7 @@ const createTableFKs = (fks: ForeignKey[], casing: Casing): string => {
412407
fks.forEach((it) => {
413408
const isSelf = it.tableTo === it.table;
414409
const tableTo = isSelf ? 'table' : `${withCasing(it.tableTo, casing)}`;
415-
statement += `\t\t${withCasing(it.name, casing)}: foreignKey(() => ({\n`;
410+
statement += `\n\t\tforeignKey({\n`;
416411
statement += `\t\t\tcolumns: [${
417412
it.columns
418413
.map((i) => `table.${withCasing(i, casing)}`)
@@ -424,7 +419,7 @@ const createTableFKs = (fks: ForeignKey[], casing: Casing): string => {
424419
.join(', ')
425420
}],\n`;
426421
statement += `\t\t\tname: "${it.name}"\n`;
427-
statement += `\t\t}))`;
422+
statement += `\t\t})`;
428423

429424
statement += it.onUpdate && it.onUpdate !== 'no action'
430425
? `.onUpdate("${it.onUpdate}")`

drizzle-kit/tests/sqlite/pull.test.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import Database from 'better-sqlite3';
22
import { SQL, sql } from 'drizzle-orm';
3-
import { AnySQLiteColumn, check, int, integer, sqliteTable, sqliteView, text } from 'drizzle-orm/sqlite-core';
3+
import {
4+
AnySQLiteColumn,
5+
check,
6+
foreignKey,
7+
int,
8+
integer,
9+
sqliteTable,
10+
sqliteView,
11+
text,
12+
} from 'drizzle-orm/sqlite-core';
413
import * as fs from 'fs';
514
import { interimToDDL } from 'src/dialects/sqlite/ddl';
615
import { fromDatabaseForDrizzle } from 'src/dialects/sqlite/introspect';
@@ -32,7 +41,6 @@ test('introspect tables with fk constraint', async () => {
3241
});
3342

3443
// https://github.com/drizzle-team/drizzle-orm/issues/4247
35-
// TODO AnySQLiteColumn should be prefixed by `type` after introspection
3644
test('introspect table with self reference', async () => {
3745
const sqlite = new Database(':memory:');
3846

@@ -49,6 +57,44 @@ test('introspect table with self reference', async () => {
4957
expect(sqlStatements).toStrictEqual([]);
5058
});
5159

60+
test('introspect table fk on multiple columns', async () => {
61+
const sqlite = new Database(':memory:');
62+
63+
const users = sqliteTable('users', {
64+
id: integer(),
65+
name: text(),
66+
invited_id: integer(),
67+
});
68+
69+
const ref = sqliteTable('ref', {
70+
id: integer(),
71+
name: text(),
72+
invited_id: integer(),
73+
}, (t) => [foreignKey({ columns: [t.name, t.invited_id], foreignColumns: [users.name, users.invited_id] })]);
74+
75+
const schema = { users, ref };
76+
77+
const { statements, sqlStatements } = await diffAfterPull(sqlite, schema, 'composite-fk');
78+
79+
expect(sqlStatements).toStrictEqual([]);
80+
});
81+
82+
test('composite fk introspect self ref', async () => {
83+
const sqlite = new Database(':memory:');
84+
85+
const users = sqliteTable('users', {
86+
id: integer(),
87+
name: text(),
88+
invited_id: integer(),
89+
}, (t) => [foreignKey({ columns: [t.name, t.invited_id], foreignColumns: [t.name, t.invited_id] })]);
90+
91+
const schema = { users };
92+
93+
const { statements, sqlStatements } = await diffAfterPull(sqlite, schema, 'composite-fk-self-ref');
94+
95+
expect(sqlStatements).toStrictEqual([]);
96+
});
97+
5298
test('generated always column: link to another column', async () => {
5399
const sqlite = new Database(':memory:');
54100

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,8 +1811,8 @@ test('index #1', async () => {
18111811
'CREATE UNIQUE INDEX `index2` ON `table1` (`col1`,`col2`);',
18121812
'CREATE INDEX `index3` ON `table1` (`col1`);',
18131813
'CREATE INDEX `index4` ON `table1` (`col1`,`col2`);',
1814-
'CREATE INDEX `index5` ON `table1` (`col1` asc);',
1815-
'CREATE INDEX `index6` ON `table1` (`col1` asc,`col2` desc);',
1814+
'CREATE INDEX `index5` ON `table1` ("col1" asc);',
1815+
'CREATE INDEX `index6` ON `table1` ("col1" asc,"col2" desc);',
18161816
];
18171817
expect(st1).toStrictEqual(expectedSt1);
18181818
expect(pst1).toStrictEqual(expectedSt1);

0 commit comments

Comments
 (0)