Skip to content

Commit 5c41eb0

Browse files
committed
Test for too old.
1 parent 712abf1 commit 5c41eb0

2 files changed

Lines changed: 79 additions & 2 deletions

File tree

migration.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var ErrMigrationTableNameInvalid = errors.New("invalid migration table name")
4040

4141
// ErrMigrationsTooOld signalizes that the migrations to be applied are older than the migrations that are already
4242
// present in the database. This error can occur when an older version of the application is started using a database
43-
// that was used already by a newer version of the appliction.
43+
// that was used already by a newer version of the application.
4444
var ErrMigrationsTooOld = errors.New("migrations too old")
4545

4646
// Dialect is an interface describing the functionalities needed to manage migrations inside a database.
@@ -217,7 +217,7 @@ func (m *Morpher) applyMigrations(db *sql.DB, lastMigration string) error {
217217

218218
// even if we are sure to catch all possibilities, we use this as a safeguard that also with later
219219
// modifications, if a successful commit cannot be done, at least the rollback is executed freeing
220-
// allocated ressources of the transaction.
220+
// allocated resources of the transaction.
221221
defer func() { _ = tx.Rollback() }()
222222

223223
if err := migration.Migrate(tx); err != nil {
@@ -255,6 +255,8 @@ func (m *Morpher) checkAppliedMigrations(appliedMigrations []string) error {
255255
}
256256

257257
if len(m.Migrations) < len(appliedMigrations) {
258+
// it is impossible to have a migration newer than the one already applied
259+
// without having at least the same amount of previous migrations
258260
return ErrMigrationsUnrelated
259261
}
260262

migration_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func prepareDB() (string, error) {
3333
return result, nil
3434
}
3535

36+
// TestMigration tests the happy flow.
3637
func 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

Comments
 (0)