| 1 | package projectiondb |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "reasonix/internal/filelock" |
| 14 | ) |
| 15 | |
| 16 | func testMigrations() []Migration { |
| 17 | return []Migration{{Version: 1, Apply: func(ctx context.Context, tx *sql.Tx) error { |
| 18 | _, err := tx.ExecContext(ctx, `CREATE TABLE values_table(value TEXT NOT NULL)`) |
| 19 | return err |
| 20 | }}} |
| 21 | } |
| 22 | |
| 23 | func TestOpenAppliesLedgerAndPrivatePermissions(t *testing.T) { |
| 24 | t.Parallel() |
| 25 | path := filepath.Join(t.TempDir(), "catalog", "v1.sqlite") |
| 26 | handle, err := Open(context.Background(), OpenOptions{Path: path, MemoryName: "test", Migrations: testMigrations()}) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | t.Cleanup(func() { _ = handle.DB.Close() }) |
| 31 | var version int |
| 32 | if err := handle.DB.QueryRow(`SELECT MAX(version) FROM schema_migrations`).Scan(&version); err != nil || version != 1 { |
| 33 | t.Fatalf("version=%d err=%v", version, err) |
| 34 | } |
| 35 | if runtime.GOOS == "windows" { |
| 36 | // os.Chmod cannot express POSIX 0600 on Windows (read-only bit only). |
| 37 | return |
| 38 | } |
| 39 | if info, err := os.Stat(path); err != nil || info.Mode().Perm()&0o077 != 0 { |
| 40 | t.Fatalf("database permissions=%v err=%v", info.Mode().Perm(), err) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestFutureSchemaIsPreservedAndFallsBackToMemory(t *testing.T) { |
| 45 | t.Parallel() |
| 46 | path := filepath.Join(t.TempDir(), "catalog.sqlite") |
| 47 | seed, err := Open(context.Background(), OpenOptions{Path: path, MemoryName: "seed", Migrations: testMigrations()}) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | if _, err := seed.DB.Exec(`INSERT INTO schema_migrations(version, applied_at) VALUES(2, ?)`, time.Now().UnixMilli()); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | if err := seed.DB.Close(); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | handle, err := Open(context.Background(), OpenOptions{Path: path, MemoryName: "future", Migrations: testMigrations()}) |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | defer handle.DB.Close() |
| 62 | if handle.Status.Mode != ModeMemory || handle.Status.State != StateDegraded || handle.Status.QuarantinedPath != "" { |
| 63 | t.Fatalf("status=%#v", handle.Status) |
| 64 | } |
| 65 | inspection := Inspect(context.Background(), path) |
| 66 | if !inspection.Exists || inspection.Schema != 2 { |
| 67 | t.Fatalf("inspection=%#v", inspection) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestInspectDoesNotCreateMissingDatabase(t *testing.T) { |
| 72 | t.Parallel() |
| 73 | path := filepath.Join(t.TempDir(), "missing.sqlite") |
| 74 | inspection := Inspect(context.Background(), path) |
| 75 | if inspection.Exists { |
| 76 | t.Fatalf("inspection=%#v", inspection) |
| 77 | } |
| 78 | if _, err := os.Stat(path); !os.IsNotExist(err) { |
| 79 | t.Fatalf("inspect created database: %v", err) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestInspectReadsLiveWALSchema(t *testing.T) { |
| 84 | path := filepath.Join(t.TempDir(), "live.sqlite") |
| 85 | handle, err := Open(t.Context(), OpenOptions{Path: path, Migrations: testMigrations()}) |
| 86 | if err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | defer handle.DB.Close() |
| 90 | if _, err := handle.DB.Exec(`PRAGMA wal_autocheckpoint=0; INSERT INTO schema_migrations(version, applied_at) VALUES(42, 1)`); err != nil { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | inspection := Inspect(t.Context(), path) |
| 94 | if !inspection.Exists || inspection.Error != "" || inspection.Schema != 42 || inspection.Integrity != "ok" { |
| 95 | t.Fatalf("live WAL inspection=%+v", inspection) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestRebuildPublishesOnlyValidatedReplacement(t *testing.T) { |
| 100 | t.Parallel() |
| 101 | path := filepath.Join(t.TempDir(), "catalog.sqlite") |
| 102 | opts := OpenOptions{Path: path, MemoryName: "rebuild", Migrations: testMigrations()} |
| 103 | seed, err := Open(context.Background(), opts) |
| 104 | if err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | if _, err := seed.DB.Exec(`INSERT INTO values_table(value) VALUES('old')`); err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | if err := seed.DB.Close(); err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | if err := Rebuild(context.Background(), opts, func(ctx context.Context, db *sql.DB) error { |
| 114 | _, err := db.ExecContext(ctx, `INSERT INTO values_table(value) VALUES('new')`) |
| 115 | return err |
| 116 | }); err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | rebuilt, err := Open(context.Background(), opts) |
| 120 | if err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | defer rebuilt.DB.Close() |
| 124 | var value string |
| 125 | if err := rebuilt.DB.QueryRow(`SELECT value FROM values_table`).Scan(&value); err != nil || value != "new" { |
| 126 | t.Fatalf("value=%q err=%v", value, err) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestRebuildCanRetainPreviousDatabaseForRollback(t *testing.T) { |
| 131 | t.Parallel() |
| 132 | path := filepath.Join(t.TempDir(), "catalog.sqlite") |
| 133 | opts := OpenOptions{Path: path, MemoryName: "rebuild-retain", Migrations: testMigrations(), RetainBackup: true} |
| 134 | seed, err := Open(context.Background(), opts) |
| 135 | if err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | if _, err := seed.DB.Exec(`INSERT INTO values_table(value) VALUES('old')`); err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | if err := seed.DB.Close(); err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | if err := Rebuild(context.Background(), opts, func(ctx context.Context, db *sql.DB) error { |
| 145 | _, err := db.ExecContext(ctx, `INSERT INTO values_table(value) VALUES('new')`) |
| 146 | return err |
| 147 | }); err != nil { |
| 148 | t.Fatal(err) |
| 149 | } |
| 150 | backups, err := filepath.Glob(path + ".replaced-*") |
| 151 | if err != nil || len(backups) != 1 { |
| 152 | t.Fatalf("retained backups = %v err=%v, want one backup", backups, err) |
| 153 | } |
| 154 | rollback, err := Open(context.Background(), OpenOptions{Path: backups[0], MemoryName: "rollback", Migrations: testMigrations()}) |
| 155 | if err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | defer rollback.DB.Close() |
| 159 | var value string |
| 160 | if err := rollback.DB.QueryRow(`SELECT value FROM values_table`).Scan(&value); err != nil || value != "old" { |
| 161 | t.Fatalf("rollback value=%q err=%v, want old", value, err) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | func TestDiskOpenUsesCrossPlatformURI(t *testing.T) { |
| 166 | t.Parallel() |
| 167 | // Opening through the DSN must succeed on this platform. |
| 168 | handle, err := Open(context.Background(), OpenOptions{ |
| 169 | Path: filepath.Join(t.TempDir(), "opened.sqlite"), MemoryName: "dsn", Migrations: testMigrations(), RequireDisk: true, |
| 170 | }) |
| 171 | if err != nil { |
| 172 | t.Fatal(err) |
| 173 | } |
| 174 | t.Cleanup(func() { _ = handle.DB.Close() }) |
| 175 | if handle.Status.Mode != ModeDisk { |
| 176 | t.Fatalf("status=%#v", handle.Status) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | func TestOpenBlankPathUsesMemoryWithoutRequireDisk(t *testing.T) { |
| 181 | t.Parallel() |
| 182 | handle, err := Open(context.Background(), OpenOptions{Path: "", MemoryName: "blank", Migrations: testMigrations()}) |
| 183 | if err != nil { |
| 184 | t.Fatal(err) |
| 185 | } |
| 186 | t.Cleanup(func() { _ = handle.DB.Close() }) |
| 187 | if handle.Status.Mode != ModeMemory { |
| 188 | t.Fatalf("status=%#v", handle.Status) |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | func TestMemoryOpenUsesOneConnectionDespiteRequestedPool(t *testing.T) { |
| 193 | t.Parallel() |
| 194 | handle, err := Open(context.Background(), OpenOptions{ |
| 195 | InMemory: true, MemoryName: "single-connection", Migrations: testMigrations(), MaxOpenConns: 4, |
| 196 | }) |
| 197 | if err != nil { |
| 198 | t.Fatal(err) |
| 199 | } |
| 200 | t.Cleanup(func() { _ = handle.DB.Close() }) |
| 201 | if got := handle.DB.Stats().MaxOpenConnections; got != 1 { |
| 202 | t.Fatalf("memory max open connections = %d, want 1 to avoid shared-cache table deadlocks", got) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | func TestMemoryOpenIsolatesHandlesWithSameNameAndClock(t *testing.T) { |
| 207 | t.Parallel() |
| 208 | fixedNow := func() time.Time { return time.Unix(123, 456) } |
| 209 | opts := OpenOptions{ |
| 210 | InMemory: true, MemoryName: "same-name", Migrations: testMigrations(), Now: fixedNow, |
| 211 | } |
| 212 | first, err := Open(context.Background(), opts) |
| 213 | if err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | t.Cleanup(func() { _ = first.DB.Close() }) |
| 217 | second, err := Open(context.Background(), opts) |
| 218 | if err != nil { |
| 219 | t.Fatal(err) |
| 220 | } |
| 221 | t.Cleanup(func() { _ = second.DB.Close() }) |
| 222 | if _, err := first.DB.Exec(`INSERT INTO values_table(value) VALUES('first-only')`); err != nil { |
| 223 | t.Fatal(err) |
| 224 | } |
| 225 | var count int |
| 226 | if err := second.DB.QueryRow(`SELECT COUNT(*) FROM values_table`).Scan(&count); err != nil { |
| 227 | t.Fatal(err) |
| 228 | } |
| 229 | if count != 0 { |
| 230 | t.Fatalf("second memory projection contains %d rows from first handle, want isolated database", count) |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | func TestRebuildRequireDiskSurfacesOpenErrors(t *testing.T) { |
| 235 | t.Parallel() |
| 236 | // RequireDisk rebuild of an unwritable parent should not silently memory-open. |
| 237 | parent := filepath.Join(t.TempDir(), "missing-parent", "nested") |
| 238 | err := Rebuild(context.Background(), OpenOptions{ |
| 239 | Path: filepath.Join(parent, "v1.sqlite"), MemoryName: "rebuild-req", Migrations: testMigrations(), |
| 240 | }, func(context.Context, *sql.DB) error { return nil }) |
| 241 | // Either parent creation works (temp dir is writable) or we get a real error. |
| 242 | // When the path is under TempDir, MkdirAll succeeds; verify success path. |
| 243 | if err != nil { |
| 244 | t.Fatalf("rebuild under temp should succeed: %v", err) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | func TestBusyOpenDoesNotQuarantineHealthyDatabase(t *testing.T) { |
| 249 | t.Parallel() |
| 250 | path := filepath.Join(t.TempDir(), "catalog.sqlite") |
| 251 | opts := OpenOptions{Path: path, MemoryName: "busy", Migrations: testMigrations()} |
| 252 | seed, err := Open(context.Background(), opts) |
| 253 | if err != nil { |
| 254 | t.Fatal(err) |
| 255 | } |
| 256 | if _, err := seed.DB.Exec(`INSERT INTO values_table(value) VALUES('keep')`); err != nil { |
| 257 | t.Fatal(err) |
| 258 | } |
| 259 | // Hold the disk connection open so a second open that somehow fails still |
| 260 | // must not rename the healthy file. Force the memory path via empty-path |
| 261 | // corruption classifier: a future-schema-like non-corruption error. |
| 262 | if err := seed.DB.Close(); err != nil { |
| 263 | t.Fatal(err) |
| 264 | } |
| 265 | // Rename aside to simulate a permission/open failure without corruption. |
| 266 | locked := path + ".locked" |
| 267 | if err := os.Rename(path, locked); err != nil { |
| 268 | t.Fatal(err) |
| 269 | } |
| 270 | // Open with a path that fails because the file is missing mid-flight after |
| 271 | // we restore it — use InMemory true to prove non-corruption path. The |
| 272 | // classifier unit is covered by isCorruptionError via future schema test. |
| 273 | if isCorruptionError(errors.New("database is locked (5) (SQLITE_BUSY)")) { |
| 274 | t.Fatal("SQLITE_BUSY must not be treated as corruption") |
| 275 | } |
| 276 | if isCorruptionError(errors.New("unable to open database file")) { |
| 277 | t.Fatal("CANTOPEN must not be treated as corruption") |
| 278 | } |
| 279 | if !isCorruptionError(errors.New("projection integrity check: *** in database main ***")) { |
| 280 | t.Fatal("integrity failures must quarantine") |
| 281 | } |
| 282 | if err := os.Rename(locked, path); err != nil { |
| 283 | t.Fatal(err) |
| 284 | } |
| 285 | if matches, _ := filepath.Glob(path + ".corrupt-*"); len(matches) != 0 { |
| 286 | t.Fatalf("unexpected quarantine files: %v", matches) |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | func TestRebuildFailureKeepsOldDatabase(t *testing.T) { |
| 291 | t.Parallel() |
| 292 | path := filepath.Join(t.TempDir(), "catalog.sqlite") |
| 293 | opts := OpenOptions{Path: path, MemoryName: "rebuild-failure", Migrations: testMigrations()} |
| 294 | seed, err := Open(context.Background(), opts) |
| 295 | if err != nil { |
| 296 | t.Fatal(err) |
| 297 | } |
| 298 | if _, err := seed.DB.Exec(`INSERT INTO values_table(value) VALUES('old')`); err != nil { |
| 299 | t.Fatal(err) |
| 300 | } |
| 301 | if err := seed.DB.Close(); err != nil { |
| 302 | t.Fatal(err) |
| 303 | } |
| 304 | wantErr := errors.New("populate failed") |
| 305 | if err := Rebuild(context.Background(), opts, func(context.Context, *sql.DB) error { return wantErr }); !errors.Is(err, wantErr) { |
| 306 | t.Fatalf("Rebuild error=%v", err) |
| 307 | } |
| 308 | current, err := Open(context.Background(), opts) |
| 309 | if err != nil { |
| 310 | t.Fatal(err) |
| 311 | } |
| 312 | defer current.DB.Close() |
| 313 | var value string |
| 314 | if err := current.DB.QueryRow(`SELECT value FROM values_table`).Scan(&value); err != nil || value != "old" { |
| 315 | t.Fatalf("value=%q err=%v", value, err) |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | func TestRebuildHoldsExclusiveLifecycleLock(t *testing.T) { |
| 320 | t.Parallel() |
| 321 | path := filepath.Join(t.TempDir(), "catalog.sqlite") |
| 322 | opts := OpenOptions{Path: path, MemoryName: "rebuild-lock", Migrations: testMigrations()} |
| 323 | entered := make(chan struct{}) |
| 324 | releasePopulate := make(chan struct{}) |
| 325 | firstDone := make(chan error, 1) |
| 326 | go func() { |
| 327 | firstDone <- Rebuild(context.Background(), opts, func(context.Context, *sql.DB) error { |
| 328 | close(entered) |
| 329 | <-releasePopulate |
| 330 | return nil |
| 331 | }) |
| 332 | }() |
| 333 | <-entered |
| 334 | if release, err := filelock.TryAcquire(path + ".rebuild.lock"); !errors.Is(err, filelock.ErrHeld) { |
| 335 | if err == nil { |
| 336 | release() |
| 337 | } |
| 338 | t.Fatalf("rebuild lifecycle lock error = %v, want held", err) |
| 339 | } |
| 340 | canceled, cancel := context.WithCancel(context.Background()) |
| 341 | cancel() |
| 342 | if err := Rebuild(canceled, opts, nil); !errors.Is(err, context.Canceled) { |
| 343 | t.Fatalf("contended rebuild error = %v, want context canceled", err) |
| 344 | } |
| 345 | close(releasePopulate) |
| 346 | if err := <-firstDone; err != nil { |
| 347 | t.Fatalf("first rebuild: %v", err) |
| 348 | } |
| 349 | release, err := filelock.TryAcquire(path + ".rebuild.lock") |
| 350 | if err != nil { |
| 351 | t.Fatalf("lifecycle lock remained held: %v", err) |
| 352 | } |
| 353 | release() |
| 354 | } |
| 355 |