@@ -435,11 +435,32 @@ impl AddMigrationOpts {
435435
436436 match ( self . timestamp , self . sequential , default_versioning) {
437437 ( true , false , _) | ( false , false , DefaultVersioning :: Timestamp ) => next_timestamp ( ) ,
438- ( false , true , _) | ( false , false , DefaultVersioning :: Sequential ) => {
439- next_sequential ( migrator) . unwrap_or_else ( || fmt_sequential ( 1 ) )
440- }
438+ ( false , true , _) | ( false , false , DefaultVersioning :: Sequential ) => fmt_sequential (
439+ migrator
440+ . migrations
441+ . last ( )
442+ . map_or ( 1 , |migration| migration. version + 1 ) ,
443+ ) ,
441444 ( false , false , DefaultVersioning :: Inferred ) => {
442- next_sequential ( migrator) . unwrap_or_else ( next_timestamp)
445+ migrator
446+ . migrations
447+ . rchunks ( 2 )
448+ . next ( )
449+ . and_then ( |migrations| {
450+ match migrations {
451+ [ previous, latest] => {
452+ // If the latest two versions differ by 1, infer sequential.
453+ ( latest. version - previous. version == 1 )
454+ . then_some ( latest. version + 1 )
455+ }
456+ [ latest] => {
457+ // If only one migration exists and its version is 0 or 1, infer sequential
458+ matches ! ( latest. version, 0 | 1 ) . then_some ( latest. version + 1 )
459+ }
460+ _ => unreachable ! ( ) ,
461+ }
462+ } )
463+ . map_or_else ( next_timestamp, fmt_sequential)
443464 }
444465 ( true , true , _) => unreachable ! ( "BUG: Clap should have rejected this case" ) ,
445466 }
@@ -450,28 +471,6 @@ fn next_timestamp() -> String {
450471 Utc :: now ( ) . format ( "%Y%m%d%H%M%S" ) . to_string ( )
451472}
452473
453- fn next_sequential ( migrator : & Migrator ) -> Option < String > {
454- let next_version = migrator
455- . migrations
456- . rchunks ( 2 )
457- . next ( )
458- . and_then ( |migrations| {
459- match migrations {
460- [ previous, latest] => {
461- // If the latest two versions differ by 1, infer sequential.
462- ( latest. version - previous. version == 1 ) . then_some ( latest. version + 1 )
463- }
464- [ latest] => {
465- // If only one migration exists and its version is 0 or 1, infer sequential
466- matches ! ( latest. version, 0 | 1 ) . then_some ( latest. version + 1 )
467- }
468- _ => unreachable ! ( ) ,
469- }
470- } ) ;
471-
472- next_version. map ( fmt_sequential)
473- }
474-
475474fn fmt_sequential ( version : i64 ) -> String {
476475 format ! ( "{version:04}" )
477476}
0 commit comments