ALTER TABLE "tenant_export_jobs"
    ADD COLUMN IF NOT EXISTS "operation" text DEFAULT 'export' NOT NULL,
    ADD COLUMN IF NOT EXISTS "previous_tenant_locked" "locked_tenant";

ALTER TABLE "tenants"
    ADD COLUMN IF NOT EXISTS "locked_by_export_job_id" uuid;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM information_schema.table_constraints
        WHERE constraint_schema = 'public'
          AND constraint_name = 'tenants_locked_by_export_job_id_tenant_export_jobs_id_fk'
    ) THEN
        ALTER TABLE "tenants"
            ADD CONSTRAINT "tenants_locked_by_export_job_id_tenant_export_jobs_id_fk"
            FOREIGN KEY ("locked_by_export_job_id")
            REFERENCES "tenant_export_jobs"("id")
            ON DELETE SET NULL;
    END IF;
END $$;
