chore: perauset customer project
This commit is contained in:
558
db/sqlite/0001-schema.sql
Normal file
558
db/sqlite/0001-schema.sql
Normal file
@@ -0,0 +1,558 @@
|
||||
-- ============================================================
|
||||
-- Perauset schema — converted from Postgres to SQLite
|
||||
-- UUIDs → TEXT, BOOLEAN → INTEGER (0/1), JSON columns → TEXT,
|
||||
-- TIMESTAMPTZ/DATE → TEXT, enums → TEXT CHECK constraints,
|
||||
-- arrays → TEXT (JSON), no schemas, no triggers, no PL/pgSQL
|
||||
-- ============================================================
|
||||
|
||||
-- ============================================================
|
||||
-- Users (replaced by better-auth 0002 — defined here as
|
||||
-- a placeholder so FKs can be deferred until 0002 runs)
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user (
|
||||
id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
email TEXT NOT NULL UNIQUE,
|
||||
name TEXT,
|
||||
email_verified INTEGER NOT NULL DEFAULT 0,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- ============================================================
|
||||
-- User profile
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE user_profile (
|
||||
profile_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
user_id TEXT NOT NULL UNIQUE REFERENCES user(id) ON DELETE CASCADE,
|
||||
date_of_birth TEXT,
|
||||
nationality TEXT,
|
||||
notes TEXT,
|
||||
emergency_contact_name TEXT,
|
||||
emergency_contact_phone TEXT,
|
||||
mobile_number TEXT,
|
||||
whatsapp_number TEXT,
|
||||
avatar_url TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
-- ============================================================
|
||||
-- Audit log (no triggers in SQLite — app writes rows directly)
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE audit_log (
|
||||
audit_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
table_name TEXT NOT NULL,
|
||||
record_id TEXT NOT NULL,
|
||||
action TEXT NOT NULL,
|
||||
user_id TEXT REFERENCES user(id),
|
||||
changed_fields TEXT,
|
||||
occurred_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_audit_log_table_record ON audit_log(table_name, record_id);
|
||||
CREATE INDEX idx_audit_log_user ON audit_log(user_id);
|
||||
CREATE INDEX idx_audit_log_occurred ON audit_log(occurred_at);
|
||||
|
||||
-- ============================================================
|
||||
-- Stay requests
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE stay_request (
|
||||
request_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
user_id TEXT NOT NULL REFERENCES user(id),
|
||||
request_type TEXT NOT NULL DEFAULT 'guest'
|
||||
CHECK (request_type IN ('guest','volunteer','staff','facilitator','day_visit')),
|
||||
requested_start_at TEXT NOT NULL,
|
||||
requested_end_at TEXT NOT NULL,
|
||||
retreat_id TEXT,
|
||||
notes TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'submitted'
|
||||
CHECK (status IN ('submitted','under_review','approved','rejected','cancelled')),
|
||||
reviewed_by_user_id TEXT REFERENCES user(id),
|
||||
reviewed_at TEXT,
|
||||
review_notes TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_stay_request_user ON stay_request(user_id);
|
||||
CREATE INDEX idx_stay_request_status ON stay_request(status);
|
||||
|
||||
-- ============================================================
|
||||
-- Stays
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE stay (
|
||||
stay_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
user_id TEXT NOT NULL REFERENCES user(id),
|
||||
stay_type TEXT NOT NULL DEFAULT 'guest'
|
||||
CHECK (stay_type IN ('guest','volunteer','staff','facilitator','day_visitor')),
|
||||
source_request_id TEXT REFERENCES stay_request(request_id),
|
||||
retreat_id TEXT,
|
||||
start_at TEXT NOT NULL,
|
||||
end_at TEXT NOT NULL,
|
||||
status TEXT NOT NULL DEFAULT 'pending'
|
||||
CHECK (status IN ('pending','confirmed','checked_in','checked_out','cancelled')),
|
||||
checked_in_at TEXT,
|
||||
checked_out_at TEXT,
|
||||
created_by_user_id TEXT NOT NULL REFERENCES user(id),
|
||||
updated_by_user_id TEXT REFERENCES user(id),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_stay_user ON stay(user_id);
|
||||
CREATE INDEX idx_stay_status ON stay(status);
|
||||
CREATE INDEX idx_stay_dates ON stay(start_at, end_at);
|
||||
|
||||
-- ============================================================
|
||||
-- Rooms
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE room (
|
||||
room_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
code TEXT NOT NULL UNIQUE,
|
||||
name TEXT NOT NULL,
|
||||
room_type TEXT NOT NULL DEFAULT 'private'
|
||||
CHECK (room_type IN ('private','shared','dorm','facilitator','staff')),
|
||||
capacity INTEGER NOT NULL DEFAULT 1,
|
||||
is_active INTEGER NOT NULL DEFAULT 1,
|
||||
notes TEXT,
|
||||
price_per_night REAL,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE room_block (
|
||||
block_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
room_id TEXT NOT NULL REFERENCES room(room_id),
|
||||
block_type TEXT NOT NULL DEFAULT 'maintenance'
|
||||
CHECK (block_type IN ('maintenance','retreat_reserved','admin_hold','other')),
|
||||
retreat_id TEXT,
|
||||
starts_at TEXT NOT NULL,
|
||||
ends_at TEXT NOT NULL,
|
||||
reason TEXT,
|
||||
created_by_user_id TEXT NOT NULL REFERENCES user(id),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_room_block_room ON room_block(room_id);
|
||||
CREATE INDEX idx_room_block_dates ON room_block(starts_at, ends_at);
|
||||
|
||||
CREATE TABLE room_request (
|
||||
request_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
stay_id TEXT NOT NULL REFERENCES stay(stay_id),
|
||||
requested_room_type TEXT
|
||||
CHECK (requested_room_type IN ('private','shared','dorm','facilitator','staff')),
|
||||
preference_notes TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'submitted'
|
||||
CHECK (status IN ('submitted','reviewed','satisfied','unsatisfied','cancelled')),
|
||||
reviewed_by_user_id TEXT REFERENCES user(id),
|
||||
reviewed_at TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE room_allocation (
|
||||
allocation_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
stay_id TEXT NOT NULL REFERENCES stay(stay_id),
|
||||
room_id TEXT NOT NULL REFERENCES room(room_id),
|
||||
starts_at TEXT NOT NULL,
|
||||
ends_at TEXT NOT NULL,
|
||||
status TEXT NOT NULL DEFAULT 'planned'
|
||||
CHECK (status IN ('planned','active','completed','cancelled')),
|
||||
allocation_reason TEXT,
|
||||
created_by_user_id TEXT NOT NULL REFERENCES user(id),
|
||||
updated_by_user_id TEXT REFERENCES user(id),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_room_allocation_room ON room_allocation(room_id);
|
||||
CREATE INDEX idx_room_allocation_stay ON room_allocation(stay_id);
|
||||
CREATE INDEX idx_room_allocation_dates ON room_allocation(starts_at, ends_at);
|
||||
|
||||
-- ============================================================
|
||||
-- Boats
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE boat_route (
|
||||
route_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
name TEXT NOT NULL,
|
||||
origin TEXT NOT NULL,
|
||||
destination TEXT NOT NULL,
|
||||
is_active INTEGER NOT NULL DEFAULT 1,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE boat (
|
||||
boat_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
name TEXT NOT NULL,
|
||||
passenger_capacity INTEGER NOT NULL,
|
||||
cargo_capacity_kg REAL,
|
||||
is_active INTEGER NOT NULL DEFAULT 1,
|
||||
notes TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE boat_trip (
|
||||
trip_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
boat_id TEXT REFERENCES boat(boat_id),
|
||||
route_id TEXT NOT NULL REFERENCES boat_route(route_id),
|
||||
scheduled_departure_at TEXT NOT NULL,
|
||||
scheduled_arrival_at TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'planned'
|
||||
CHECK (status IN ('planned','open','full','closed','cancelled','completed')),
|
||||
notes TEXT,
|
||||
created_by_user_id TEXT NOT NULL REFERENCES user(id),
|
||||
updated_by_user_id TEXT REFERENCES user(id),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_boat_trip_status ON boat_trip(status);
|
||||
CREATE INDEX idx_boat_trip_departure ON boat_trip(scheduled_departure_at);
|
||||
|
||||
CREATE TABLE boat_reservation_request (
|
||||
request_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
trip_id TEXT NOT NULL REFERENCES boat_trip(trip_id),
|
||||
user_id TEXT NOT NULL REFERENCES user(id),
|
||||
stay_id TEXT REFERENCES stay(stay_id),
|
||||
retreat_id TEXT,
|
||||
requested_seats INTEGER NOT NULL DEFAULT 1,
|
||||
cargo_notes TEXT,
|
||||
special_requirements TEXT,
|
||||
priority_reason TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'submitted'
|
||||
CHECK (status IN ('submitted','under_review','confirmed','waitlisted','declined','cancelled')),
|
||||
reviewed_by_user_id TEXT REFERENCES user(id),
|
||||
reviewed_at TEXT,
|
||||
review_notes TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_boat_reservation_trip ON boat_reservation_request(trip_id);
|
||||
CREATE INDEX idx_boat_reservation_user ON boat_reservation_request(user_id);
|
||||
CREATE INDEX idx_boat_reservation_status ON boat_reservation_request(status);
|
||||
|
||||
CREATE TABLE boat_manifest_entry (
|
||||
entry_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
trip_id TEXT NOT NULL REFERENCES boat_trip(trip_id),
|
||||
reservation_request_id TEXT REFERENCES boat_reservation_request(request_id),
|
||||
user_id TEXT NOT NULL REFERENCES user(id),
|
||||
stay_id TEXT REFERENCES stay(stay_id),
|
||||
status TEXT NOT NULL DEFAULT 'confirmed'
|
||||
CHECK (status IN ('confirmed','boarded','no_show','cancelled')),
|
||||
confirmed_by_user_id TEXT NOT NULL REFERENCES user(id),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_boat_manifest_trip ON boat_manifest_entry(trip_id);
|
||||
|
||||
-- ============================================================
|
||||
-- Tasks
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE task_template (
|
||||
template_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
title TEXT NOT NULL,
|
||||
description TEXT,
|
||||
category TEXT NOT NULL DEFAULT 'operations'
|
||||
CHECK (category IN ('operations','maintenance','kitchen','retreat','transport','housekeeping')),
|
||||
is_claimable INTEGER NOT NULL DEFAULT 0,
|
||||
default_location TEXT,
|
||||
estimated_minutes INTEGER,
|
||||
recurrence_cron TEXT,
|
||||
is_active INTEGER NOT NULL DEFAULT 1,
|
||||
created_by_user_id TEXT NOT NULL REFERENCES user(id),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE task_instance (
|
||||
task_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
template_id TEXT REFERENCES task_template(template_id),
|
||||
title TEXT NOT NULL,
|
||||
description TEXT,
|
||||
category TEXT NOT NULL DEFAULT 'operations'
|
||||
CHECK (category IN ('operations','maintenance','kitchen','retreat','transport','housekeeping')),
|
||||
location TEXT,
|
||||
assigned_role TEXT,
|
||||
linked_retreat_id TEXT,
|
||||
linked_stay_id TEXT REFERENCES stay(stay_id),
|
||||
scheduled_start_at TEXT,
|
||||
scheduled_end_at TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'planned'
|
||||
CHECK (status IN ('planned','open','assigned','in_progress','blocked','completed','cancelled')),
|
||||
created_by_user_id TEXT NOT NULL REFERENCES user(id),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_task_instance_status ON task_instance(status);
|
||||
CREATE INDEX idx_task_instance_category ON task_instance(category);
|
||||
CREATE INDEX idx_task_instance_template ON task_instance(template_id);
|
||||
|
||||
CREATE TABLE task_assignment (
|
||||
assignment_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
task_instance_id TEXT NOT NULL REFERENCES task_instance(task_id),
|
||||
assigned_user_id TEXT NOT NULL REFERENCES user(id),
|
||||
assigned_by_user_id TEXT NOT NULL REFERENCES user(id),
|
||||
status TEXT NOT NULL DEFAULT 'proposed'
|
||||
CHECK (status IN ('proposed','accepted','declined','reassigned','completed')),
|
||||
notes TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_task_assignment_task ON task_assignment(task_instance_id);
|
||||
CREATE INDEX idx_task_assignment_user ON task_assignment(assigned_user_id);
|
||||
|
||||
-- ============================================================
|
||||
-- Notifications
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE notification (
|
||||
notification_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
user_id TEXT NOT NULL REFERENCES user(id),
|
||||
type TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
body TEXT,
|
||||
entity_type TEXT,
|
||||
entity_id TEXT,
|
||||
read_at TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_notification_user ON notification(user_id, read_at, created_at);
|
||||
|
||||
-- ============================================================
|
||||
-- Retreats
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE retreat (
|
||||
retreat_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
slug TEXT NOT NULL UNIQUE,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
start_at TEXT NOT NULL,
|
||||
end_at TEXT NOT NULL,
|
||||
status TEXT NOT NULL DEFAULT 'draft'
|
||||
CHECK (status IN ('draft','published','active','completed','cancelled')),
|
||||
capacity INTEGER,
|
||||
default_check_in_at TEXT,
|
||||
default_check_out_at TEXT,
|
||||
created_by_user_id TEXT NOT NULL REFERENCES user(id),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_retreat_status ON retreat(status);
|
||||
CREATE INDEX idx_retreat_dates ON retreat(start_at, end_at);
|
||||
|
||||
CREATE TABLE retreat_person (
|
||||
retreat_person_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
retreat_id TEXT NOT NULL REFERENCES retreat(retreat_id) ON DELETE CASCADE,
|
||||
user_id TEXT REFERENCES user(id),
|
||||
stay_id TEXT REFERENCES stay(stay_id),
|
||||
full_name TEXT,
|
||||
email TEXT,
|
||||
phone TEXT,
|
||||
role_type TEXT NOT NULL DEFAULT 'participant'
|
||||
CHECK (role_type IN ('participant','facilitator','assistant_facilitator','organizer','support_staff')),
|
||||
attendance_status TEXT NOT NULL DEFAULT 'registered'
|
||||
CHECK (attendance_status IN ('invited','registered','confirmed','checked_in','cancelled')),
|
||||
arrival_override_at TEXT,
|
||||
departure_override_at TEXT,
|
||||
notes TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
UNIQUE(retreat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_retreat_person_retreat ON retreat_person(retreat_id);
|
||||
CREATE INDEX idx_retreat_person_user ON retreat_person(user_id);
|
||||
|
||||
CREATE TABLE retreat_schedule_item (
|
||||
item_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
retreat_id TEXT NOT NULL REFERENCES retreat(retreat_id) ON DELETE CASCADE,
|
||||
title TEXT NOT NULL,
|
||||
description TEXT,
|
||||
starts_at TEXT NOT NULL,
|
||||
ends_at TEXT NOT NULL,
|
||||
location TEXT,
|
||||
lead_retreat_person_id TEXT REFERENCES retreat_person(retreat_person_id),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_retreat_schedule_retreat ON retreat_schedule_item(retreat_id);
|
||||
|
||||
-- ============================================================
|
||||
-- Kitchen / Dietary
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE dietary_profile (
|
||||
profile_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
user_id TEXT NOT NULL UNIQUE REFERENCES user(id) ON DELETE CASCADE,
|
||||
is_vegan INTEGER NOT NULL DEFAULT 0,
|
||||
is_vegetarian INTEGER NOT NULL DEFAULT 0,
|
||||
is_gluten_free INTEGER NOT NULL DEFAULT 0,
|
||||
is_dairy_free INTEGER NOT NULL DEFAULT 0,
|
||||
has_nut_allergy INTEGER NOT NULL DEFAULT 0,
|
||||
allergy_notes TEXT,
|
||||
other_requirements TEXT,
|
||||
dislikes TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE meal_service (
|
||||
service_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
service_type TEXT NOT NULL
|
||||
CHECK (service_type IN ('breakfast','lunch','dinner','snack')),
|
||||
service_date TEXT NOT NULL,
|
||||
retreat_id TEXT REFERENCES retreat(retreat_id),
|
||||
planned_headcount INTEGER,
|
||||
menu_notes TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'planned'
|
||||
CHECK (status IN ('planned','ready','served','completed')),
|
||||
created_by_user_id TEXT NOT NULL REFERENCES user(id),
|
||||
updated_by_user_id TEXT REFERENCES user(id),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_meal_service_date ON meal_service(service_date);
|
||||
CREATE INDEX idx_meal_service_type ON meal_service(service_type);
|
||||
CREATE INDEX idx_meal_service_retreat ON meal_service(retreat_id);
|
||||
|
||||
CREATE TABLE meal_attendance (
|
||||
attendance_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
meal_service_id TEXT NOT NULL REFERENCES meal_service(service_id) ON DELETE CASCADE,
|
||||
user_id TEXT NOT NULL REFERENCES user(id),
|
||||
stay_id TEXT REFERENCES stay(stay_id),
|
||||
source_type TEXT NOT NULL DEFAULT 'manual'
|
||||
CHECK (source_type IN ('stay','retreat','manual')),
|
||||
status TEXT NOT NULL DEFAULT 'expected'
|
||||
CHECK (status IN ('expected','confirmed','cancelled','served')),
|
||||
notes TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
UNIQUE(meal_service_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_meal_attendance_service ON meal_attendance(meal_service_id);
|
||||
CREATE INDEX idx_meal_attendance_user ON meal_attendance(user_id);
|
||||
|
||||
-- ============================================================
|
||||
-- Inventory
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE inventory_item (
|
||||
item_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
name TEXT NOT NULL,
|
||||
unit TEXT NOT NULL DEFAULT 'unit',
|
||||
current_quantity REAL NOT NULL DEFAULT 0,
|
||||
minimum_quantity REAL NOT NULL DEFAULT 0,
|
||||
is_active INTEGER NOT NULL DEFAULT 1,
|
||||
category TEXT NOT NULL DEFAULT 'operations',
|
||||
subcategory TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_inventory_item_active ON inventory_item(is_active);
|
||||
CREATE INDEX idx_inventory_item_category ON inventory_item(category);
|
||||
|
||||
CREATE TABLE inventory_request (
|
||||
request_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
requested_by_user_id TEXT NOT NULL REFERENCES user(id),
|
||||
item_id TEXT REFERENCES inventory_item(item_id),
|
||||
item_name TEXT NOT NULL,
|
||||
quantity REAL NOT NULL,
|
||||
unit TEXT NOT NULL,
|
||||
purpose TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'submitted'
|
||||
CHECK (status IN ('submitted','approved','rejected','fulfilled','cancelled')),
|
||||
reviewed_by_user_id TEXT REFERENCES user(id),
|
||||
reviewed_at TEXT,
|
||||
review_notes TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_inventory_request_status ON inventory_request(status);
|
||||
CREATE INDEX idx_inventory_request_user ON inventory_request(requested_by_user_id);
|
||||
|
||||
CREATE TABLE inventory_transaction (
|
||||
transaction_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
item_id TEXT NOT NULL REFERENCES inventory_item(item_id),
|
||||
quantity_change REAL NOT NULL,
|
||||
reason TEXT NOT NULL,
|
||||
notes TEXT,
|
||||
created_by_user_id TEXT NOT NULL REFERENCES user(id),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_inventory_transaction_item ON inventory_transaction(item_id);
|
||||
CREATE INDEX idx_inventory_transaction_created ON inventory_transaction(created_at);
|
||||
|
||||
-- ============================================================
|
||||
-- Finance
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE finance_record (
|
||||
finance_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
amount REAL NOT NULL,
|
||||
currency TEXT NOT NULL DEFAULT 'EUR',
|
||||
record_type TEXT NOT NULL DEFAULT 'expense',
|
||||
amount_egp REAL,
|
||||
purchased_by_user_id TEXT REFERENCES user(id),
|
||||
purchased_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
category TEXT NOT NULL DEFAULT 'operations',
|
||||
created_by_user_id TEXT NOT NULL REFERENCES user(id),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_finance_record_category ON finance_record(category);
|
||||
CREATE INDEX idx_finance_record_purchased_at ON finance_record(purchased_at);
|
||||
CREATE INDEX idx_finance_record_purchased_by ON finance_record(purchased_by_user_id);
|
||||
|
||||
CREATE TABLE finance_link (
|
||||
link_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
finance_id TEXT NOT NULL REFERENCES finance_record(finance_id) ON DELETE CASCADE,
|
||||
entity_type TEXT NOT NULL,
|
||||
entity_id TEXT NOT NULL,
|
||||
notes TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_finance_link_finance ON finance_link(finance_id);
|
||||
CREATE INDEX idx_finance_link_entity ON finance_link(entity_type, entity_id);
|
||||
|
||||
-- ============================================================
|
||||
-- Exchange rates
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE exchange_rate (
|
||||
rate_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)),2) || '-' || substr('AB89',1+(abs(random())%4),1) || substr(hex(randomblob(2)),2) || '-' || hex(randomblob(6)))),
|
||||
base_currency TEXT NOT NULL DEFAULT 'EGP',
|
||||
target_currency TEXT NOT NULL,
|
||||
rate REAL NOT NULL,
|
||||
fetched_at TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX idx_exchange_rate_unique ON exchange_rate(base_currency, target_currency, fetched_at);
|
||||
CREATE INDEX idx_exchange_rate_date ON exchange_rate(fetched_at);
|
||||
66
db/sqlite/0002-better-auth.sql
Normal file
66
db/sqlite/0002-better-auth.sql
Normal file
@@ -0,0 +1,66 @@
|
||||
-- Drop the placeholder user table from 0001 and replace with better-auth's schema
|
||||
DROP TABLE IF EXISTS user;
|
||||
|
||||
-- better-auth user table with app-specific additionalFields
|
||||
CREATE TABLE user (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
email TEXT NOT NULL UNIQUE,
|
||||
email_verified INTEGER NOT NULL DEFAULT 0,
|
||||
image TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
-- additionalFields
|
||||
roles TEXT NOT NULL DEFAULT '[]',
|
||||
display_name TEXT,
|
||||
mobile_number TEXT,
|
||||
whatsapp_number TEXT,
|
||||
avatar_url TEXT
|
||||
);
|
||||
|
||||
-- better-auth session table
|
||||
CREATE TABLE session (
|
||||
id TEXT PRIMARY KEY,
|
||||
expires_at TEXT NOT NULL,
|
||||
token TEXT NOT NULL UNIQUE,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
ip_address TEXT,
|
||||
user_agent TEXT,
|
||||
user_id TEXT NOT NULL REFERENCES user(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX idx_session_token ON session(token);
|
||||
CREATE INDEX idx_session_user ON session(user_id);
|
||||
|
||||
-- better-auth account table
|
||||
CREATE TABLE account (
|
||||
id TEXT PRIMARY KEY,
|
||||
account_id TEXT NOT NULL,
|
||||
provider_id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL REFERENCES user(id) ON DELETE CASCADE,
|
||||
access_token TEXT,
|
||||
refresh_token TEXT,
|
||||
id_token TEXT,
|
||||
access_token_expires_at TEXT,
|
||||
refresh_token_expires_at TEXT,
|
||||
scope TEXT,
|
||||
password TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_account_user ON account(user_id);
|
||||
CREATE UNIQUE INDEX idx_account_provider ON account(provider_id, account_id);
|
||||
|
||||
-- better-auth verification table
|
||||
CREATE TABLE verification (
|
||||
id TEXT PRIMARY KEY,
|
||||
identifier TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
expires_at TEXT NOT NULL,
|
||||
created_at TEXT DEFAULT (datetime('now')),
|
||||
updated_at TEXT DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_verification_identifier ON verification(identifier);
|
||||
433
db/sqlite/0003-audit-triggers.sql
Normal file
433
db/sqlite/0003-audit-triggers.sql
Normal file
@@ -0,0 +1,433 @@
|
||||
-- Audit logging for SQLite (libsql/better-sqlite3).
|
||||
--
|
||||
-- Postgres used a generic trigger function + a per-request session variable
|
||||
-- (set_config) to attribute audit rows. SQLite has neither, so we emulate it:
|
||||
-- * audit_ctx holds the current user id for the active connection (set by the
|
||||
-- app via setAuditContext() before each mutation).
|
||||
-- * Per-table AFTER INSERT/UPDATE/DELETE triggers write audit_log rows,
|
||||
-- reading the actor from audit_ctx.
|
||||
--
|
||||
-- NOTE: audit_ctx is connection-global. Attribution is correct when mutations
|
||||
-- run serially per connection (the better-sqlite3 case); under heavy
|
||||
-- interleaved concurrency the actor could be misattributed.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS audit_ctx (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
user_id TEXT
|
||||
);
|
||||
INSERT OR IGNORE INTO audit_ctx (id, user_id) VALUES (1, NULL);
|
||||
|
||||
CREATE TRIGGER audit_user_insert AFTER INSERT ON user
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'user', NEW.id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_user_update AFTER UPDATE ON user
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'user', NEW.id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_user_delete AFTER DELETE ON user
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'user', OLD.id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_stay_request_insert AFTER INSERT ON stay_request
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'stay_request', NEW.request_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_stay_request_update AFTER UPDATE ON stay_request
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'stay_request', NEW.request_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_stay_request_delete AFTER DELETE ON stay_request
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'stay_request', OLD.request_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_stay_insert AFTER INSERT ON stay
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'stay', NEW.stay_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_stay_update AFTER UPDATE ON stay
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'stay', NEW.stay_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_stay_delete AFTER DELETE ON stay
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'stay', OLD.stay_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_route_insert AFTER INSERT ON boat_route
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat_route', NEW.route_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_route_update AFTER UPDATE ON boat_route
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat_route', NEW.route_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_route_delete AFTER DELETE ON boat_route
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat_route', OLD.route_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_insert AFTER INSERT ON boat
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat', NEW.boat_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_update AFTER UPDATE ON boat
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat', NEW.boat_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_delete AFTER DELETE ON boat
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat', OLD.boat_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_trip_insert AFTER INSERT ON boat_trip
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat_trip', NEW.trip_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_trip_update AFTER UPDATE ON boat_trip
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat_trip', NEW.trip_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_trip_delete AFTER DELETE ON boat_trip
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat_trip', OLD.trip_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_reservation_request_insert AFTER INSERT ON boat_reservation_request
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat_reservation_request', NEW.request_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_reservation_request_update AFTER UPDATE ON boat_reservation_request
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat_reservation_request', NEW.request_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_reservation_request_delete AFTER DELETE ON boat_reservation_request
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat_reservation_request', OLD.request_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_manifest_entry_insert AFTER INSERT ON boat_manifest_entry
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat_manifest_entry', NEW.entry_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_manifest_entry_update AFTER UPDATE ON boat_manifest_entry
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat_manifest_entry', NEW.entry_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_boat_manifest_entry_delete AFTER DELETE ON boat_manifest_entry
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'boat_manifest_entry', OLD.entry_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_room_insert AFTER INSERT ON room
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'room', NEW.room_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_room_update AFTER UPDATE ON room
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'room', NEW.room_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_room_delete AFTER DELETE ON room
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'room', OLD.room_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_room_block_insert AFTER INSERT ON room_block
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'room_block', NEW.block_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_room_block_update AFTER UPDATE ON room_block
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'room_block', NEW.block_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_room_block_delete AFTER DELETE ON room_block
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'room_block', OLD.block_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_room_request_insert AFTER INSERT ON room_request
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'room_request', NEW.request_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_room_request_update AFTER UPDATE ON room_request
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'room_request', NEW.request_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_room_request_delete AFTER DELETE ON room_request
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'room_request', OLD.request_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_room_allocation_insert AFTER INSERT ON room_allocation
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'room_allocation', NEW.allocation_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_room_allocation_update AFTER UPDATE ON room_allocation
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'room_allocation', NEW.allocation_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_room_allocation_delete AFTER DELETE ON room_allocation
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'room_allocation', OLD.allocation_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_dietary_profile_insert AFTER INSERT ON dietary_profile
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'dietary_profile', NEW.profile_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_dietary_profile_update AFTER UPDATE ON dietary_profile
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'dietary_profile', NEW.profile_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_dietary_profile_delete AFTER DELETE ON dietary_profile
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'dietary_profile', OLD.profile_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_meal_service_insert AFTER INSERT ON meal_service
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'meal_service', NEW.service_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_meal_service_update AFTER UPDATE ON meal_service
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'meal_service', NEW.service_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_meal_service_delete AFTER DELETE ON meal_service
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'meal_service', OLD.service_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_meal_attendance_insert AFTER INSERT ON meal_attendance
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'meal_attendance', NEW.attendance_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_meal_attendance_update AFTER UPDATE ON meal_attendance
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'meal_attendance', NEW.attendance_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_meal_attendance_delete AFTER DELETE ON meal_attendance
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'meal_attendance', OLD.attendance_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_retreat_insert AFTER INSERT ON retreat
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'retreat', NEW.retreat_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_retreat_update AFTER UPDATE ON retreat
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'retreat', NEW.retreat_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_retreat_delete AFTER DELETE ON retreat
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'retreat', OLD.retreat_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_retreat_person_insert AFTER INSERT ON retreat_person
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'retreat_person', NEW.retreat_person_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_retreat_person_update AFTER UPDATE ON retreat_person
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'retreat_person', NEW.retreat_person_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_retreat_person_delete AFTER DELETE ON retreat_person
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'retreat_person', OLD.retreat_person_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_retreat_schedule_item_insert AFTER INSERT ON retreat_schedule_item
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'retreat_schedule_item', NEW.item_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_retreat_schedule_item_update AFTER UPDATE ON retreat_schedule_item
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'retreat_schedule_item', NEW.item_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_retreat_schedule_item_delete AFTER DELETE ON retreat_schedule_item
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'retreat_schedule_item', OLD.item_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_task_template_insert AFTER INSERT ON task_template
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'task_template', NEW.template_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_task_template_update AFTER UPDATE ON task_template
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'task_template', NEW.template_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_task_template_delete AFTER DELETE ON task_template
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'task_template', OLD.template_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_task_instance_insert AFTER INSERT ON task_instance
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'task_instance', NEW.task_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_task_instance_update AFTER UPDATE ON task_instance
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'task_instance', NEW.task_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_task_instance_delete AFTER DELETE ON task_instance
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'task_instance', OLD.task_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_task_assignment_insert AFTER INSERT ON task_assignment
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'task_assignment', NEW.assignment_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_task_assignment_update AFTER UPDATE ON task_assignment
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'task_assignment', NEW.assignment_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_task_assignment_delete AFTER DELETE ON task_assignment
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'task_assignment', OLD.assignment_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_inventory_item_insert AFTER INSERT ON inventory_item
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'inventory_item', NEW.item_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_inventory_item_update AFTER UPDATE ON inventory_item
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'inventory_item', NEW.item_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_inventory_item_delete AFTER DELETE ON inventory_item
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'inventory_item', OLD.item_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_inventory_request_insert AFTER INSERT ON inventory_request
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'inventory_request', NEW.request_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_inventory_request_update AFTER UPDATE ON inventory_request
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'inventory_request', NEW.request_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER audit_inventory_request_delete AFTER DELETE ON inventory_request
|
||||
BEGIN
|
||||
INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
|
||||
VALUES (lower(hex(randomblob(16))), 'inventory_request', OLD.request_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
|
||||
END;
|
||||
|
||||
29
db/sqlite/0004-fabric-audit.sql
Normal file
29
db/sqlite/0004-fabric-audit.sql
Normal file
@@ -0,0 +1,29 @@
|
||||
-- ============================================================
|
||||
-- Fabric audit event sink — the table Pikku Fabric writes AuditEvent
|
||||
-- records to. Distinct from the domain `audit_log` (row-change history
|
||||
-- in 0001/0003): this is the framework-level event stream.
|
||||
-- Columns mirror the AuditEvent shape (camelCase → snake_case for the
|
||||
-- CamelCasePlugin kysely).
|
||||
-- ============================================================
|
||||
|
||||
CREATE TABLE audit (
|
||||
event_id TEXT PRIMARY KEY,
|
||||
type TEXT NOT NULL,
|
||||
source TEXT,
|
||||
outcome TEXT,
|
||||
occurred_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
function_id TEXT,
|
||||
wire_type TEXT,
|
||||
wire_id TEXT,
|
||||
trace_id TEXT,
|
||||
transaction_id TEXT,
|
||||
query_id TEXT,
|
||||
actor TEXT,
|
||||
input TEXT,
|
||||
metadata TEXT
|
||||
);
|
||||
|
||||
CREATE INDEX idx_audit_type ON audit(type);
|
||||
CREATE INDEX idx_audit_occurred ON audit(occurred_at);
|
||||
CREATE INDEX idx_audit_function ON audit(function_id);
|
||||
CREATE INDEX idx_audit_trace ON audit(trace_id);
|
||||
13
db/sqlite/0005-member-roles-and-admin.sql
Normal file
13
db/sqlite/0005-member-roles-and-admin.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
-- Rename the app RBAC field so it is never confused with the better-auth admin
|
||||
-- plugin's `role`. `member_roles` holds a user's assigned community roles
|
||||
-- (coordinator, facilitator, …); the admin plugin's `role` is platform-level.
|
||||
ALTER TABLE user RENAME COLUMN roles TO member_roles;
|
||||
|
||||
-- better-auth admin plugin schema. `role` gates the admin/impersonation
|
||||
-- endpoints (default 'user'); ban_* back the ban feature; session.impersonated_by
|
||||
-- records the admin's id while a session is an impersonation.
|
||||
ALTER TABLE user ADD COLUMN role TEXT NOT NULL DEFAULT 'user';
|
||||
ALTER TABLE user ADD COLUMN banned INTEGER NOT NULL DEFAULT 0;
|
||||
ALTER TABLE user ADD COLUMN ban_reason TEXT;
|
||||
ALTER TABLE user ADD COLUMN ban_expires TEXT;
|
||||
ALTER TABLE session ADD COLUMN impersonated_by TEXT;
|
||||
11
db/sqlite/0006-user-actor.sql
Normal file
11
db/sqlite/0006-user-actor.sql
Normal file
@@ -0,0 +1,11 @@
|
||||
-- =============================================================================
|
||||
-- BETTER AUTH ACTOR PLUGIN — user-flow actors
|
||||
-- Adds the `actor` boolean the Better Auth `actor()` plugin declares so its
|
||||
-- schema is covered by an explicit migration and the db drift check passes.
|
||||
-- Actor rows are synthetic users signed in by pikkuUserFlow via
|
||||
-- POST /api/auth/sign-in/actor with the server-held USER_FLOW_ACTOR_SECRET;
|
||||
-- the flag rides into the pikku core session so audits/analytics can address
|
||||
-- synthetic traffic. A non-actor user can never be signed in this way.
|
||||
-- =============================================================================
|
||||
|
||||
ALTER TABLE user ADD COLUMN actor integer NOT NULL DEFAULT 0;
|
||||
13
db/sqlite/0007-fabric.sql
Normal file
13
db/sqlite/0007-fabric.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
-- =============================================================================
|
||||
-- BETTER AUTH FABRIC PLUGIN — Fabric operator admin sessions
|
||||
-- Adds the `fabric` boolean the Better Auth `fabric()` plugin declares so its
|
||||
-- schema is covered by an explicit migration and the db drift check passes.
|
||||
-- Fabric rows are synthetic operator users minted by POST /api/auth/sign-in/fabric
|
||||
-- after verifying a short-lived RS256 token the Fabric control plane signed
|
||||
-- (checked against FABRIC_AUTH_PUBLIC_KEY). They are created with role 'admin' so
|
||||
-- the console Users tab can list/impersonate real end-users WITHOUT the operator
|
||||
-- being one of them; these rows are filtered out of any end-user listing. A real
|
||||
-- user can never be signed in this way.
|
||||
-- =============================================================================
|
||||
|
||||
ALTER TABLE user ADD COLUMN fabric integer NOT NULL DEFAULT 0;
|
||||
Reference in New Issue
Block a user