Refactor transit_api into a repo/routes module split
Split the 5,441-line index.js into an 86-line bootstrap, thin domain
route modules, shared lib/ helpers, and a repo/ layer that isolates the
DEV_MOCK branches (repo/*.mock.js). Repo methods return {statusCode, body};
routes are one-line wrappers. Pin search_path to "transit.bus" and
document the /bus/ prefix with legacy root aliases (Deprecation header).
Parity verified: 57/57 byte-identical on the stub differential harness
plus 24/24 live read-only responses vs the original against real
PostgreSQL. The only intentional behavior change: mock reorder/delete
bus-line endpoints no longer require a live DB (previously 500 in
DEV_MOCK=1 without PG). DEV_MOCK gate and node --check clean.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
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 };
|
||||
Reference in New Issue
Block a user