| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | _ "modernc.org/sqlite" |
| 10 | ) |
| 11 | |
| 12 | func TestAppRegionRoundTripAndDefaults(t *testing.T) { |
| 13 | s := openTestStore(t) |
| 14 | now := time.Now() |
| 15 | if ok, err := s.CreateAppInRegion(OpsAccountID, "blog", "p@k.dev", "uk", "timezone", now); err != nil || !ok { |
| 16 | t.Fatalf("CreateAppInRegion = %v %v", ok, err) |
| 17 | } |
| 18 | if r, src, err := s.AppRegion(OpsAccountID, "blog"); err != nil || r != "uk" || src != "timezone" { |
| 19 | t.Fatalf("AppRegion = %q %q %v", r, src, err) |
| 20 | } |
| 21 | if ok, err := s.CreateAppIn(OpsAccountID, "old", "p@k.dev", now); err != nil || !ok { |
| 22 | t.Fatalf("CreateAppIn = %v %v", ok, err) |
| 23 | } |
| 24 | if r, src, _ := s.AppRegion(OpsAccountID, "old"); r != "" || src != "" { |
| 25 | t.Fatalf("CreateAppIn row = %q %q, want the home and no source", r, src) |
| 26 | } |
| 27 | if r, src, err := s.AppRegion(OpsAccountID, "missing"); err != nil || r != "" || src != "" { |
| 28 | t.Fatalf("missing row = %q %q %v", r, src, err) |
| 29 | } |
| 30 | // A lost claim race writes nothing. |
| 31 | if ok, _ := s.CreateAppInRegion(OpsAccountID, "blog", "x@k.dev", "au", "ip", now); ok { |
| 32 | t.Fatal("a second claim of the same name succeeded") |
| 33 | } |
| 34 | if r, _, _ := s.AppRegion(OpsAccountID, "blog"); r != "uk" { |
| 35 | t.Fatalf("a lost claim changed the region to %q", r) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // The fixture runs NewServer, whose backfill creates the ops account row |
| 40 | // (sqid "bab") that NonHomeAppRegions joins on. |
| 41 | func TestNonHomeAppRegions(t *testing.T) { |
| 42 | fx := newConsoleFixture(t) |
| 43 | now := time.Now() |
| 44 | if _, err := fx.st.CreateAppInRegion(OpsAccountID, "blog", "p@k.dev", "uk", "chosen", now); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if _, err := fx.st.CreateAppInRegion(OpsAccountID, "home", "p@k.dev", "", "default", now); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | rows, err := fx.st.NonHomeAppRegions() |
| 51 | if err != nil || len(rows) != 1 || rows[0].App != "blog" || rows[0].Region != "uk" || rows[0].Sqid != "bab" { |
| 52 | t.Fatalf("NonHomeAppRegions = %+v %v", rows, err) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // spec §5.2: a database from BEFORE migrateAppsKey whose rows already |
| 57 | // carry an app region keeps it through the rebuild, a reopen, a rename and |
| 58 | // a move to another account. An empty value would not prove the copy. |
| 59 | func TestRegionValuesSurviveRebuildRenameAndMove(t *testing.T) { |
| 60 | path := filepath.Join(t.TempDir(), "console.db") |
| 61 | db, err := sql.Open("sqlite", path) |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | if _, err := db.Exec(`CREATE TABLE apps ( |
| 66 | app TEXT PRIMARY KEY, created_by TEXT NOT NULL, created_at INTEGER NOT NULL, |
| 67 | account_id INTEGER NOT NULL DEFAULT 1, deleted_at INTEGER NOT NULL DEFAULT 0, |
| 68 | placement TEXT NOT NULL DEFAULT '', control_plane INTEGER NOT NULL DEFAULT 0, |
| 69 | build_probe TEXT NOT NULL DEFAULT '', region TEXT NOT NULL DEFAULT '', |
| 70 | region_source TEXT NOT NULL DEFAULT ''); |
| 71 | INSERT INTO apps (app, created_by, created_at, region, region_source) VALUES ('legacy', 'p@k.dev', 1, 'uk', 'timezone');`); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | db.Close() |
| 75 | for i := 0; i < 2; i++ { // the rebuild, then a reopen of the rebuilt table |
| 76 | s, err := Open(path) |
| 77 | if err != nil { |
| 78 | t.Fatalf("open %d: %v", i, err) |
| 79 | } |
| 80 | if r, src, err := s.AppRegion(OpsAccountID, "legacy"); err != nil || r != "uk" || src != "timezone" { |
| 81 | t.Fatalf("open %d: legacy = %q %q %v, want uk/timezone carried", i, r, src, err) |
| 82 | } |
| 83 | s.Close() |
| 84 | } |
| 85 | s, err := Open(path) |
| 86 | if err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | defer s.Close() |
| 90 | // Open migrates but does not backfill accounts (NewServer does), and |
| 91 | // rename and move both resolve the app's account row: seed the ops |
| 92 | // account the legacy row belongs to, and the move's destination. |
| 93 | if _, err := s.db.Exec(`INSERT OR IGNORE INTO accounts (id, sqid, name, created_at) VALUES (?, 'bab', 'Ops', 0)`, OpsAccountID); err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | if err := s.RenameAppIn(OpsAccountID, "legacy", "renamed"); err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | if r, src, _ := s.AppRegion(OpsAccountID, "renamed"); r != "uk" || src != "timezone" { |
| 100 | t.Fatalf("after rename = %q %q", r, src) |
| 101 | } |
| 102 | const other = 2001 |
| 103 | if _, err := s.db.Exec(`INSERT INTO accounts (id, sqid, name, created_at) VALUES (?, 'zzz', 'Other', 0)`, other); err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | if err := s.MoveApp("renamed", OpsAccountID, other); err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | if r, src, _ := s.AppRegion(other, "renamed"); r != "uk" || src != "timezone" { |
| 110 | t.Fatalf("after move = %q %q", r, src) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // PF10: a database from BEFORE the region columns existed gains both on open, |
| 115 | // and every row it already held reads "" (the home, no source) in both. The |
| 116 | // column default is the backfill, so this is the proof there is none to run. |
| 117 | // It is checked with raw SQL, not the accessors, so it asserts the schema and |
| 118 | // the stored values rather than the accessor's own defaulting. Both shapes a |
| 119 | // real database can be in: not yet rebuilt (PRIMARY KEY(app), so the ALTERs |
| 120 | // and the migrateAppsKey copy both run) and already rebuilt (the ALTERs only). |
| 121 | func TestPreRegionDatabaseGainsColumnsReadingEmpty(t *testing.T) { |
| 122 | shapes := map[string]string{ |
| 123 | "pre-rebuild": `CREATE TABLE apps ( |
| 124 | app TEXT PRIMARY KEY, |
| 125 | account_id INTEGER NOT NULL DEFAULT 1, |
| 126 | created_by TEXT NOT NULL, |
| 127 | created_at INTEGER NOT NULL, |
| 128 | deleted_at INTEGER NOT NULL DEFAULT 0, |
| 129 | placement TEXT NOT NULL DEFAULT '' |
| 130 | );`, |
| 131 | "already-rebuilt": `CREATE TABLE apps ( |
| 132 | app TEXT NOT NULL, |
| 133 | account_id INTEGER NOT NULL DEFAULT 1, |
| 134 | created_by TEXT NOT NULL, |
| 135 | created_at INTEGER NOT NULL, |
| 136 | deleted_at INTEGER NOT NULL DEFAULT 0, |
| 137 | placement TEXT NOT NULL DEFAULT '', |
| 138 | control_plane INTEGER NOT NULL DEFAULT 0, |
| 139 | build_probe TEXT NOT NULL DEFAULT '', |
| 140 | PRIMARY KEY (account_id, app) |
| 141 | );`, |
| 142 | } |
| 143 | for name, ddl := range shapes { |
| 144 | t.Run(name, func(t *testing.T) { |
| 145 | path := filepath.Join(t.TempDir(), "console.db") |
| 146 | raw, err := sql.Open("sqlite", path) |
| 147 | if err != nil { |
| 148 | t.Fatal(err) |
| 149 | } |
| 150 | if _, err := raw.Exec(ddl); err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | 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 { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | raw.Close() |
| 157 | |
| 158 | s, err := Open(path) |
| 159 | if err != nil { |
| 160 | t.Fatalf("Open: %v", err) |
| 161 | } |
| 162 | defer s.Close() |
| 163 | rows, err := s.db.Query(`SELECT app, region, region_source FROM apps ORDER BY app`) |
| 164 | if err != nil { |
| 165 | t.Fatalf("the region columns are missing after Open: %v", err) |
| 166 | } |
| 167 | defer rows.Close() |
| 168 | n := 0 |
| 169 | for rows.Next() { |
| 170 | var app, region, source string |
| 171 | if err := rows.Scan(&app, ®ion, &source); err != nil { |
| 172 | t.Fatal(err) |
| 173 | } |
| 174 | if region != "" || source != "" { |
| 175 | t.Fatalf("%s reads region %q source %q, want both empty", app, region, source) |
| 176 | } |
| 177 | n++ |
| 178 | } |
| 179 | if err := rows.Err(); err != nil { |
| 180 | t.Fatal(err) |
| 181 | } |
| 182 | if n != 2 { |
| 183 | t.Fatalf("%d rows survived, want 2", n) |
| 184 | } |
| 185 | }) |
| 186 | } |
| 187 | } |
| 188 | |