-- Durable control plane for outbound-only 1C polling. No clinical data is removed. ALTER TABLE onec_appointments ADD COLUMN patient_comment text NOT NULL DEFAULT ''; ALTER TABLE onec_appointments ADD COLUMN patient_birth_date text NOT NULL DEFAULT ''; CREATE INDEX onec_appointments_occupancy_idx ON onec_appointments(doctor_onec_id,start_at) WHERE status IN ('pending','booked','arrived'); CREATE TABLE onec_push_state ( singleton boolean PRIMARY KEY DEFAULT true CHECK(singleton), last_snapshot_at timestamptz, sync_requested boolean NOT NULL DEFAULT true ); INSERT INTO onec_push_state(singleton) VALUES(true); CREATE TABLE onec_commands ( id bigserial PRIMARY KEY, appointment_id bigint NOT NULL REFERENCES onec_appointments(id), kind text NOT NULL CHECK(kind IN ('cancel','link_bitrix','link_crm')), value text NOT NULL DEFAULT '', final_state text NOT NULL DEFAULT '', completed_at timestamptz, notification_done boolean NOT NULL DEFAULT false, last_error text NOT NULL DEFAULT '', created_at timestamptz NOT NULL DEFAULT now(), UNIQUE(appointment_id,kind) );