@@ -488,6 +488,123 @@ for (const dialect of DIALECTS) {
488488 expect ( executedDownMethods2 ) . to . eql ( [ 'migration4' , 'migration3' ] )
489489 } )
490490
491+ it ( 'should migrate up to a specific migration with direction enforced' , async ( ) => {
492+ const { migrator, executedUpMethods } = createMigrations ( [
493+ 'migration1' ,
494+ 'migration2' ,
495+ 'migration3' ,
496+ ] )
497+
498+ const { results } = await migrator . migrateTo ( 'migration2' , {
499+ direction : 'Up' ,
500+ } )
501+
502+ expect ( results ) . to . eql ( [
503+ { migrationName : 'migration1' , direction : 'Up' , status : 'Success' } ,
504+ { migrationName : 'migration2' , direction : 'Up' , status : 'Success' } ,
505+ ] )
506+
507+ expect ( executedUpMethods ) . to . eql ( [ 'migration1' , 'migration2' ] )
508+ } )
509+
510+ it ( 'should return an error when migrating up to an already executed migration' , async ( ) => {
511+ const { migrator, executedUpMethods, executedDownMethods } =
512+ createMigrations ( [ 'migration1' , 'migration2' , 'migration3' ] )
513+
514+ await migrator . migrateToLatest ( )
515+
516+ const { error } = await migrator . migrateTo ( 'migration2' , {
517+ direction : 'Up' ,
518+ } )
519+
520+ expect ( error ) . to . be . an . instanceOf ( Error )
521+ expect ( getMessage ( error ) ) . to . eql (
522+ `migration "migration2" is already executed; can't migrate up to it` ,
523+ )
524+
525+ expect ( executedUpMethods ) . to . eql ( [
526+ 'migration1' ,
527+ 'migration2' ,
528+ 'migration3' ,
529+ ] )
530+ expect ( executedDownMethods ) . to . eql ( [ ] )
531+ } )
532+
533+ it ( 'should migrate down to a specific migration with direction enforced' , async ( ) => {
534+ const { migrator, executedDownMethods } = createMigrations ( [
535+ 'migration1' ,
536+ 'migration2' ,
537+ 'migration3' ,
538+ ] )
539+
540+ await migrator . migrateToLatest ( )
541+
542+ const { results } = await migrator . migrateTo ( 'migration1' , {
543+ direction : 'Down' ,
544+ } )
545+
546+ expect ( results ) . to . eql ( [
547+ { migrationName : 'migration3' , direction : 'Down' , status : 'Success' } ,
548+ { migrationName : 'migration2' , direction : 'Down' , status : 'Success' } ,
549+ ] )
550+
551+ expect ( executedDownMethods ) . to . eql ( [ 'migration3' , 'migration2' ] )
552+ } )
553+
554+ it ( 'should return an error when migrating down to a pending migration' , async ( ) => {
555+ const { migrator, executedUpMethods, executedDownMethods } =
556+ createMigrations ( [ 'migration1' , 'migration2' , 'migration3' ] )
557+
558+ const { error } = await migrator . migrateTo ( 'migration2' , {
559+ direction : 'Down' ,
560+ } )
561+
562+ expect ( error ) . to . be . an . instanceOf ( Error )
563+ expect ( getMessage ( error ) ) . to . eql (
564+ `migration "migration2" isn't executed; can't migrate down to it` ,
565+ )
566+
567+ expect ( executedUpMethods ) . to . eql ( [ ] )
568+ expect ( executedDownMethods ) . to . eql ( [ ] )
569+ } )
570+
571+ it ( 'should migrate all the way down with direction enforced' , async ( ) => {
572+ const { migrator, executedDownMethods } = createMigrations ( [
573+ 'migration1' ,
574+ 'migration2' ,
575+ ] )
576+
577+ await migrator . migrateToLatest ( )
578+
579+ const { results } = await migrator . migrateTo ( NO_MIGRATIONS , {
580+ direction : 'Down' ,
581+ } )
582+
583+ expect ( results ) . to . eql ( [
584+ { migrationName : 'migration2' , direction : 'Down' , status : 'Success' } ,
585+ { migrationName : 'migration1' , direction : 'Down' , status : 'Success' } ,
586+ ] )
587+
588+ expect ( executedDownMethods ) . to . eql ( [ 'migration2' , 'migration1' ] )
589+ } )
590+
591+ it ( 'should return an error when migrating up to NO_MIGRATIONS' , async ( ) => {
592+ const { migrator, executedUpMethods, executedDownMethods } =
593+ createMigrations ( [ 'migration1' , 'migration2' ] )
594+
595+ await migrator . migrateToLatest ( )
596+
597+ const { error } = await migrator . migrateTo ( NO_MIGRATIONS , {
598+ direction : 'Up' ,
599+ } )
600+
601+ expect ( error ) . to . be . an . instanceOf ( Error )
602+ expect ( getMessage ( error ) ) . to . eql ( `can't migrate up to NO_MIGRATIONS` )
603+
604+ expect ( executedUpMethods ) . to . eql ( [ 'migration1' , 'migration2' ] )
605+ expect ( executedDownMethods ) . to . eql ( [ ] )
606+ } )
607+
491608 describe ( 'with allowUnorderedMigrations enabled' , ( ) => {
492609 it ( 'should migrate up to a specific migration' , async ( ) => {
493610 const { migrator : migrator1 , executedUpMethods : executedUpMethods1 } =
0 commit comments