49 lines
2.0 KiB
PowerShell
49 lines
2.0 KiB
PowerShell
# inquiry_robot: keep only admin-related sys_* tables; drop the rest.
|
|
$ErrorActionPreference = "Continue"
|
|
$mysql = "C:\yutongda\tools\mysql-8.0.39-winx64\bin\mysql.exe"
|
|
$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 ($db -ne "inquiry_robot") { throw "refuse db=$db" }
|
|
|
|
$tables = & $mysql -h $hostn -P 3306 -u $user "-p$pass" --batch -N $db -e "SHOW TABLES" 2>&1 |
|
|
ForEach-Object {"$_".Trim()} | Where-Object { $_ -ne "" -and $_ -notmatch "Warning" }
|
|
|
|
$keep = @()
|
|
$drop = @()
|
|
foreach ($t in $tables) {
|
|
# 只要管理员体系:用户/角色/菜单/部门/权限/字典/租户/日志等 sys_*
|
|
if ($t -match '^sys_') { $keep += $t } else { $drop += $t }
|
|
}
|
|
|
|
Write-Output ("before=" + $tables.Count + " keep=" + $keep.Count + " drop=" + $drop.Count)
|
|
Write-Output "=== keep ==="
|
|
$keep | ForEach-Object { $_ }
|
|
Write-Output "=== drop ==="
|
|
$drop | ForEach-Object { $_ }
|
|
|
|
if ($drop.Count -eq 0) {
|
|
Write-Output "NOTHING_TO_DROP"
|
|
exit 0
|
|
}
|
|
|
|
$sql = "SET FOREIGN_KEY_CHECKS=0;`n"
|
|
foreach ($t in $drop) { $sql += "DROP TABLE IF EXISTS ``$t``;`n" }
|
|
$sql += "SET FOREIGN_KEY_CHECKS=1;`n"
|
|
$sql += "SELECT COUNT(*) AS c FROM information_schema.tables WHERE table_schema=DATABASE();`n"
|
|
$sqlFile = "E:\wwwroot\ops\sql\_keep_sys_only.sql"
|
|
Set-Content $sqlFile -Value $sql -Encoding ascii
|
|
& $mysql -h $hostn -P 3306 -u $user "-p$pass" $db -e "source $($sqlFile -replace '\\','/')" 2>&1 |
|
|
Where-Object { $_ -notmatch "Warning|password" }
|
|
|
|
$left = & $mysql -h $hostn -P 3306 -u $user "-p$pass" --batch -N $db -e "SHOW TABLES" 2>&1 |
|
|
ForEach-Object {"$_".Trim()} | Where-Object { $_ -ne "" -and $_ -notmatch "Warning" }
|
|
Write-Output ("after_total=" + @($left).Count)
|
|
$left | ForEach-Object { $_ }
|
|
Write-Output "SYS_ONLY_OK"
|