From 29ae857b5ca13fa12f1d07d4a30597ff9ce28767 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 31 May 2026 21:34:44 +0200 Subject: Add UserVersion() This function gets the user_version SQLite3 field describing which migration was the last executed one. A test checking this before applying any migrations and after each migration is included. --- database_test.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'database_test.go') diff --git a/database_test.go b/database_test.go index b412af0..901db42 100644 --- a/database_test.go +++ b/database_test.go @@ -29,21 +29,21 @@ func TestMigrationContent(t *testing.T) { if !strings.HasPrefix(migration, expectation) { t.Fatalf("Expected '%s' at start of '%s'.", expectation, path) } - - n := strings.Count(migration, `user_version`) - if n != 1 { - t.Fatalf( - "'user_version' was mentioned more than once in %s. "+ - "This is likely not intended.", - path, - ) - } } } func TestMigrations(t *testing.T) { db, _ := emptyDB(t) + userVersion, err := UserVersion(db) + if err != nil { + t.Fatalf("Failed to get initial user_version: %v", err) + } + + if userVersion != 0 { + t.Fatalf("Expected user_version 0 but got %d.", userVersion) + } + for i := range migrations { path := migrationPath(i) t.Logf("Test execution of %s", path) @@ -52,5 +52,20 @@ func TestMigrations(t *testing.T) { if err != nil { t.Fatalf("Failed to execute migration %s: %v", path, err) } + + userVersion, err = UserVersion(db) + if err != nil { + t.Fatalf("Failed to get user_version after migration: %v", err) + } + + expected := i + 1 + if userVersion != expected { + t.Fatalf( + "Expected user_version %d after running %s but got %d.", + expected, + path, + userVersion, + ) + } } } -- cgit v1.3.1