67 lines
2.9 KiB
Go
67 lines
2.9 KiB
Go
package db
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
_ "modernc.org/sqlite"
|
|
)
|
|
|
|
// TestSQLiteAddsMissingColumns simulates exactly the bug a user hit: an
|
|
// existing local .db file created before share_token/service_interval_*/
|
|
// is_service existed. Connect must add them in place, not require deleting
|
|
// the file. The "old" schema is hand-built (not via schema.sql, which
|
|
// already has the new columns) so CREATE TABLE IF NOT EXISTS in the real
|
|
// connectSQLite is a genuine no-op against it, same as it would be against
|
|
// a real pre-existing file.
|
|
func TestSQLiteAddsMissingColumns(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "old.db")
|
|
|
|
oldConn, err := sqlx.Connect("sqlite", path)
|
|
if err != nil {
|
|
t.Fatalf("open old-style db: %v", err)
|
|
}
|
|
for _, stmt := range []string{
|
|
`CREATE TABLE users (id TEXT PRIMARY KEY, oidc_sub TEXT NOT NULL UNIQUE, email TEXT NOT NULL, name TEXT, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP)`,
|
|
`CREATE TABLE cars (id TEXT PRIMARY KEY, user_id TEXT NOT NULL, rego TEXT NOT NULL, odometer INTEGER, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP)`,
|
|
`CREATE TABLE service_entries (id TEXT PRIMARY KEY, car_id TEXT NOT NULL, entry_date DATE NOT NULL, entry_type TEXT NOT NULL, odometer INTEGER, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP)`,
|
|
} {
|
|
if _, err := oldConn.Exec(stmt); err != nil {
|
|
t.Fatalf("build old-style schema (%s): %v", stmt, err)
|
|
}
|
|
}
|
|
oldConn.Close()
|
|
|
|
// Reconnecting through the real path (as a fresh app start would) must
|
|
// backfill the missing columns without erroring and without the
|
|
// caller/operator doing anything special.
|
|
conn, err := connectSQLite(path)
|
|
if err != nil {
|
|
t.Fatalf("connect against old-style schema: %v", err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
if _, err := conn.Exec(`INSERT INTO cars (id, user_id, rego, service_interval_km) VALUES ('c1', 'u1', 'ABC123', 10000)`); err != nil {
|
|
t.Fatalf("insert using backfilled column: %v", err)
|
|
}
|
|
if _, err := conn.Exec(`UPDATE cars SET share_token = 'tok1' WHERE id = 'c1'`); err != nil {
|
|
t.Fatalf("update using backfilled share_token column: %v", err)
|
|
}
|
|
var count int
|
|
if err := conn.Get(&count, `SELECT COUNT(*) FROM cars WHERE share_token = 'tok1'`); err != nil || count != 1 {
|
|
t.Fatalf("expected to find the row by its backfilled share_token, err=%v count=%d", err, count)
|
|
}
|
|
|
|
// The share_token uniqueness constraint must actually be enforced, not
|
|
// just present as an unconstrained column.
|
|
if _, err := conn.Exec(`INSERT INTO cars (id, user_id, rego, share_token) VALUES ('c2', 'u1', 'XYZ789', 'tok1')`); err == nil {
|
|
t.Fatal("expected duplicate share_token to be rejected")
|
|
}
|
|
|
|
if _, err := conn.Exec(`INSERT INTO service_entries (id, car_id, entry_date, entry_type, is_service) VALUES ('e1', 'c1', '2026-01-01', 'Service', 1)`); err != nil {
|
|
t.Fatalf("insert using backfilled is_service column: %v", err)
|
|
}
|
|
}
|