Files
garage/backend/migrations/sqlite/schema.sql
T
2026-08-06 18:59:38 +12:00

112 lines
4.7 KiB
SQL

-- Applied idempotently (CREATE ... IF NOT EXISTS) on every startup rather
-- than through golang-migrate — this is a local dev/testing convenience DB,
-- not something that needs versioned migration history. For production, use
-- Postgres (DB_DRIVER=postgres) and the versioned migrations/postgres/ set.
--
-- Column types matter here beyond documentation: modernc.org/sqlite only
-- auto-converts to/from Go's time.Time when a column's declared type
-- contains DATE/DATETIME/TIMESTAMP (plain TEXT would come back as a string
-- and fail to scan), and only auto-converts to/from Go's bool when the
-- declared type is BOOLEAN — both verified empirically, not assumed.
--
-- NOTE: "IF NOT EXISTS" means new columns added to an existing table
-- definition here do NOT retroactively appear in an already-created local
-- .db file — CREATE TABLE IF NOT EXISTS is a no-op once the table exists.
-- db.go's ensureSQLiteColumns() handles that: it ALTER TABLE ADD COLUMNs
-- anything new (see internal/db/sqlite_migrate.go), so an existing local
-- .db file picks up schema growth automatically — no need to delete it.
CREATE TABLE IF NOT EXISTS 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 IF NOT EXISTS cars (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
rego TEXT NOT NULL,
name TEXT,
make TEXT,
model TEXT,
year INTEGER,
color TEXT,
odometer INTEGER,
rego_expiry DATE,
wof_expiry DATE,
photo_s3_key TEXT,
share_token TEXT UNIQUE,
service_interval_km INTEGER,
service_interval_months INTEGER,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (user_id, rego)
);
CREATE INDEX IF NOT EXISTS idx_cars_user_id ON cars(user_id);
CREATE TABLE IF NOT EXISTS service_entries (
id TEXT PRIMARY KEY,
car_id TEXT NOT NULL REFERENCES cars(id) ON DELETE CASCADE,
entry_date DATE NOT NULL,
entry_type TEXT NOT NULL,
description TEXT,
performed_at TEXT,
odometer INTEGER,
is_service BOOLEAN NOT NULL DEFAULT 0,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_service_entries_car_id ON service_entries(car_id);
CREATE INDEX IF NOT EXISTS idx_service_entries_date ON service_entries(entry_date);
CREATE TABLE IF NOT EXISTS service_entry_files (
id TEXT PRIMARY KEY,
service_entry_id TEXT NOT NULL REFERENCES service_entries(id) ON DELETE CASCADE,
s3_key TEXT NOT NULL,
file_name TEXT NOT NULL,
content_type TEXT NOT NULL,
size_bytes INTEGER NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_service_entry_files_entry_id ON service_entry_files(service_entry_id);
CREATE TABLE IF NOT EXISTS reminders (
id TEXT PRIMARY KEY,
car_id TEXT NOT NULL REFERENCES cars(id) ON DELETE CASCADE,
reminder_type TEXT NOT NULL,
label TEXT,
due_date DATE,
due_odometer INTEGER,
notes TEXT,
completed_at DATETIME,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CHECK (due_date IS NOT NULL OR due_odometer IS NOT NULL)
);
CREATE INDEX IF NOT EXISTS idx_reminders_car_id ON reminders(car_id);
CREATE INDEX IF NOT EXISTS idx_reminders_due_date ON reminders(due_date) WHERE completed_at IS NULL;
CREATE TABLE IF NOT EXISTS knowledge_entries (
id TEXT PRIMARY KEY,
car_id TEXT NOT NULL REFERENCES cars(id) ON DELETE CASCADE,
title TEXT NOT NULL,
body TEXT NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_knowledge_entries_car_id ON knowledge_entries(car_id);
CREATE TABLE IF NOT EXISTS fuel_entries (
id TEXT PRIMARY KEY,
car_id TEXT NOT NULL REFERENCES cars(id) ON DELETE CASCADE,
entry_date DATE NOT NULL,
litres REAL NOT NULL,
cost REAL NOT NULL,
odometer INTEGER,
location TEXT,
notes TEXT,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_fuel_entries_car_id ON fuel_entries(car_id);
CREATE INDEX IF NOT EXISTS idx_fuel_entries_date ON fuel_entries(entry_date);