79 lines
2.2 KiB
PowerShell
79 lines
2.2 KiB
PowerShell
#Requires -Version 5.1
|
|
<#
|
|
.SYNOPSIS
|
|
重置 jinrong_core 模拟库并同步到 agent / Neo4j
|
|
#>
|
|
param(
|
|
[string]$MysqlUser = "root",
|
|
[string]$MysqlHost = "127.0.0.1",
|
|
[string]$MysqlPassword = $env:MYSQL_PASSWORD,
|
|
[switch]$SkipNeo4j,
|
|
[switch]$SkipSync
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
$Root = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent
|
|
Set-Location $Root
|
|
|
|
if (-not $MysqlPassword) {
|
|
$EnvPath = Join-Path $Root ".env"
|
|
if (Test-Path -LiteralPath $EnvPath) {
|
|
$MysqlPasswordLine = Get-Content -LiteralPath $EnvPath -Encoding UTF8 |
|
|
Where-Object { $_ -match "^MYSQL_PASSWORD=" } |
|
|
Select-Object -First 1
|
|
if ($MysqlPasswordLine) {
|
|
$MysqlPassword = $MysqlPasswordLine.Substring("MYSQL_PASSWORD=".Length).Trim()
|
|
}
|
|
}
|
|
}
|
|
|
|
$MysqlArgs = @("-h", $MysqlHost, "-u", $MysqlUser)
|
|
$MysqlArgs += "--default-character-set=utf8mb4"
|
|
if ($MysqlPassword) {
|
|
$MysqlArgs += "--password=$MysqlPassword"
|
|
} else {
|
|
$MysqlArgs += "-p"
|
|
}
|
|
|
|
$OutputEncoding = [System.Text.UTF8Encoding]::new($false)
|
|
|
|
function Invoke-SqlFile($path) {
|
|
Write-Host ">> $path"
|
|
$sql = Get-Content -LiteralPath $path -Raw -Encoding UTF8
|
|
$sql | mysql @MysqlArgs
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "MySQL 执行失败,退出码:$LASTEXITCODE,文件:$path"
|
|
}
|
|
}
|
|
|
|
Write-Host "=== Drop & recreate jinrong_core ==="
|
|
mysql @MysqlArgs -e "DROP DATABASE IF EXISTS jinrong_core;"
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "MySQL 删除 jinrong_core 失败,退出码:$LASTEXITCODE"
|
|
}
|
|
|
|
$files = @(
|
|
"scripts/core/00-create-database.sql",
|
|
"scripts/core/01-ddl.sql",
|
|
"scripts/core/02-seed-base.sql",
|
|
"scripts/core/03-seed-customers.sql",
|
|
"scripts/core/04-seed-holdings.sql",
|
|
"scripts/core/05-seed-trades.sql",
|
|
"scripts/core/06-seed-nav.sql"
|
|
)
|
|
foreach ($f in $files) { Invoke-SqlFile (Join-Path $Root $f) }
|
|
|
|
if (-not $SkipSync) {
|
|
Write-Host "=== Sync advisor rel ==="
|
|
python scripts/sync/sync_advisor_rel.py
|
|
|
|
if (-not $SkipNeo4j) {
|
|
Write-Host "=== Sync Neo4j ==="
|
|
python scripts/sync/sync_neo4j.py
|
|
}
|
|
} else {
|
|
Write-Host "=== Skip sync tasks (Core only) ==="
|
|
}
|
|
|
|
Write-Host "Done."
|