@@ -33,6 +33,7 @@ func prepareDB() (string, error) {
3333 return result , nil
3434}
3535
36+ // TestMigration tests the happy flow.
3637func TestMigration (t * testing.T ) {
3738 dbFile , dbFileErr := prepareDB ()
3839
@@ -62,3 +63,77 @@ func TestMigration(t *testing.T) {
6263 t .Errorf ("Migrations could not be run: %v" , runErr )
6364 }
6465}
66+
67+ // TestMigration tests what happens, if the applied migrations are too old.
68+ func TestMigrationTooOld (t * testing.T ) {
69+ dbFile , dbFileErr := prepareDB ()
70+
71+ if dbFileErr != nil {
72+ t .Errorf ("DB file could not be created: %v" , dbFileErr )
73+ } else {
74+ defer func () { _ = os .Remove (dbFile ) }()
75+ }
76+
77+ db , dbErr := sql .Open ("sqlite" , dbFile )
78+
79+ if dbErr != nil {
80+ t .Errorf ("DB file could not be created: %v" , dbErr )
81+ } else {
82+ defer func () { _ = db .Close () }()
83+ }
84+
85+ migrationsDir , migrationsDirErr := fs .Sub (testMigrationsDir , "testData" )
86+
87+ assert .Nil (t , migrationsDirErr , "migrations directory could not be opened" )
88+
89+ runErr := Run (db ,
90+ WithDialect (DialectSQLite ()),
91+ WithMigrationsFromFS (migrationsDir .(fs.ReadDirFS )))
92+
93+ if runErr != nil {
94+ t .Errorf ("preparation migrations could not be run: %v" , runErr )
95+ }
96+
97+ runErr = Run (db ,
98+ WithDialect (DialectSQLite ()),
99+ WithMigrationFromFileFS ("01_base_table.sql" , migrationsDir ))
100+
101+ assert .ErrorIs (t , runErr , ErrMigrationsTooOld , "migrations did not give expected error" )
102+ }
103+
104+ // TestMigration tests what happens, if the applied migrations are unrelated to existing ones.
105+ func TestMigrationUnrelated (t * testing.T ) {
106+ dbFile , dbFileErr := prepareDB ()
107+
108+ if dbFileErr != nil {
109+ t .Errorf ("DB file could not be created: %v" , dbFileErr )
110+ } else {
111+ defer func () { _ = os .Remove (dbFile ) }()
112+ }
113+
114+ db , dbErr := sql .Open ("sqlite" , dbFile )
115+
116+ if dbErr != nil {
117+ t .Errorf ("DB file could not be created: %v" , dbErr )
118+ } else {
119+ defer func () { _ = db .Close () }()
120+ }
121+
122+ migrationsDir , migrationsDirErr := fs .Sub (testMigrationsDir , "testData" )
123+
124+ assert .Nil (t , migrationsDirErr , "migrations directory could not be opened" )
125+
126+ runErr := Run (db ,
127+ WithDialect (DialectSQLite ()),
128+ WithMigrationFromFileFS ("01_base_table.sql" , migrationsDir ))
129+
130+ if runErr != nil {
131+ t .Errorf ("preparation migrations could not be run: %v" , runErr )
132+ }
133+
134+ runErr = Run (db ,
135+ WithDialect (DialectSQLite ()),
136+ WithMigrationFromFileFS ("02_addon_table.sql" , migrationsDir ))
137+
138+ assert .ErrorIs (t , runErr , ErrMigrationsUnrelated , "migrations did not give expected error" )
139+ }
0 commit comments