11import { getTableName , is , SQL } from 'drizzle-orm' ;
22import { Relations } from 'drizzle-orm/_relations' ;
3- import type { AnySQLiteColumn , AnySQLiteTable } from 'drizzle-orm/sqlite-core' ;
3+ import type { AnySQLiteColumn , AnySQLiteTable , UpdateDeleteAction } from 'drizzle-orm/sqlite-core' ;
44import {
55 getTableConfig ,
66 getViewConfig ,
@@ -10,6 +10,7 @@ import {
1010 SQLiteTimestamp ,
1111 SQLiteView ,
1212} from 'drizzle-orm/sqlite-core' ;
13+ import { assertUnreachable } from 'src/utils' ;
1314import { safeRegister } from 'src/utils/utils-node' ;
1415import type { CasingType } from '../../cli/validations/common' ;
1516import { getColumnCasing , sqlToStr } from '../drizzle' ;
@@ -27,6 +28,16 @@ import type {
2728} from './ddl' ;
2829import { Int , nameForForeignKey , nameForPk , nameForUnique , typeFor } from './grammar' ;
2930
31+ export const transformOnUpdateDelete = ( on : UpdateDeleteAction ) : ForeignKey [ 'onUpdate' ] => {
32+ if ( on === 'no action' ) return 'NO ACTION' ;
33+ if ( on === 'cascade' ) return 'CASCADE' ;
34+ if ( on === 'restrict' ) return 'RESTRICT' ;
35+ if ( on === 'set default' ) return 'SET DEFAULT' ;
36+ if ( on === 'set null' ) return 'SET NULL' ;
37+
38+ assertUnreachable ( on ) ;
39+ } ;
40+
3041export const fromDrizzleSchema = (
3142 dTables : AnySQLiteTable [ ] ,
3243 dViews : SQLiteView [ ] ,
@@ -110,8 +121,8 @@ export const fromDrizzleSchema = (
110121 const fks = tableConfigs . map ( ( it ) => {
111122 return it . config . foreignKeys . map ( ( fk ) => {
112123 const tableFrom = it . config . name ;
113- const onDelete = fk . onDelete ?? 'no action' ;
114- const onUpdate = fk . onUpdate ?? 'no action' ;
124+ const onDelete = fk . onDelete ;
125+ const onUpdate = fk . onUpdate ;
115126 const reference = fk . reference ( ) ;
116127
117128 const referenceFT = reference . foreignTable ;
@@ -131,8 +142,8 @@ export const fromDrizzleSchema = (
131142 tableTo,
132143 columns : columnsFrom ,
133144 columnsTo,
134- onDelete,
135- onUpdate,
145+ onDelete : transformOnUpdateDelete ( onDelete ?? 'no action' ) ,
146+ onUpdate : transformOnUpdateDelete ( onUpdate ?? 'no action' ) ,
136147 nameExplicit : fk . isNameExplicit ( ) ,
137148 } satisfies ForeignKey ;
138149 } ) ;
0 commit comments