BEGIN;

-- 已产生业务员归属事实后禁止删列；应用回滚应保留字段并采用前向修复。
DO $$
BEGIN
    IF to_regclass('tb_shop') IS NULL
       OR NOT EXISTS (
           SELECT 1
           FROM pg_attribute
           WHERE attrelid = to_regclass('tb_shop')
             AND attname = 'business_owner_account_id'
             AND NOT attisdropped
       ) THEN
        RETURN;
    END IF;

    LOCK TABLE tb_shop IN ACCESS EXCLUSIVE MODE;
    IF EXISTS (SELECT 1 FROM tb_shop WHERE business_owner_account_id IS NOT NULL LIMIT 1) THEN
        RAISE EXCEPTION 'tb_shop 已存在业务员归属事实，禁止删列回滚，请保留字段并向前修复';
    END IF;

    DROP INDEX IF EXISTS idx_shop_business_owner_account_id;
    ALTER TABLE tb_shop DROP COLUMN business_owner_account_id;
END
$$;

COMMIT;
