Initial commit
This commit is contained in:
commit
b6014c6ffc
47 files changed
+4329
No files matched your search
@@ -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);
|
||||
Reference in new issue
Block a user