83 lines
3.3 KiB
PowerShell
83 lines
3.3 KiB
PowerShell
# Import official Jeecg SQL into inquiry_robot only (strip CREATE/USE jeecg-boot).
|
|
$ErrorActionPreference = "Stop"
|
|
$mysql = "C:\yutongda\tools\mysql-8.0.39-winx64\bin\mysql.exe"
|
|
$srcSql = "C:\Users\ytd_extdev_0903\incoming\jeecgboot-mysql-5.7.sql"
|
|
$opsDir = "E:\wwwroot\ops\sql"
|
|
New-Item -ItemType Directory -Force -Path $opsDir | Out-Null
|
|
if (-not (Test-Path $srcSql)) { throw "missing incoming sql" }
|
|
|
|
$patched = Join-Path $opsDir "jeecgboot-mysql-5.7-inquiry_robot.sql"
|
|
Write-Output "patching SQL for inquiry_robot..."
|
|
$raw = [System.IO.File]::ReadAllText($srcSql)
|
|
$raw = [regex]::Replace($raw, '(?im)^\s*CREATE\s+database\s+if\s+NOT\s+EXISTS\s+`jeecg-boot`[^;]*;\s*', '')
|
|
$raw = [regex]::Replace($raw, '(?im)^\s*USE\s+`jeecg-boot`\s*;\s*', '')
|
|
[System.IO.File]::WriteAllText($patched, $raw)
|
|
Write-Output ("patched_bytes=" + (Get-Item $patched).Length)
|
|
|
|
$envFile = "E:\wwwroot\.env"
|
|
$hostn = "10.206.0.15"
|
|
$user = "inquiry_robot"
|
|
$pass = $null
|
|
$db = "inquiry_robot"
|
|
Get-Content $envFile | ForEach-Object {
|
|
if ($_ -match '^MYSQL_HOST=(.+)$') { $hostn = $Matches[1].Trim() }
|
|
if ($_ -match '^MYSQL_USER=(.+)$') { $user = $Matches[1].Trim() }
|
|
if ($_ -match '^MYSQL_PASSWORD=(.+)$') { $pass = $Matches[1].Trim() }
|
|
if ($_ -match '^MYSQL_DB=(.+)$') { $db = $Matches[1].Trim() }
|
|
}
|
|
if ([string]::IsNullOrWhiteSpace($pass)) { throw "MYSQL_PASSWORD empty" }
|
|
if ($db -ne "inquiry_robot") { throw "refuse db=$db" }
|
|
|
|
Write-Output ("import host=$hostn db=$db user=$user")
|
|
$ErrorActionPreference = "Continue"
|
|
|
|
function Mysql-Scalar([string]$query) {
|
|
$out = & $mysql -h $hostn -P 3306 -u $user "-p$pass" --connect-timeout=15 --batch -N $db -e $query 2>&1
|
|
$err = ($out | ForEach-Object {"$_"}) | Where-Object { $_ -match "ERROR\s+\d+" } | Select-Object -First 1
|
|
if ($err) { throw $err }
|
|
return ($out | ForEach-Object {"$_".Trim()} | Where-Object { $_ -ne "" -and $_ -notmatch "Warning" } | Select-Object -Last 1)
|
|
}
|
|
|
|
Write-Output ("connected db=" + (Mysql-Scalar "SELECT DATABASE()"))
|
|
$cnt = Mysql-Scalar "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema=DATABASE() AND table_name='sys_user'"
|
|
Write-Output ("has_sys_user=$cnt")
|
|
if ($cnt -eq "1") {
|
|
Write-Output "SKIP_ALREADY_IMPORTED"
|
|
exit 0
|
|
}
|
|
|
|
$cnf = Join-Path $opsDir "_mysql_import.cnf"
|
|
@"
|
|
[client]
|
|
host=$hostn
|
|
port=3306
|
|
user=$user
|
|
password=$pass
|
|
default-character-set=utf8mb4
|
|
"@ | Set-Content $cnf -Encoding ascii
|
|
|
|
Write-Output "running import..."
|
|
$errLog = Join-Path $opsDir "_import_err.txt"
|
|
$outLog = Join-Path $opsDir "_import_out.txt"
|
|
$p = Start-Process -FilePath $mysql `
|
|
-ArgumentList @("--defaults-extra-file=$cnf", $db) `
|
|
-RedirectStandardInput $patched `
|
|
-RedirectStandardError $errLog `
|
|
-RedirectStandardOutput $outLog `
|
|
-Wait -PassThru -NoNewWindow
|
|
Remove-Item $cnf -Force -ErrorAction SilentlyContinue
|
|
Write-Output ("mysql_exit=" + $p.ExitCode)
|
|
if ($p.ExitCode -ne 0) {
|
|
Get-Content $errLog -Tail 40 -ErrorAction SilentlyContinue
|
|
throw "import exit=$($p.ExitCode)"
|
|
}
|
|
|
|
$cnt2 = Mysql-Scalar "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema=DATABASE() AND table_name='sys_user'"
|
|
Write-Output ("after_has_sys_user=$cnt2")
|
|
if ($cnt2 -ne "1") {
|
|
Get-Content $errLog -Tail 50 -ErrorAction SilentlyContinue
|
|
throw "import did not create sys_user"
|
|
}
|
|
Write-Output ("table_count=" + (Mysql-Scalar "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema=DATABASE()"))
|
|
Write-Output "IMPORT_OK"
|