44 "database/sql"
55 "fmt"
66 "path/filepath"
7+ "strings"
78 "testing"
89
910 "github.com/golang-migrate/migrate/v4"
@@ -667,7 +668,7 @@ func TestMigrationConfigConsistency(t *testing.T) {
667668 seenVersions := make (map [int ]string )
668669 seenSchemaVersions := make (map [int ]string )
669670
670- for _ , m := range migrations {
671+ for i , m := range migrations {
671672 // 1. Verify no duplicate global versions.
672673 if existing , ok := seenVersions [m .Version ]; ok {
673674 t .Fatalf ("duplicate global version %d: %q and %q" ,
@@ -680,20 +681,29 @@ func TestMigrationConfigConsistency(t *testing.T) {
680681 // and no two config entries claim the same schema version
681682 // with different file prefixes.
682683 prevSchema := 0
683- if m . Version > 1 {
684- prevSchema = migrations [m . Version - 2 ].SchemaVersion
684+ if i > 0 {
685+ prevSchema = migrations [i - 1 ].SchemaVersion
685686 }
686687
688+ require .GreaterOrEqual (t , m .SchemaVersion , prevSchema ,
689+ "migration %q regresses schema version from %d to %d" ,
690+ m .Name , prevSchema , m .SchemaVersion )
691+
687692 // A migration advances the schema if its SchemaVersion is
688693 // higher than the previous migration's SchemaVersion.
689694 if m .SchemaVersion > prevSchema {
690- _ , hasFile := fileSchemaVersions [m .SchemaVersion ]
695+ fileName , hasFile := fileSchemaVersions [m .SchemaVersion ]
691696 require .True (t , hasFile ,
692697 "migration %q (version %d) declares " +
693698 "SchemaVersion=%d but no %06d_*.up.sql" +
694699 " file exists in the embedded FS" ,
695700 m .Name , m .Version , m .SchemaVersion ,
696701 m .SchemaVersion )
702+ require .Equal (t , strings .TrimSuffix (fileName , ".up.sql" ),
703+ m .Name , "migration %q (version %d) has " +
704+ "SchemaVersion=%d but its name does not " +
705+ "match embedded file %q" ,
706+ m .Name , m .Version , m .SchemaVersion , fileName )
697707
698708 if existing , ok := seenSchemaVersions [m .SchemaVersion ]; ok {
699709 t .Fatalf ("duplicate schema version %d: " +
0 commit comments