-- ================================================================
-- 创建 metadata_version_history 表脚本
-- 用于存储元数据版本变更历史（PG审计表）
-- ================================================================

CREATE TABLE IF NOT EXISTS public.metadata_version_history (
    id BIGSERIAL PRIMARY KEY,
    meta_id BIGINT NOT NULL, -- Neo4j DataMeta 节点ID
    change_source VARCHAR(50) NOT NULL DEFAULT 'ddl',
    before_snapshot JSONB NOT NULL,
    after_snapshot JSONB NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    created_by VARCHAR(100)
);

CREATE INDEX IF NOT EXISTS idx_metadata_version_history_meta_id
    ON public.metadata_version_history(meta_id);
CREATE INDEX IF NOT EXISTS idx_metadata_version_history_created_at
    ON public.metadata_version_history(created_at DESC);

COMMENT ON TABLE public.metadata_version_history IS '元数据版本变更历史表（保存变更前后快照）';
COMMENT ON COLUMN public.metadata_version_history.meta_id IS 'Neo4j DataMeta 节点ID';
COMMENT ON COLUMN public.metadata_version_history.change_source IS '变更来源：ddl等';
COMMENT ON COLUMN public.metadata_version_history.before_snapshot IS '变更前快照（jsonb）';
COMMENT ON COLUMN public.metadata_version_history.after_snapshot IS '变更后快照（jsonb）';
COMMENT ON COLUMN public.metadata_version_history.created_by IS '创建人';


