Skip to content
Clone
971c37c
101 lines 4.9 KB SQL
-- schema_pre_sync.sql
-- Drops partial unique indexes created by schema_extras.sql before every
-- sea-orm schema sync. The sync layer cannot express partial predicates via
-- entity attributes, and treats these manually-created unique indexes as
-- stale constraints (DROP CONSTRAINT), which fails because they are plain
-- indexes. schema_extras.sql recreates all of them after the sync succeeds.
DROP INDEX IF EXISTS idx_issue_watch_issue_user;
DROP INDEX IF EXISTS idx_issue_watch_project_user;
DROP INDEX IF EXISTS idx_issue_reaction_issue;
DROP INDEX IF EXISTS idx_issue_reaction_comment;
DROP INDEX IF EXISTS idx_issue_reaction_project;
DROP INDEX IF EXISTS idx_branch_protection_whitelist_user;
DROP INDEX IF EXISTS idx_branch_protection_whitelist_group;
DROP INDEX IF EXISTS idx_chat_message_room_nonce;
DROP INDEX IF EXISTS idx_chat_dm_pair;
DROP INDEX IF EXISTS idx_chat_gdm_key;
DROP INDEX IF EXISTS idx_chat_draft_unique;
DROP INDEX IF EXISTS idx_space_join_request_pending;
-- scheduled messages feature removed; drop the legacy table before sync
DROP TABLE IF EXISTS user_scheduled_message;
-- pipeline_run was replaced by workflow_run/workflow_job/workflow_step
DROP INDEX IF EXISTS idx_workflow_run_repo;
DROP INDEX IF EXISTS idx_workflow_run_concurrency;
DROP INDEX IF EXISTS idx_workflow_run_commit;
DROP INDEX IF EXISTS idx_workflow_job_claim;
DROP INDEX IF EXISTS idx_workflow_job_lease;
DROP INDEX IF EXISTS idx_workflow_step_job;
DROP INDEX IF EXISTS idx_ci_log_chunk_job;
DROP INDEX IF EXISTS idx_ci_secret_repo;
DROP INDEX IF EXISTS idx_ci_secret_space;
DROP INDEX IF EXISTS idx_ci_secret_environment;
DROP INDEX IF EXISTS idx_ci_environment_space;
DROP INDEX IF EXISTS idx_ci_deployment_environment;
DROP INDEX IF EXISTS idx_repo_webhook_delivery_due;
DROP INDEX IF EXISTS idx_ci_runner_state;
DROP TABLE IF EXISTS pipeline_run;
-- idempotent column adds for phase-2 model changes (sync layer cannot
-- express NOT NULL DEFAULT additions on populated tables).
-- Guarded with to_regclass: on a fresh database the tables do not exist yet
-- (they are created by the registry sync right after, with these columns
-- already included), so the ALTERs must be skipped instead of failing.
DO $$ BEGIN
IF to_regclass('room_members') IS NOT NULL THEN
ALTER TABLE room_members ADD COLUMN IF NOT EXISTS role SMALLINT NOT NULL DEFAULT 0;
END IF;
END $$;
DO $$ BEGIN
IF to_regclass('issue') IS NOT NULL THEN
ALTER TABLE issue ADD COLUMN IF NOT EXISTS priority SMALLINT NOT NULL DEFAULT 0;
ALTER TABLE issue ADD COLUMN IF NOT EXISTS due_at TIMESTAMP WITH TIME ZONE;
END IF;
END $$;
DO $$ BEGIN
IF to_regclass('user_access_key') IS NOT NULL THEN
ALTER TABLE user_access_key ADD COLUMN IF NOT EXISTS expires_at TIMESTAMP WITH TIME ZONE;
ALTER TABLE user_access_key ADD COLUMN IF NOT EXISTS last_used_at TIMESTAMP WITH TIME ZONE;
END IF;
END $$;
DO $$ BEGIN
IF to_regclass('chat_message_reminder') IS NOT NULL THEN
ALTER TABLE chat_message_reminder ADD COLUMN IF NOT EXISTS note TEXT;
END IF;
END $$;
-- pull_request domain (PR API): the sync layer cannot relax NOT NULL nor add
-- NOT NULL DEFAULT columns on populated tables, so both are applied here.
DO $$ BEGIN
IF to_regclass('pull_request') IS NOT NULL THEN
ALTER TABLE pull_request ALTER COLUMN issue_id DROP NOT NULL;
ALTER TABLE pull_request ADD COLUMN IF NOT EXISTS head_repo_id UUID;
ALTER TABLE pull_request ADD COLUMN IF NOT EXISTS head_label VARCHAR;
ALTER TABLE pull_request ADD COLUMN IF NOT EXISTS is_draft BOOLEAN NOT NULL DEFAULT false;
ALTER TABLE pull_request ADD COLUMN IF NOT EXISTS merge_method SMALLINT NOT NULL DEFAULT 0;
ALTER TABLE pull_request ADD COLUMN IF NOT EXISTS squash_commit_sha VARCHAR;
ALTER TABLE pull_request ADD COLUMN IF NOT EXISTS auto_merge BOOLEAN NOT NULL DEFAULT false;
END IF;
END $$;
DO $$ BEGIN
IF to_regclass('pull_request_comment') IS NOT NULL THEN
ALTER TABLE pull_request_comment ALTER COLUMN commit_sha DROP NOT NULL;
ALTER TABLE pull_request_comment ALTER COLUMN path DROP NOT NULL;
ALTER TABLE pull_request_comment ADD COLUMN IF NOT EXISTS parent_comment_id UUID;
ALTER TABLE pull_request_comment ADD COLUMN IF NOT EXISTS original_commit_sha VARCHAR;
ALTER TABLE pull_request_comment ADD COLUMN IF NOT EXISTS side VARCHAR;
ALTER TABLE pull_request_comment ADD COLUMN IF NOT EXISTS original_line INTEGER;
ALTER TABLE pull_request_comment ADD COLUMN IF NOT EXISTS resolved_at TIMESTAMP WITH TIME ZONE;
ALTER TABLE pull_request_comment ADD COLUMN IF NOT EXISTS resolved_by UUID;
END IF;
END $$;
DO $$ BEGIN
IF to_regclass('repo_branch_protection_policy') IS NOT NULL THEN
ALTER TABLE repo_branch_protection_policy
ADD COLUMN IF NOT EXISTS required_status_contexts VARCHAR NOT NULL DEFAULT '';
END IF;
END $$;