Ver Fonte

chore(sql): 完善 rebuild_all.sh 数据库重建脚本

新增功能:
- --drop-only: 只删除表,不初始化
- --init-only: 只初始化,不删除(用于全新数据库)
- --list-migrations: 列出所有迁移脚本

改进:
- 添加进度提示 [STEP] 显示当前执行阶段
- 迁移脚本执行显示 [n/total] 进度
- 更新帮助文档,列出所有迁移脚本
- 添加更新日志
何文松 há 1 mês atrás
pai
commit
70d601e27a
1 ficheiros alterados com 114 adições e 9 exclusões
  1. 114 9
      backend/sql/rebuild_all.sh

+ 114 - 9
backend/sql/rebuild_all.sh

@@ -3,6 +3,10 @@
 # ============================================
 # 数据库完整重建脚本
 # 删除所有表并重新初始化(包含所有迁移)
+# 
+# 更新日志:
+#   2026-01-23: 添加 --drop-only, --init-only, --list-migrations 选项
+#   2026-01-22: 添加 extract_tables 迁移支持
 # ============================================
 
 set -e
@@ -12,6 +16,7 @@ RED='\033[0;31m'
 GREEN='\033[0;32m'
 YELLOW='\033[1;33m'
 BLUE='\033[0;34m'
+CYAN='\033[0;36m'
 NC='\033[0m'
 
 # 数据库配置(根据实际情况修改)
@@ -25,6 +30,7 @@ log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
 log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
 log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
 log_title() { echo -e "\n${BLUE}========== $1 ==========${NC}\n"; }
+log_step() { echo -e "${CYAN}[STEP]${NC} $1"; }
 
 # 获取脚本所在目录
 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -115,30 +121,69 @@ execute_sql() {
     fi
 }
 
+# 列出迁移脚本
+list_migrations() {
+    log_title "迁移脚本列表"
+    
+    if [ -d "$MIGRATIONS_DIR" ]; then
+        local count=0
+        echo ""
+        printf "  %-5s %-60s %s\n" "序号" "文件名" "大小"
+        echo "  -------------------------------------------------------------------------"
+        for migration in $(ls -1 "${MIGRATIONS_DIR}"/*.sql 2>/dev/null | sort); do
+            if [ -f "$migration" ]; then
+                count=$((count + 1))
+                local size=$(du -h "$migration" | cut -f1)
+                printf "  %-5s %-60s %s\n" "$count" "$(basename $migration)" "$size"
+            fi
+        done
+        echo ""
+        log_info "共 ${count} 个迁移脚本"
+    else
+        log_warn "迁移目录不存在: ${MIGRATIONS_DIR}"
+    fi
+}
+
 # 初始化所有表
 init_all_tables() {
     log_title "初始化数据库表"
     
+    local step=1
+    
     # 1. 基础表(users, documents 等核心表)
+    log_step "[$step/5] 基础表"
     execute_sql "${SQL_DIR}/init.sql" "基础表 (users, documents, elements, etc.)"
+    step=$((step + 1))
     
     # 2. 图谱表(graph_nodes, graph_relations - 先于 supplement_tables)
+    log_step "[$step/5] 图谱表"
     execute_sql "${SQL_DIR}/graph_tables.sql" "图谱表 (graph_nodes, graph_relations)"
+    step=$((step + 1))
     
     # 3. 补充表(rules, data_sources, templates, text_storage 等)
+    log_step "[$step/5] 补充表"
     execute_sql "${SQL_DIR}/supplement_tables.sql" "补充表 (rules, data_sources, templates, text_storage)"
+    step=$((step + 1))
     
     # 4. RAG 表(text_chunks, vector_embeddings - 可能需要 pgvector)
+    log_step "[$step/5] RAG 表"
     execute_sql "${SQL_DIR}/rag_tables_compatible.sql" "RAG 表 (text_chunks, vector_embeddings)"
+    step=$((step + 1))
     
     # 5. 执行迁移文件(按文件名排序)
+    log_step "[$step/5] 迁移脚本"
     log_title "执行迁移脚本"
     if [ -d "$MIGRATIONS_DIR" ]; then
+        local migration_count=0
+        local total_migrations=$(ls -1 "${MIGRATIONS_DIR}"/*.sql 2>/dev/null | wc -l)
+        
         for migration in $(ls -1 "${MIGRATIONS_DIR}"/*.sql 2>/dev/null | sort); do
             if [ -f "$migration" ]; then
-                execute_sql "$migration" "迁移: $(basename $migration)"
+                migration_count=$((migration_count + 1))
+                execute_sql "$migration" "[$migration_count/$total_migrations] $(basename $migration)"
             fi
         done
+        log_info "共执行 ${migration_count} 个迁移脚本"
     else
         log_warn "迁移目录不存在: ${MIGRATIONS_DIR}"
     fi
@@ -205,9 +250,12 @@ show_help() {
 用法: ./rebuild_all.sh [选项]
 
 选项:
-    -h, --help      显示帮助
-    -f, --force     跳过确认提示
-    --no-test-user  不创建测试用户
+    -h, --help          显示帮助
+    -f, --force         跳过确认提示
+    --no-test-user      不创建测试用户
+    --drop-only         只删除表,不初始化
+    --init-only         只初始化,不删除(用于全新数据库)
+    --list-migrations   列出所有迁移脚本后退出
 
 环境变量:
     DB_HOST         数据库主机 (默认: localhost)
@@ -217,14 +265,23 @@ show_help() {
     DB_PASSWORD     数据库密码 (默认: postgres)
 
 示例:
-    # 使用默认配置
+    # 使用默认配置(删除 + 重建)
     ./rebuild_all.sh
 
     # 指定数据库配置
     DB_PASSWORD=mypassword ./rebuild_all.sh
 
     # 强制执行(跳过确认)
-    FORCE=yes ./rebuild_all.sh
+    ./rebuild_all.sh -f
+
+    # 只删除所有表
+    ./rebuild_all.sh --drop-only -f
+
+    # 初始化全新数据库(不删除)
+    ./rebuild_all.sh --init-only
+
+    # 查看迁移脚本列表
+    ./rebuild_all.sh --list-migrations
 
     # 服务器上执行
     DB_USER=lingyue DB_PASSWORD=123123 ./rebuild_all.sh
@@ -232,10 +289,16 @@ show_help() {
 执行顺序:
     1. 删除所有表、序列、函数
     2. 执行 init.sql(用户、文档、元素等基础表)
-    3. 执行 supplement_tables.sql(补充表)
-    4. 执行 graph_tables.sql(图谱表)
+    3. 执行 graph_tables.sql(图谱表)
+    4. 执行 supplement_tables.sql(补充表)
     5. 执行 rag_tables_compatible.sql(RAG表)
-    6. 执行 database/migrations/*.sql(迁移脚本)
+    6. 执行 database/migrations/*.sql(迁移脚本,按名称排序)
+       - V2026_01_21__add_document_blocks_and_entities.sql
+       - V2026_01_21_02__add_document_elements.sql
+       - V2026_01_21_03__enhance_data_sources.sql
+       - V2026_01_22_01__enhance_parse_tasks_stages.sql
+       - V2026_01_22_02__create_extract_tables.sql (旧版 extract 表)
+       - V2026_01_23_01__refactor_extract_to_template.sql (新版 template 表)
     7. 创建测试用户
     8. 验证表创建
 
@@ -245,6 +308,9 @@ EOF
 # 主函数
 main() {
     CREATE_TEST_USER=true
+    DROP_ONLY=false
+    INIT_ONLY=false
+    LIST_MIGRATIONS=false
     
     while [[ $# -gt 0 ]]; do
         case $1 in
@@ -260,6 +326,18 @@ main() {
                 CREATE_TEST_USER=false
                 shift
                 ;;
+            --drop-only)
+                DROP_ONLY=true
+                shift
+                ;;
+            --init-only)
+                INIT_ONLY=true
+                shift
+                ;;
+            --list-migrations)
+                LIST_MIGRATIONS=true
+                shift
+                ;;
             *)
                 log_error "未知选项: $1"
                 show_help
@@ -276,6 +354,33 @@ main() {
     echo "迁移目录: ${MIGRATIONS_DIR}"
     echo ""
     
+    # 列出迁移脚本模式
+    if [ "$LIST_MIGRATIONS" = true ]; then
+        list_migrations
+        exit 0
+    fi
+    
+    # 只删除模式
+    if [ "$DROP_ONLY" = true ]; then
+        drop_all_tables
+        log_title "删除完成"
+        log_info "所有表已删除,数据库已清空"
+        exit 0
+    fi
+    
+    # 只初始化模式
+    if [ "$INIT_ONLY" = true ]; then
+        init_all_tables
+        if [ "$CREATE_TEST_USER" = true ]; then
+            create_test_user
+        fi
+        verify_tables
+        log_title "初始化完成"
+        log_info "所有表已创建并验证"
+        exit 0
+    fi
+    
+    # 完整重建模式
     drop_all_tables
     init_all_tables