Files
medical-mall/mall_sql/deploy.bat
2026-01-30 16:11:23 +08:00

202 lines
6.1 KiB
Batchfile
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
@echo off
REM ================================================================================
REM Mall SQL 自动部署脚本 (Windows)
REM ================================================================================
REM 用途:自动执行商城数据库脚本
REM 使用deploy.bat [选项]
REM
REM 选项:
REM --full 完整部署(删除重建)
REM --upgrade 增量升级
REM --check 仅检查数据库状态
REM --test 创建测试数据
REM --help 显示帮助
REM ================================================================================
setlocal enabledelayedexpansion
REM 默认配置
if "%DB_HOST%"=="" set DB_HOST=localhost
if "%DB_PORT%"=="" set DB_PORT=5432
if "%DB_NAME%"=="" set DB_NAME=akmon
if "%DB_USER%"=="" set DB_USER=postgres
set SCHEMA_DIR=schemas
set MIGRATION_DIR=migrations
set TEST_DIR=tests
set SUBSCRIPTION_DIR=subscription
REM 显示帮助
if "%1"=="--help" goto :show_help
if "%1"=="-h" goto :show_help
echo ==========================================
echo Mall SQL 自动部署脚本
echo ==========================================
echo 数据库: %DB_USER%@%DB_HOST%:%DB_PORT%/%DB_NAME%
echo ==========================================
echo.
REM 检查连接
echo [INFO] 检查数据库连接...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -c "\q" >nul 2>&1
if errorlevel 1 (
echo [ERROR] 无法连接到数据库
echo 请检查环境变量或连接信息
goto :end
)
echo [SUCCESS] 数据库连接成功
echo.
REM 解析参数
if "%1"=="--full" goto :deploy_full
if "%1"=="--upgrade" goto :deploy_upgrade
if "%1"=="--check" goto :check_database
if "%1"=="--test" goto :create_test_data
if "%1"=="--subscription" goto :deploy_subscription
echo [ERROR] 未知选项: %1%
echo.
goto :show_help
:deploy_full
echo ==========================================
echo [WARNING] 完整部署将重建所有商城表!
echo ==========================================
set /p confirm="确认继续? (yes/no): "
if /i not "%confirm%"=="yes" (
echo [INFO] 已取消
goto :end
)
echo [INFO] 开始完整部署...
echo [INFO] 执行: 创建商城核心表...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %SCHEMA_DIR%\complete_mall_database.sql
if errorlevel 1 goto :error
echo [INFO] 执行: 创建商品补充表...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %SCHEMA_DIR%\product_database.sql
if errorlevel 1 goto :error
echo [INFO] 执行: 配置SEO和安全策略...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %SCHEMA_DIR%\mall_seo_security.sql
if errorlevel 1 goto :error
echo [INFO] 执行: 创建订阅表...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %SUBSCRIPTION_DIR%\create_mall_subscription_tables.sql
if errorlevel 1 goto :error
echo [INFO] 执行: 创建订阅触发器...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %SUBSCRIPTION_DIR%\subscription_guard_trigger.sql
if errorlevel 1 goto :error
echo [INFO] 执行: 创建订阅RLS策略...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %SUBSCRIPTION_DIR%\subscription_rls_policies.sql
if errorlevel 1 goto :error
echo.
echo [SUCCESS] 完整部署完成!
echo [INFO] 接下来可以运行: deploy.bat --test
goto :end
:deploy_upgrade
echo [INFO] 开始增量升级...
echo [INFO] 执行: 数据库状态检查...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %TEST_DIR%\mall_database_check.sql
if errorlevel 1 goto :error
echo [INFO] 执行: 增量升级...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %MIGRATION_DIR%\mall_alter_upgrade.sql
if errorlevel 1 goto :error
echo [INFO] 执行: 数据完整性验证...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %TEST_DIR%\validation_test.sql
if errorlevel 1 goto :error
echo.
echo [SUCCESS] 增量升级完成!
goto :end
:check_database
echo [INFO] 检查数据库状态...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %TEST_DIR%\mall_database_check.sql
if errorlevel 1 goto :error
echo [SUCCESS] 检查完成
goto :end
:create_test_data
echo [INFO] 创建测试数据...
echo [INFO] 执行: 创建测试用户...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %TEST_DIR%\create_supabase_auth_users.sql
if errorlevel 1 goto :error
echo [INFO] 执行: 创建模拟数据...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %TEST_DIR%\mock_data_insert.sql
if errorlevel 1 goto :error
echo [INFO] 执行: 验证测试数据...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %TEST_DIR%\verify_mock_data_fix.sql
if errorlevel 1 goto :error
echo.
echo [SUCCESS] 测试数据创建完成!
goto :end
:deploy_subscription
echo [INFO] 部署订阅系统...
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %SUBSCRIPTION_DIR%\create_mall_subscription_tables.sql
if errorlevel 1 goto :error
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %SUBSCRIPTION_DIR%\subscription_guard_trigger.sql
if errorlevel 1 goto :error
psql -h %DB_HOST% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -f %SUBSCRIPTION_DIR%\subscription_rls_policies.sql
if errorlevel 1 goto :error
echo [SUCCESS] 订阅系统部署完成!
goto :end
:show_help
echo Mall SQL 自动部署脚本 (Windows)
echo.
echo 用法: deploy.bat [选项]
echo.
echo 选项:
echo --full 完整部署(删除重建)
echo --upgrade 增量升级(保留现有数据)
echo --check 检查数据库状态
echo --test 创建测试数据
echo --subscription 部署订阅系统
echo --help 显示此帮助信息
echo.
echo 环境变量:
echo DB_HOST 数据库主机 (默认: localhost)
echo DB_PORT 数据库端口 (默认: 5432)
echo DB_NAME 数据库名称 (默认: akmon)
echo DB_USER 数据库用户 (默认: postgres)
echo.
echo 示例:
echo 完整部署
echo deploy.bat --full
echo.
echo 增量升级
echo deploy.bat --upgrade
echo.
echo 使用自定义数据库
echo set DB_HOST=prod.example.com
echo set DB_NAME=akmon_prod
echo deploy.bat --upgrade
goto :end
:error
echo.
echo [ERROR] 部署失败!请检查错误信息。
exit /b 1
:end
endlocal