Files
bj_power/bj_power_mes/scripts/init-db.ps1
T

118 lines
4.7 KiB
PowerShell

# ============================================================================
# PostgreSQL 数据库初始化脚本
# ============================================================================
# 用法 (PowerShell):
# .\scripts\init-db.ps1
#
# 前提条件:
# 1. 本地已安装 PostgreSQL
# 2. postgres 用户可访问(可能需要输入密码)
# 3. psql 命令已在系统 PATH 中
# ============================================================================
param(
[string]$DbUser = "postgres",
[string]$DbName = "back_cover",
[string]$SqlFile = "$PSScriptRoot\..\doc\data.sql"
)
$ErrorActionPreference = "Stop"
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "PostgreSQL 数据库初始化脚本" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# 检查 psql 是否可用
try {
$psqlVersion = psql --version
Write-Host "[✓] psql 已安装: $psqlVersion" -ForegroundColor Green
} catch {
Write-Host "[✗] 错误: 未找到 psql 命令" -ForegroundColor Red
Write-Host "请确保 PostgreSQL 已安装且 psql 在系统 PATH 中" -ForegroundColor Yellow
Write-Host "下载地址: https://www.postgresql.org/download/windows/" -ForegroundColor Yellow
exit 1
}
# 步骤 1: 创建数据库
Write-Host ""
Write-Host "--- 步骤 1: 创建数据库 ---" -ForegroundColor Cyan
$dbExists = psql -U $DbUser -tc "SELECT 1 FROM pg_database WHERE datname = '$DbName'" 2>$null
if ($dbExists -eq "1") {
Write-Host "[!] 数据库 '$DbName' 已存在" -ForegroundColor Yellow
$confirm = Read-Host "是否删除并重建? (y/n)"
if ($confirm -eq "y" -or $confirm -eq "Y") {
Write-Host "正在删除数据库..." -ForegroundColor Yellow
psql -U $DbUser -c "DROP DATABASE IF EXISTS $DbName;" 2>&1 | Out-Null
Write-Host "[✓] 数据库已删除" -ForegroundColor Green
} else {
Write-Host "[✓] 跳过数据库创建" -ForegroundColor Green
}
} else {
Write-Host "正在创建数据库 '$DbName'..." -ForegroundColor Yellow
psql -U $DbUser -c "CREATE DATABASE $DbName;" 2>&1 | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host "[✓] 数据库创建成功" -ForegroundColor Green
} else {
Write-Host "[✗] 数据库创建失败" -ForegroundColor Red
exit 1
}
}
# 步骤 2: 提示用户启动应用以创建表结构
Write-Host ""
Write-Host "--- 步骤 2: 创建表结构 ---" -ForegroundColor Cyan
Write-Host "Ent ORM 会在首次启动时自动创建表结构。" -ForegroundColor White
Write-Host ""
Write-Host "请按以下方式操作:" -ForegroundColor Yellow
Write-Host " 1. 打开新的终端窗口" -ForegroundColor White
Write-Host " 2. 运行: cd $($PSScriptRoot)\.." -ForegroundColor White
Write-Host " 3. 运行: go run hougai.go -f etc\hougai-api.yaml" -ForegroundColor White
Write-Host " 4. 等待看到 'Starting server at...' 消息" -ForegroundColor White
Write-Host " 5. 按 Ctrl+C 停止应用(无需等待完整启动)" -ForegroundColor White
Write-Host ""
Read-Host "完成后按回车继续..."
# 步骤 3: 插入种子数据
Write-Host ""
Write-Host "--- 步骤 3: 插入种子数据 ---" -ForegroundColor Cyan
if (-not (Test-Path $SqlFile)) {
Write-Host "[✗] 错误: SQL 文件不存在: $SqlFile" -ForegroundColor Red
exit 1
}
Write-Host "正在执行: $SqlFile" -ForegroundColor Yellow
$result = psql -U $DbUser -d $DbName -f $SqlFile 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host "[✓] 种子数据插入成功" -ForegroundColor Green
Write-Host ""
Write-Host "执行输出:" -ForegroundColor Cyan
Write-Host $result -ForegroundColor Gray
} else {
Write-Host "[✗] 种子数据插入失败" -ForegroundColor Red
Write-Host ""
Write-Host "错误输出:" -ForegroundColor Red
Write-Host $result -ForegroundColor Red
exit 1
}
# 完成
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "数据库初始化完成!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "下一步:" -ForegroundColor Yellow
Write-Host " 1. 重启后端应用" -ForegroundColor White
Write-Host " go run hougai.go -f etc/hougai-api.yaml" -ForegroundColor Gray
Write-Host ""
Write-Host " 2. 启动前端开发服务器" -ForegroundColor White
Write-Host " cd frontend" -ForegroundColor Gray
Write-Host " pnpm dev" -ForegroundColor Gray
Write-Host ""
Write-Host " 3. 浏览器访问: http://localhost:7070" -ForegroundColor White
Write-Host " 用户名: admin" -ForegroundColor Gray
Write-Host " 密码: 123456" -ForegroundColor Gray
Write-Host ""