also looking at this
fix(schema): emit inline FOREIGN KEY on SQLite CREATE TABLE (#1019)
#1020
6 files
+239
-10
| @@ -68,6 +68,31 @@ describe('migrations - diffing and hashing', () => { | ||
| 68 | 68 | expect(sql.join('\n')).toContain('ALTER TABLE "projects" ADD CONSTRAINT') |
| 69 | 69 | }) |
| 70 | 70 | |
| 71 | it('sqlite diff emits inline REFERENCES for new tables (FK rides on CREATE)', () => { | |
| 72 | const models2 = defineModels({ | |
| 73 | ...baseModels, | |
| 74 | Project: { | |
| 75 | name: 'Project', | |
| 76 | table: 'projects', | |
| 77 | primaryKey: 'id', | |
| 78 | attributes: { | |
| 79 | id: { validation: { rule: {} } }, | |
| 80 | user_id: { validation: { rule: {} } }, | |
| 81 | name: { validation: { rule: {} } }, | |
| 82 | }, | |
| 83 | }, | |
| 84 | } as const) | |
| 85 | const p1 = buildMigrationPlan(baseModels as any, { dialect: 'sqlite' }) | |
| 86 | const p2 = buildMigrationPlan(models2 as any, { dialect: 'sqlite' }) | |
| 87 | const joined = generateDiffSql(p1, p2).join('\n') | |
| 88 | ||
| 89 | expect(joined).toContain('CREATE TABLE') | |
| 90 | expect(joined).toMatch(/"user_id"[^,)]*REFERENCES\s+"users"\("id"\)/) | |
| 91 | // SQLite can't run ALTER TABLE ADD CONSTRAINT, so no separate | |
| 92 | // ALTER must be emitted for new tables. | |
| 93 | expect(joined).not.toContain('ADD CONSTRAINT') | |
| 94 | }) | |
| 95 | ||
| 71 | 96 | it('dialect specific types map as expected', () => { |
| 72 | 97 | const planPg = buildMigrationPlan(baseModels as any, { dialect: 'postgres' }) |
| 73 | 98 | const sqlPg = generateSql(planPg) |