-- 【管理员执行】数据库迁移脚本
-- 使用 postgres 或其他超级用户执行此脚本

-- ============================================================================
-- 【步骤 1】添加缺失的字段到 journal_entries 表
-- ============================================================================

-- 检查并添加 is_latest 字段
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'journal_entries' AND column_name = 'is_latest'
    ) THEN
        ALTER TABLE journal_entries ADD COLUMN is_latest BOOLEAN DEFAULT true;
        RAISE NOTICE '✓ 字段 is_latest 已添加';
    ELSE
        RAISE NOTICE '✓ 字段 is_latest 已存在';
    END IF;
END $$;

-- 检查并添加 revision_count 字段
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'journal_entries' AND column_name = 'revision_count'
    ) THEN
        ALTER TABLE journal_entries ADD COLUMN revision_count INTEGER DEFAULT 1;
        RAISE NOTICE '✓ 字段 revision_count 已添加';
    ELSE
        RAISE NOTICE '✓ 字段 revision_count 已存在';
    END IF;
END $$;

-- 检查并添加 original_entry_id 字段
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'journal_entries' AND column_name = 'original_entry_id'
    ) THEN
        ALTER TABLE journal_entries ADD COLUMN original_entry_id INTEGER;
        RAISE NOTICE '✓ 字段 original_entry_id 已添加';
    ELSE
        RAISE NOTICE '✓ 字段 original_entry_id 已存在';
    END IF;
END $$;

-- ============================================================================
-- 【步骤 2】添加外键约束（如果字段已添加）
-- ============================================================================

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.table_constraints 
        WHERE table_name = 'journal_entries' AND constraint_name = 'fk_original_entry_id'
    ) THEN
        ALTER TABLE journal_entries 
        ADD CONSTRAINT fk_original_entry_id 
        FOREIGN KEY (original_entry_id) 
        REFERENCES journal_entries(journal_entry_id) 
        ON DELETE CASCADE;
        RAISE NOTICE '✓ 外键约束已添加';
    ELSE
        RAISE NOTICE '✓ 外键约束已存在';
    END IF;
END $$;

-- ============================================================================
-- 【步骤 3】创建索引以优化查询
-- ============================================================================

CREATE INDEX IF NOT EXISTS idx_journal_entries_is_latest
ON journal_entries(is_latest, entry_date);

CREATE INDEX IF NOT EXISTS idx_journal_entries_original_entry_id
ON journal_entries(original_entry_id);

-- ============================================================================
-- 【步骤 4】验证迁移结果
-- ============================================================================

SELECT 'Migration Verification:' as status;

SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'journal_entries'
  AND column_name IN ('is_latest', 'revision_count', 'original_entry_id')
ORDER BY ordinal_position;

SELECT 'Indexes:' as status;

SELECT indexname
FROM pg_indexes
WHERE tablename = 'journal_entries'
AND (indexname LIKE 'idx_journal_entries_is_latest' OR indexname LIKE 'idx_journal_entries_original_entry_id');

SELECT '✓ 迁移完成！' as complete;
