Refuse an unversioned database that already has a files table (closes #11)
check / check (push) Failing after 1s
check / check (push) Failing after 1s
An unversioned database (user_version 0) that already contains a files table was not created by this build; it is a foreign or partially initialized file. Adopting it silently could corrupt unrelated data, so createSchema now checks for a files table first and, when one exists, returns the schema-version error telling the operator to remove the file and rescan. A genuinely empty database is still created and stamped as before. Model: opus-4-8
This commit is contained in:
+32
@@ -78,6 +78,38 @@ func TestOpenScanDatabaseCreates(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenScanDatabaseUnversionedForeign(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
path := testDBPath(t)
|
||||
|
||||
// A database that has a files table but user_version 0 — a foreign
|
||||
// or partially initialized file. scan must refuse it with a clear
|
||||
// schema-version error, not adopt it and not emit a raw SQLite
|
||||
// "table files already exists".
|
||||
db, err := openDB(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = db.ExecContext(t.Context(), "CREATE TABLE files (x INTEGER)")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_ = db.Close()
|
||||
|
||||
_, err = openScanDatabase(t.Context(), path)
|
||||
if !errors.Is(err, errSchemaVersion) {
|
||||
t.Fatalf("err = %v, want errSchemaVersion", err)
|
||||
}
|
||||
|
||||
if !strings.Contains(err.Error(), "remove the file and rescan") {
|
||||
t.Fatalf("err = %v, want it to tell the operator to remove and rescan",
|
||||
err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenReportDatabaseMissing(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user