-- visa.buildfor.life, phase 1: people, passports, addresses in Thailand, -- sessions and the audit log (docs/PLAN.md "Data model"). -- -- Optional text is NOT NULL DEFAULT '' so the code has one empty value; -- dates are NULL when unknown. CREATE TABLE people ( id BIGSERIAL PRIMARY KEY, -- as printed in the passport surname TEXT NOT NULL, first_name TEXT NOT NULL, middle_name TEXT NOT NULL DEFAULT '', title TEXT NOT NULL DEFAULT '' CHECK (title IN ('', 'Mr', 'Mrs', 'Ms', 'Miss')), sex TEXT NOT NULL DEFAULT '' CHECK (sex IN ('', 'F', 'M', 'X')), date_of_birth DATE, place_of_birth TEXT NOT NULL DEFAULT '', -- ISO 3166-1 alpha-3, as in the passport's machine-readable zone nationality TEXT NOT NULL DEFAULT '', occupation TEXT NOT NULL DEFAULT '', employer TEXT NOT NULL DEFAULT '', phone TEXT NOT NULL DEFAULT '', email TEXT NOT NULL DEFAULT '', notes TEXT NOT NULL DEFAULT '', created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), archived_at TIMESTAMPTZ ); CREATE TABLE passports ( id BIGSERIAL PRIMARY KEY, person_id BIGINT NOT NULL REFERENCES people (id), number TEXT NOT NULL, issuing_country TEXT NOT NULL, issued_on DATE, expires_on DATE NOT NULL, -- the issuing authority or place as printed ("issued at" on TM.7) issued_at TEXT NOT NULL DEFAULT '', -- the machine-readable zone as read, two lines mrz TEXT NOT NULL DEFAULT '', created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), CHECK (issued_on IS NULL OR issued_on < expires_on) ); CREATE UNIQUE INDEX passports_number_key ON passports (issuing_country, number); CREATE INDEX passports_person ON passports (person_id); CREATE TABLE addresses ( id BIGSERIAL PRIMARY KEY, person_id BIGINT NOT NULL REFERENCES people (id), -- in the order Thai forms use house_no TEXT NOT NULL DEFAULT '', moo TEXT NOT NULL DEFAULT '', building TEXT NOT NULL DEFAULT '', soi TEXT NOT NULL DEFAULT '', road TEXT NOT NULL DEFAULT '', subdistrict TEXT NOT NULL DEFAULT '', district TEXT NOT NULL DEFAULT '', province TEXT NOT NULL DEFAULT '', postal_code TEXT NOT NULL DEFAULT '', moved_in_on DATE, moved_out_on DATE, -- the owner or landlord who files TM.30 house_master TEXT NOT NULL DEFAULT '', house_master_phone TEXT NOT NULL DEFAULT '', tm30_reported_on DATE, tm30_receipt_no TEXT NOT NULL DEFAULT '', created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), CHECK (moved_out_on IS NULL OR moved_in_on IS NULL OR moved_in_on <= moved_out_on) ); CREATE INDEX addresses_person ON addresses (person_id); -- Signed-in sessions. The key is the SHA-256 of the cookie's value, so a dump -- of this table holds no usable session. CREATE TABLE sessions ( id_hash BYTEA PRIMARY KEY, login TEXT NOT NULL, name TEXT NOT NULL, csrf TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT now(), expires_at TIMESTAMPTZ NOT NULL ); CREATE INDEX sessions_expires ON sessions (expires_at); -- Who viewed or changed which record (PDPA). The names of changed fields -- only, never their values. Append-only. CREATE TABLE audit_log ( id BIGSERIAL PRIMARY KEY, at TIMESTAMPTZ NOT NULL DEFAULT now(), actor TEXT NOT NULL, action TEXT NOT NULL CHECK ( action IN ('view', 'create', 'update', 'delete', 'archive', 'restore', 'sign-in', 'sign-out') ), entity TEXT NOT NULL CHECK (entity IN ('person', 'passport', 'address', 'session')), entity_id BIGINT, person_id BIGINT, fields TEXT[] NOT NULL DEFAULT '{}' ); CREATE INDEX audit_log_person ON audit_log (person_id, at); CREATE FUNCTION audit_log_append_only() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'audit_log is append-only'; END $$; CREATE TRIGGER audit_log_no_change BEFORE UPDATE OR DELETE ON audit_log FOR EACH ROW EXECUTE FUNCTION audit_log_append_only();