const db = require("../db"); const { DEV_MOCK } = require("./env"); async function ensureScheduleStopProgressTable() { if (DEV_MOCK) return; await db.query(` CREATE TABLE IF NOT EXISTS schedule_stop_progress ( id BIGSERIAL PRIMARY KEY, schedule_id INTEGER NOT NULL, bus_line_id INTEGER NOT NULL, stop_id INTEGER NOT NULL, stop_order INTEGER NOT NULL, stop_name TEXT, estimated_arrival_minutes INTEGER NOT NULL DEFAULT 0, scheduled_passed_at TIMESTAMPTZ NOT NULL, scheduled_passed_date DATE NOT NULL, actual_passed_at TIMESTAMPTZ NOT NULL, delay_minutes INTEGER NOT NULL DEFAULT 0, marked_by_user_id INTEGER, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), UNIQUE (schedule_id, stop_order, scheduled_passed_date) ) `); await db.query(` ALTER TABLE schedule_stop_progress ADD COLUMN IF NOT EXISTS scheduled_passed_date DATE `); await db.query(` UPDATE schedule_stop_progress SET scheduled_passed_date = scheduled_passed_at::date WHERE scheduled_passed_date IS NULL `); await db.query(` ALTER TABLE schedule_stop_progress ALTER COLUMN scheduled_passed_date SET NOT NULL `); await db.query(` DO $$ DECLARE constraint_name text; BEGIN SELECT tc.constraint_name INTO constraint_name FROM information_schema.table_constraints tc JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema AND tc.table_name = kcu.table_name WHERE tc.table_schema = 'transit.bus' AND tc.table_name = 'schedule_stop_progress' AND tc.constraint_type = 'UNIQUE' GROUP BY tc.constraint_name HAVING array_agg(kcu.column_name ORDER BY kcu.ordinal_position)::text[] = ARRAY['schedule_id', 'stop_order']::text[] LIMIT 1; IF constraint_name IS NOT NULL THEN EXECUTE format('ALTER TABLE schedule_stop_progress DROP CONSTRAINT %I', constraint_name); END IF; END $$; `); await db.query(` CREATE UNIQUE INDEX IF NOT EXISTS idx_schedule_stop_progress_schedule_order_date ON schedule_stop_progress (schedule_id, stop_order, scheduled_passed_date) `); await db.query(` CREATE INDEX IF NOT EXISTS idx_schedule_stop_progress_schedule_order ON schedule_stop_progress (schedule_id, stop_order, scheduled_passed_date) `); } module.exports = { ensureScheduleStopProgressTable };