Param( [string]$Host = $env:DB_HOST, [int]$Port = [int]($env:DB_PORT), [string]$Database = $env:DB_NAME, [string]$User = $env:DB_USER, [string]$Password = $env:DB_PASSWORD, [string]$AdminPlain = 'Admin123456' ) Write-Host "Using DB: $Host:$Port/$Database" # 生成管理员 bcrypt 哈希 try { $nodeScript = @" const bcrypt = require('bcryptjs'); const pwd = process.argv[2] || 'Admin123456'; bcrypt.hash(pwd, 10).then(h => { console.log(h); }).catch(e => { console.error(e); process.exit(1); }); "@ $hash = node -e $nodeScript $AdminPlain if (-not $hash) { throw 'bcrypt hash failed' } } catch { Write-Error "Failed to generate bcrypt hash: $_" exit 1 } # 读取SQL并替换占位符 $schema = Get-Content -Raw -Encoding UTF8 "$PSScriptRoot/create-bank-schema.sql" $seed = (Get-Content -Raw -Encoding UTF8 "$PSScriptRoot/seed-bank-demo.sql").Replace('REPLACE_ADMIN_BCRYPT', $hash) $sql = $schema + "`n" + $seed # 写入临时文件 $tmp = New-TemporaryFile Set-Content -Path $tmp -Value $sql -Encoding UTF8 # 调用 mysql 客户端 try { $env:MYSQL_PWD = $Password & mysql --host=$Host --port=$Port --user=$User --database=$Database --default-character-set=utf8mb4 --protocol=TCP < $tmp if ($LASTEXITCODE -ne 0) { throw "mysql returned $LASTEXITCODE" } Write-Host "✅ Schema & seed executed successfully" } catch { Write-Error "Failed to execute SQL: $_" exit 1 } finally { Remove-Item $tmp -Force -ErrorAction SilentlyContinue }