@@ -146,8 +146,8 @@ export class Migrator {
146
146
* }
147
147
* ```
148
148
*/
149
- async migrateToLatest ( ) : Promise < MigrationResultSet > {
150
- return this . #migrate( ( ) => ( { direction : 'Up' , step : Infinity } ) )
149
+ async migrateToLatest ( options ?: MigrateOptions ) : Promise < MigrationResultSet > {
150
+ return this . #migrate( ( ) => ( { direction : 'Up' , step : Infinity } ) , options )
151
151
}
152
152
153
153
/**
@@ -203,6 +203,7 @@ export class Migrator {
203
203
*/
204
204
async migrateTo (
205
205
targetMigrationName : string | NoMigrations ,
206
+ options ?: MigrateOptions ,
206
207
) : Promise < MigrationResultSet > {
207
208
return this . #migrate(
208
209
( {
@@ -226,6 +227,7 @@ export class Migrator {
226
227
const executedIndex = executedMigrations . indexOf (
227
228
targetMigrationName as string ,
228
229
)
230
+
229
231
const pendingIndex = pendingMigrations . findIndex (
230
232
( m ) => m . name === ( targetMigrationName as string ) ,
231
233
)
@@ -235,14 +237,17 @@ export class Migrator {
235
237
direction : 'Down' ,
236
238
step : executedMigrations . length - executedIndex - 1 ,
237
239
}
238
- } else if ( pendingIndex !== - 1 ) {
240
+ }
241
+
242
+ if ( pendingIndex !== - 1 ) {
239
243
return { direction : 'Up' , step : pendingIndex + 1 }
240
- } else {
241
- throw new Error (
242
- `migration "${ targetMigrationName } " isn't executed or pending` ,
243
- )
244
244
}
245
+
246
+ throw new Error (
247
+ `migration "${ targetMigrationName } " isn't executed or pending` ,
248
+ )
245
249
} ,
250
+ options ,
246
251
)
247
252
}
248
253
@@ -274,8 +279,8 @@ export class Migrator {
274
279
* await migrator.migrateUp()
275
280
* ```
276
281
*/
277
- async migrateUp ( ) : Promise < MigrationResultSet > {
278
- return this . #migrate( ( ) => ( { direction : 'Up' , step : 1 } ) )
282
+ async migrateUp ( options ?: MigrateOptions ) : Promise < MigrationResultSet > {
283
+ return this . #migrate( ( ) => ( { direction : 'Up' , step : 1 } ) , options )
279
284
}
280
285
281
286
/**
@@ -306,19 +311,20 @@ export class Migrator {
306
311
* await migrator.migrateDown()
307
312
* ```
308
313
*/
309
- async migrateDown ( ) : Promise < MigrationResultSet > {
310
- return this . #migrate( ( ) => ( { direction : 'Down' , step : 1 } ) )
314
+ async migrateDown ( options ?: MigrateOptions ) : Promise < MigrationResultSet > {
315
+ return this . #migrate( ( ) => ( { direction : 'Down' , step : 1 } ) , options )
311
316
}
312
317
313
318
async #migrate(
314
319
getMigrationDirectionAndStep : ( state : MigrationState ) => {
315
320
direction : MigrationDirection
316
321
step : number
317
322
} ,
323
+ options : MigrateOptions | undefined ,
318
324
) : Promise < MigrationResultSet > {
319
325
try {
320
326
await this . #ensureMigrationTablesExists( )
321
- return await this . #runMigrations( getMigrationDirectionAndStep )
327
+ return await this . #runMigrations( getMigrationDirectionAndStep , options )
322
328
} catch ( error ) {
323
329
if ( error instanceof MigrationResultSetError ) {
324
330
return error . resultSet
@@ -489,6 +495,7 @@ export class Migrator {
489
495
direction : MigrationDirection
490
496
step : number
491
497
} ,
498
+ options : MigrateOptions | undefined ,
492
499
) : Promise < MigrationResultSet > {
493
500
const adapter = this . #props. db . getExecutor ( ) . adapter
494
501
@@ -526,11 +533,14 @@ export class Migrator {
526
533
}
527
534
}
528
535
529
- if ( adapter . supportsTransactionalDdl && ! this . #props. disableTransactions ) {
530
- return this . #props. db . transaction ( ) . execute ( run )
531
- } else {
536
+ const disableTransaction =
537
+ options ?. disableTransactions ?? this . #props. disableTransactions
538
+
539
+ if ( ! adapter . supportsTransactionalDdl || disableTransaction ) {
532
540
return this . #props. db . connection ( ) . execute ( run )
533
541
}
542
+
543
+ return this . #props. db . transaction ( ) . execute ( run )
534
544
}
535
545
536
546
async #getState( db : Kysely < any > ) : Promise < MigrationState > {
@@ -752,7 +762,18 @@ export class Migrator {
752
762
}
753
763
}
754
764
755
- export interface MigratorProps {
765
+ export interface MigrateOptions {
766
+ /**
767
+ * When `true`, don't run migrations in transactions even if the dialect supports transactional DDL.
768
+ *
769
+ * Default is `false`.
770
+ *
771
+ * This is useful when some migrations include queries that would fail otherwise.
772
+ */
773
+ readonly disableTransactions ?: boolean
774
+ }
775
+
776
+ export interface MigratorProps extends MigrateOptions {
756
777
readonly db : Kysely < any >
757
778
readonly provider : MigrationProvider
758
779
@@ -825,15 +846,6 @@ export interface MigratorProps {
825
846
* Default is `name0.localeCompare(name1)`.
826
847
*/
827
848
readonly nameComparator ?: ( name0 : string , name1 : string ) => number
828
-
829
- /**
830
- * When `true`, don't run migrations in transactions even if the dialect supports transactional DDL.
831
- *
832
- * Default is `false`.
833
- *
834
- * This is useful when some migrations include queries that would fail otherwise.
835
- */
836
- readonly disableTransactions ?: boolean
837
849
}
838
850
839
851
/**
0 commit comments