This repository has been archived on 2026-11-21. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
encapi/internal/database/migrate_test.go
T
unkin-agent 519088b9ba
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
Add restore-from-testing endpoint using a recorded previous environment
2026-11-08 17:28:52 +11:00

38 lines
1.0 KiB
Go

package database
import (
"context"
"io/fs"
"testing"
"git.unkin.net/unkin/encapi/migrations"
)
// encapi's schema predates schema_migrations, so the first start after this
// change re-applies 0001_init against a database that already has the tables.
// The file is IF NOT EXISTS-guarded for exactly that: the run must succeed and
// leave only the tracking rows behind.
func TestMigrateAdoptsAnExistingSchema(t *testing.T) {
requireDB(t)
ctx := context.Background()
if _, err := testDB.Pool.Exec(ctx, `DROP TABLE schema_migrations`); err != nil {
t.Fatalf("drop schema_migrations: %v", err)
}
db, err := New(ctx, testDSN, nil)
if err != nil {
t.Fatalf("migrate over an existing schema: %v", err)
}
defer db.Close()
var n int
if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM schema_migrations`).Scan(&n); err != nil {
t.Fatalf("count schema_migrations: %v", err)
}
files, _ := fs.Glob(migrations.FS, "*.sql")
if n != len(files) {
t.Fatalf("schema_migrations rows = %d, want %d", n, len(files))
}
}