Implement local DB migrations and archived configuration lifecycle
This commit is contained in:
72
internal/localdb/local_migrations_test.go
Normal file
72
internal/localdb/local_migrations_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package localdb
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRunLocalMigrationsBackfillsExistingConfigurations(t *testing.T) {
|
||||
dbPath := filepath.Join(t.TempDir(), "legacy_local.db")
|
||||
|
||||
local, err := New(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("open localdb: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = local.Close() })
|
||||
|
||||
cfg := &LocalConfiguration{
|
||||
UUID: "legacy-cfg",
|
||||
Name: "Legacy",
|
||||
Items: LocalConfigItems{},
|
||||
SyncStatus: "pending",
|
||||
OriginalUsername: "tester",
|
||||
IsActive: true,
|
||||
}
|
||||
if err := local.SaveConfiguration(cfg); err != nil {
|
||||
t.Fatalf("save seed config: %v", err)
|
||||
}
|
||||
if err := local.DB().Where("configuration_uuid = ?", "legacy-cfg").Delete(&LocalConfigurationVersion{}).Error; err != nil {
|
||||
t.Fatalf("delete seed versions: %v", err)
|
||||
}
|
||||
if err := local.DB().Model(&LocalConfiguration{}).
|
||||
Where("uuid = ?", "legacy-cfg").
|
||||
Update("current_version_id", nil).Error; err != nil {
|
||||
t.Fatalf("clear current_version_id: %v", err)
|
||||
}
|
||||
if err := local.DB().Where("1=1").Delete(&LocalSchemaMigration{}).Error; err != nil {
|
||||
t.Fatalf("clear migration records: %v", err)
|
||||
}
|
||||
|
||||
if err := runLocalMigrations(local.DB()); err != nil {
|
||||
t.Fatalf("run local migrations manually: %v", err)
|
||||
}
|
||||
|
||||
migratedCfg, err := local.GetConfigurationByUUID("legacy-cfg")
|
||||
if err != nil {
|
||||
t.Fatalf("get migrated config: %v", err)
|
||||
}
|
||||
if migratedCfg.CurrentVersionID == nil || *migratedCfg.CurrentVersionID == "" {
|
||||
t.Fatalf("expected current_version_id after migration")
|
||||
}
|
||||
if !migratedCfg.IsActive {
|
||||
t.Fatalf("expected migrated config to be active")
|
||||
}
|
||||
|
||||
var versionCount int64
|
||||
if err := local.DB().Model(&LocalConfigurationVersion{}).
|
||||
Where("configuration_uuid = ?", "legacy-cfg").
|
||||
Count(&versionCount).Error; err != nil {
|
||||
t.Fatalf("count versions: %v", err)
|
||||
}
|
||||
if versionCount != 1 {
|
||||
t.Fatalf("expected 1 backfilled version, got %d", versionCount)
|
||||
}
|
||||
|
||||
var migrationCount int64
|
||||
if err := local.DB().Model(&LocalSchemaMigration{}).Count(&migrationCount).Error; err != nil {
|
||||
t.Fatalf("count local migrations: %v", err)
|
||||
}
|
||||
if migrationCount == 0 {
|
||||
t.Fatalf("expected local migrations to be recorded")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user