Initial commit
This commit is contained in:
commit
b6014c6ffc
47 files changed
+4329
No files matched your search
@@ -0,0 +1,16 @@
|
||||
// Package migrations embeds the DB schema for both supported drivers so the
|
||||
// server can apply it on startup with no separate migrate step.
|
||||
//
|
||||
// Postgres uses versioned golang-migrate .sql files (PostgresFS). SQLite
|
||||
// uses one idempotent schema file applied directly (SQLiteSchemaSQL) — it's
|
||||
// a local dev/testing convenience DB, not something that needs migration
|
||||
// history.
|
||||
package migrations
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed postgres/*.sql
|
||||
var PostgresFS embed.FS
|
||||
|
||||
//go:embed sqlite/schema.sql
|
||||
var SQLiteSchemaSQL string
|
||||
@@ -0,0 +1,5 @@
|
||||
DROP TABLE IF EXISTS reminders;
|
||||
DROP TABLE IF EXISTS service_entry_files;
|
||||
DROP TABLE IF EXISTS service_entries;
|
||||
DROP TABLE IF EXISTS cars;
|
||||
DROP TABLE IF EXISTS users;
|
||||
@@ -0,0 +1,68 @@
|
||||
-- IDs are generated in Go (uuid.New()) rather than via a DB default, so the
|
||||
-- same INSERT statements work unchanged against SQLite too — no pgcrypto
|
||||
-- extension needed.
|
||||
|
||||
CREATE TABLE users (
|
||||
id UUID PRIMARY KEY,
|
||||
oidc_sub TEXT NOT NULL UNIQUE,
|
||||
email TEXT NOT NULL,
|
||||
name TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE cars (
|
||||
id UUID PRIMARY KEY,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
rego TEXT NOT NULL,
|
||||
make TEXT,
|
||||
model TEXT,
|
||||
year INT,
|
||||
color TEXT,
|
||||
odometer INT,
|
||||
rego_expiry DATE,
|
||||
wof_expiry DATE,
|
||||
photo_s3_key TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
UNIQUE (user_id, rego)
|
||||
);
|
||||
CREATE INDEX idx_cars_user_id ON cars(user_id);
|
||||
|
||||
CREATE TABLE service_entries (
|
||||
id UUID PRIMARY KEY,
|
||||
car_id UUID NOT NULL REFERENCES cars(id) ON DELETE CASCADE,
|
||||
entry_date DATE NOT NULL,
|
||||
entry_type TEXT NOT NULL,
|
||||
description TEXT,
|
||||
performed_at TEXT,
|
||||
odometer INT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX idx_service_entries_car_id ON service_entries(car_id);
|
||||
CREATE INDEX idx_service_entries_date ON service_entries(entry_date);
|
||||
|
||||
CREATE TABLE service_entry_files (
|
||||
id UUID PRIMARY KEY,
|
||||
service_entry_id UUID 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 BIGINT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX idx_service_entry_files_entry_id ON service_entry_files(service_entry_id);
|
||||
|
||||
CREATE TABLE reminders (
|
||||
id UUID PRIMARY KEY,
|
||||
car_id UUID NOT NULL REFERENCES cars(id) ON DELETE CASCADE,
|
||||
reminder_type TEXT NOT NULL,
|
||||
label TEXT,
|
||||
due_date DATE,
|
||||
due_odometer INT,
|
||||
notes TEXT,
|
||||
completed_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CHECK (due_date IS NOT NULL OR due_odometer IS NOT NULL)
|
||||
);
|
||||
CREATE INDEX idx_reminders_car_id ON reminders(car_id);
|
||||
CREATE INDEX idx_reminders_due_date ON reminders(due_date) WHERE completed_at IS NULL;
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS knowledge_entries;
|
||||
@@ -0,0 +1,9 @@
|
||||
CREATE TABLE knowledge_entries (
|
||||
id UUID PRIMARY KEY,
|
||||
car_id UUID NOT NULL REFERENCES cars(id) ON DELETE CASCADE,
|
||||
title TEXT NOT NULL,
|
||||
body TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX idx_knowledge_entries_car_id ON knowledge_entries(car_id);
|
||||
@@ -0,0 +1,5 @@
|
||||
DROP TABLE IF EXISTS fuel_entries;
|
||||
ALTER TABLE cars DROP COLUMN IF EXISTS share_token;
|
||||
ALTER TABLE cars DROP COLUMN IF EXISTS service_interval_km;
|
||||
ALTER TABLE cars DROP COLUMN IF EXISTS service_interval_months;
|
||||
ALTER TABLE service_entries DROP COLUMN IF EXISTS is_service;
|
||||
@@ -0,0 +1,28 @@
|
||||
-- Nullable, unique when set: NULL means sharing is off for that car. A
|
||||
-- random opaque token (not the car's own id) so a share link can't be
|
||||
-- guessed and doesn't leak the car's real UUID.
|
||||
ALTER TABLE cars ADD COLUMN share_token TEXT UNIQUE;
|
||||
|
||||
-- Either/both may be set; whichever comes first triggers the synthesized
|
||||
-- "service due" reminder (computed from the most recent is_service entry).
|
||||
ALTER TABLE cars ADD COLUMN service_interval_km INT;
|
||||
ALTER TABLE cars ADD COLUMN service_interval_months INT;
|
||||
|
||||
-- Marks a service_entries row as "this was a full service" — i.e. the
|
||||
-- baseline the next service-due reminder counts forward from. Most entries
|
||||
-- (oil top-up, tyre rotation, etc.) won't set this.
|
||||
ALTER TABLE service_entries ADD COLUMN is_service BOOLEAN NOT NULL DEFAULT false;
|
||||
|
||||
CREATE TABLE fuel_entries (
|
||||
id UUID PRIMARY KEY,
|
||||
car_id UUID NOT NULL REFERENCES cars(id) ON DELETE CASCADE,
|
||||
entry_date DATE NOT NULL,
|
||||
litres DOUBLE PRECISION NOT NULL,
|
||||
cost DOUBLE PRECISION NOT NULL,
|
||||
odometer INT,
|
||||
location TEXT,
|
||||
notes TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX idx_fuel_entries_car_id ON fuel_entries(car_id);
|
||||
CREATE INDEX idx_fuel_entries_date ON fuel_entries(entry_date);
|
||||
@@ -0,0 +1,110 @@
|
||||
-- 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,
|
||||
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);
|
||||
Reference in new issue
Block a user