ReviewOS

also looking at this

stacks/bun-query-builder

fix(schema): emit inline FOREIGN KEY on SQLite CREATE TABLE (#1019)

#1020
Merged glennmichael123 wants to merge fix/inline-foreign-keys into main
6 files +239 -10
packages/bun-query-builder/test/migrations.diff.test.tsmodified+25-0
Changes to packages/bun-query-builder/test/migrations.diff.test.ts
@@ -68,6 +68,31 @@ describe('migrations - diffing and hashing', () => {
6868 expect(sql.join('\n')).toContain('ALTER TABLE "projects" ADD CONSTRAINT')
6969 })
7070
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
7196 it('dialect specific types map as expected', () => {
7297 const planPg = buildMigrationPlan(baseModels as any, { dialect: 'postgres' })
7398 const sqlPg = generateSql(planPg)