-- =============================================================================
-- FDW 反向映射配置（测试环境）— 在 test_etl_feiqiu 数据库中执行
-- 用途：通过 postgres_fdw 将 test_zqyy_app.member_retention_clue 只读映射到
--       test_etl_feiqiu，使 ETL DWS 任务在测试环境下可读取维客线索数据。
-- 方向：test_etl_feiqiu → test_zqyy_app（与 setup_fdw_test.sql 方向相反）
-- 前提：test_zqyy_app 数据库已部署 member_retention_clue 表
-- 基于 setup_fdw_reverse.sql，仅将目标库替换为测试库
-- CHANGE 2026-02-26 | member_birthday_manual → member_retention_clue（维客线索重构）
-- =============================================================================

-- -----------------------------------------------------------------------------
-- 1. 安装 postgres_fdw 扩展（如已安装则跳过）
-- -----------------------------------------------------------------------------
CREATE EXTENSION IF NOT EXISTS postgres_fdw;

-- -----------------------------------------------------------------------------
-- 2. 创建外部服务器（指向 test_zqyy_app 测试业务库）
-- -----------------------------------------------------------------------------
CREATE SERVER IF NOT EXISTS test_zqyy_app_server
    FOREIGN DATA WRAPPER postgres_fdw
    OPTIONS (host 'localhost', dbname 'test_zqyy_app', port '5432');

-- -----------------------------------------------------------------------------
-- 3. 创建用户映射（只读角色）
-- -----------------------------------------------------------------------------
CREATE USER MAPPING IF NOT EXISTS FOR etl_user
    SERVER test_zqyy_app_server
    OPTIONS (user 'app_reader', password '***');

-- -----------------------------------------------------------------------------
-- 4. 创建目标 schema（存放来自业务库的外部表）
-- -----------------------------------------------------------------------------
CREATE SCHEMA IF NOT EXISTS fdw_app;

-- -----------------------------------------------------------------------------
-- 5. 创建外部表：member_retention_clue
--    映射 test_zqyy_app.public.member_retention_clue，ETL 侧只读
-- -----------------------------------------------------------------------------
CREATE FOREIGN TABLE IF NOT EXISTS fdw_app.member_retention_clue (
    id                       BIGINT,
    member_id                BIGINT,
    category                 VARCHAR(20),
    summary                  VARCHAR(200),
    detail                   TEXT,
    recorded_by_assistant_id BIGINT,
    recorded_by_name         VARCHAR(50),
    recorded_at              TIMESTAMPTZ,
    site_id                  BIGINT
) SERVER test_zqyy_app_server
  OPTIONS (schema_name 'public', table_name 'member_retention_clue');

-- -----------------------------------------------------------------------------
-- 6. 授权
-- -----------------------------------------------------------------------------
GRANT USAGE ON SCHEMA fdw_app TO etl_user;
GRANT SELECT ON ALL TABLES IN SCHEMA fdw_app TO etl_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA fdw_app GRANT SELECT ON TABLES TO etl_user;


-- =============================================================================
-- 回滚脚本（按逆序执行）
-- =============================================================================
-- ALTER DEFAULT PRIVILEGES IN SCHEMA fdw_app REVOKE SELECT ON TABLES FROM etl_user;
-- REVOKE SELECT ON ALL TABLES IN SCHEMA fdw_app FROM etl_user;
-- REVOKE USAGE ON SCHEMA fdw_app FROM etl_user;
-- DROP FOREIGN TABLE IF EXISTS fdw_app.member_retention_clue;
-- DROP SCHEMA IF EXISTS fdw_app CASCADE;
-- DROP USER MAPPING IF EXISTS FOR etl_user SERVER test_zqyy_app_server;
-- DROP SERVER IF EXISTS test_zqyy_app_server CASCADE;


-- =============================================================================
-- 验证 SQL
-- =============================================================================
-- 1. 确认外部服务器存在
-- SELECT srvname, srvoptions FROM pg_foreign_server
--  WHERE srvname = 'test_zqyy_app_server';

-- 2. 确认外部表列结构完整（9 列）
-- SELECT column_name, data_type FROM information_schema.columns
--  WHERE table_schema = 'fdw_app' AND table_name = 'member_retention_clue'
--  ORDER BY ordinal_position;

-- 3. 确认外部表可读取
-- SELECT COUNT(*) FROM fdw_app.member_retention_clue;
