| new file mode 100644 |
| index 000000000..d8771b1e4 |
| --- /dev/null |
| +++ b/internal/console/store_region_test.go |
| @@ -0,0 +1,187 @@ |
| +package console |
| + |
| +import ( |
| + "database/sql" |
| + "path/filepath" |
| + "testing" |
| + "time" |
| + |
| + _ "modernc.org/sqlite" |
| +) |
| + |
| +func TestAppRegionRoundTripAndDefaults(t *testing.T) { |
| + s := openTestStore(t) |
| + now := time.Now() |
| + if ok, err := s.CreateAppInRegion(OpsAccountID, "blog", "p@k.dev", "uk", "timezone", now); err != nil || !ok { |
| + t.Fatalf("CreateAppInRegion = %v %v", ok, err) |
| + } |
| + if r, src, err := s.AppRegion(OpsAccountID, "blog"); err != nil || r != "uk" || src != "timezone" { |
| + t.Fatalf("AppRegion = %q %q %v", r, src, err) |
| + } |
| + if ok, err := s.CreateAppIn(OpsAccountID, "old", "p@k.dev", now); err != nil || !ok { |
| + t.Fatalf("CreateAppIn = %v %v", ok, err) |
| + } |
| + if r, src, _ := s.AppRegion(OpsAccountID, "old"); r != "" || src != "" { |
| + t.Fatalf("CreateAppIn row = %q %q, want the home and no source", r, src) |
| + } |
| + if r, src, err := s.AppRegion(OpsAccountID, "missing"); err != nil || r != "" || src != "" { |
| + t.Fatalf("missing row = %q %q %v", r, src, err) |
| + } |
| + // A lost claim race writes nothing. |
| + if ok, _ := s.CreateAppInRegion(OpsAccountID, "blog", "x@k.dev", "au", "ip", now); ok { |
| + t.Fatal("a second claim of the same name succeeded") |
| + } |
| + if r, _, _ := s.AppRegion(OpsAccountID, "blog"); r != "uk" { |
| + t.Fatalf("a lost claim changed the region to %q", r) |
| + } |
| +} |
| + |
| +// The fixture runs NewServer, whose backfill creates the ops account row |
| +// (sqid "bab") that NonHomeAppRegions joins on. |
| +func TestNonHomeAppRegions(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + now := time.Now() |
| + if _, err := fx.st.CreateAppInRegion(OpsAccountID, "blog", "p@k.dev", "uk", "chosen", now); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := fx.st.CreateAppInRegion(OpsAccountID, "home", "p@k.dev", "", "default", now); err != nil { |
| + t.Fatal(err) |
| + } |
| + rows, err := fx.st.NonHomeAppRegions() |
| + if err != nil || len(rows) != 1 || rows[0].App != "blog" || rows[0].Region != "uk" || rows[0].Sqid != "bab" { |
| + t.Fatalf("NonHomeAppRegions = %+v %v", rows, err) |
| + } |
| +} |
| + |
| +// spec §5.2: a database from BEFORE migrateAppsKey whose rows already |
| +// carry an app region keeps it through the rebuild, a reopen, a rename and |
| +// a move to another account. An empty value would not prove the copy. |
| +func TestRegionValuesSurviveRebuildRenameAndMove(t *testing.T) { |
| + path := filepath.Join(t.TempDir(), "console.db") |
| + db, err := sql.Open("sqlite", path) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := db.Exec(`CREATE TABLE apps ( |
| + app TEXT PRIMARY KEY, created_by TEXT NOT NULL, created_at INTEGER NOT NULL, |
| + account_id INTEGER NOT NULL DEFAULT 1, deleted_at INTEGER NOT NULL DEFAULT 0, |
| + placement TEXT NOT NULL DEFAULT '', control_plane INTEGER NOT NULL DEFAULT 0, |
| + build_probe TEXT NOT NULL DEFAULT '', region TEXT NOT NULL DEFAULT '', |
| + region_source TEXT NOT NULL DEFAULT ''); |
| +INSERT INTO apps (app, created_by, created_at, region, region_source) VALUES ('legacy', 'p@k.dev', 1, 'uk', 'timezone');`); err != nil { |
| + t.Fatal(err) |
| + } |
| + db.Close() |
| + for i := 0; i < 2; i++ { // the rebuild, then a reopen of the rebuilt table |
| + s, err := Open(path) |
| + if err != nil { |
| + t.Fatalf("open %d: %v", i, err) |
| + } |
| + if r, src, err := s.AppRegion(OpsAccountID, "legacy"); err != nil || r != "uk" || src != "timezone" { |
| + t.Fatalf("open %d: legacy = %q %q %v, want uk/timezone carried", i, r, src, err) |
| + } |
| + s.Close() |
| + } |
| + s, err := Open(path) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer s.Close() |
| + // Open migrates but does not backfill accounts (NewServer does), and |
| + // rename and move both resolve the app's account row: seed the ops |
| + // account the legacy row belongs to, and the move's destination. |
| + if _, err := s.db.Exec(`INSERT OR IGNORE INTO accounts (id, sqid, name, created_at) VALUES (?, 'bab', 'Ops', 0)`, OpsAccountID); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := s.RenameAppIn(OpsAccountID, "legacy", "renamed"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if r, src, _ := s.AppRegion(OpsAccountID, "renamed"); r != "uk" || src != "timezone" { |
| + t.Fatalf("after rename = %q %q", r, src) |
| + } |
| + const other = 2001 |
| + if _, err := s.db.Exec(`INSERT INTO accounts (id, sqid, name, created_at) VALUES (?, 'zzz', 'Other', 0)`, other); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := s.MoveApp("renamed", OpsAccountID, other); err != nil { |
| + t.Fatal(err) |
| + } |
| + if r, src, _ := s.AppRegion(other, "renamed"); r != "uk" || src != "timezone" { |
| + t.Fatalf("after move = %q %q", r, src) |
| + } |
| +} |
| + |
| +// PF10: a database from BEFORE the region columns existed gains both on open, |
| +// and every row it already held reads "" (the home, no source) in both. The |
| +// column default is the backfill, so this is the proof there is none to run. |
| +// It is checked with raw SQL, not the accessors, so it asserts the schema and |
| +// the stored values rather than the accessor's own defaulting. Both shapes a |
| +// real database can be in: not yet rebuilt (PRIMARY KEY(app), so the ALTERs |
| +// and the migrateAppsKey copy both run) and already rebuilt (the ALTERs only). |
| +func TestPreRegionDatabaseGainsColumnsReadingEmpty(t *testing.T) { |
| + shapes := map[string]string{ |
| + "pre-rebuild": `CREATE TABLE apps ( |
| + app TEXT PRIMARY KEY, |
| + account_id INTEGER NOT NULL DEFAULT 1, |
| + created_by TEXT NOT NULL, |
| + created_at INTEGER NOT NULL, |
| + deleted_at INTEGER NOT NULL DEFAULT 0, |
| + placement TEXT NOT NULL DEFAULT '' |
| +);`, |
| + "already-rebuilt": `CREATE TABLE apps ( |
| + app TEXT NOT NULL, |
| + account_id INTEGER NOT NULL DEFAULT 1, |
| + created_by TEXT NOT NULL, |
| + created_at INTEGER NOT NULL, |
| + deleted_at INTEGER NOT NULL DEFAULT 0, |
| + placement TEXT NOT NULL DEFAULT '', |
| + control_plane INTEGER NOT NULL DEFAULT 0, |
| + build_probe TEXT NOT NULL DEFAULT '', |
| + PRIMARY KEY (account_id, app) |
| +);`, |
| + } |
| + for name, ddl := range shapes { |
| + t.Run(name, func(t *testing.T) { |
| + path := filepath.Join(t.TempDir(), "console.db") |
| + raw, err := sql.Open("sqlite", path) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := raw.Exec(ddl); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := raw.Exec(`INSERT INTO apps (app, created_by, created_at) VALUES ('one', 'p@k.dev', 1), ('two', 'p@k.dev', 2)`); err != nil { |
| + t.Fatal(err) |
| + } |
| + raw.Close() |
| + |
| + s, err := Open(path) |
| + if err != nil { |
| + t.Fatalf("Open: %v", err) |
| + } |
| + defer s.Close() |
| + rows, err := s.db.Query(`SELECT app, region, region_source FROM apps ORDER BY app`) |
| + if err != nil { |
| + t.Fatalf("the region columns are missing after Open: %v", err) |
| + } |
| + defer rows.Close() |
| + n := 0 |
| + for rows.Next() { |
| + var app, region, source string |
| + if err := rows.Scan(&app, ®ion, &source); err != nil { |
| + t.Fatal(err) |
| + } |
| + if region != "" || source != "" { |
| + t.Fatalf("%s reads region %q source %q, want both empty", app, region, source) |
| + } |
| + n++ |
| + } |
| + if err := rows.Err(); err != nil { |
| + t.Fatal(err) |
| + } |
| + if n != 2 { |
| + t.Fatalf("%d rows survived, want 2", n) |
| + } |
| + }) |
| + } |
| +} |