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:
@@ -174,9 +174,30 @@ func initSchema(ctx context.Context, db *sql.DB) error {
|
||||
}
|
||||
|
||||
// createSchema applies the schema to a fresh database and stamps the
|
||||
// schema version.
|
||||
// schema version. A database with user_version 0 that already has a
|
||||
// files table was not created by this build — a foreign or partially
|
||||
// initialized file. Adopting it silently could corrupt unrelated data,
|
||||
// so that is a fatal schema-version error telling the operator to
|
||||
// remove the file and rescan.
|
||||
func createSchema(ctx context.Context, db *sql.DB) error {
|
||||
_, err := db.ExecContext(ctx, createTableSQL)
|
||||
var name string
|
||||
|
||||
err := db.QueryRowContext(ctx,
|
||||
"SELECT name FROM sqlite_master "+
|
||||
"WHERE type = 'table' AND name = 'files'").Scan(&name)
|
||||
|
||||
switch {
|
||||
case err == nil:
|
||||
return fmt.Errorf(
|
||||
"has a files table but no schema version; "+
|
||||
"remove the file and rescan: %w", errSchemaVersion)
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
// Genuinely empty: create the schema below.
|
||||
default:
|
||||
return fmt.Errorf("check for files table: %w", err)
|
||||
}
|
||||
|
||||
_, err = db.ExecContext(ctx, createTableSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create schema: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user