-- Durable customer-machine links; no changes to existing sessions during migration. CREATE TABLE IF NOT EXISTS anydesk_machine_links ( anydesk_id VARCHAR(64) PRIMARY KEY CHECK (anydesk_id ~ '^[0-9]+$'), customer_id INTEGER NOT NULL REFERENCES customers(id) ON DELETE CASCADE, contact_id INTEGER REFERENCES contacts(id) ON DELETE SET NULL, source_session_id INTEGER REFERENCES anydesk_sessions(id) ON DELETE SET NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE INDEX IF NOT EXISTS idx_anydesk_machine_links_customer ON anydesk_machine_links(customer_id);