# Keep only 5 RBAC tables. $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" } $keepSet = New-Object 'System.Collections.Generic.HashSet[string]' @('sys_user','sys_role','sys_user_role','sys_permission','sys_role_permission') | ForEach-Object { [void]$keepSet.Add($_) } $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" } $drop = New-Object System.Collections.Generic.List[string] foreach ($t in $tables) { if (-not $keepSet.Contains($t)) { [void]$drop.Add($t) } } Write-Output ("before=" + @($tables).Count + " drop=" + $drop.Count) $sb = New-Object System.Text.StringBuilder [void]$sb.AppendLine("SET FOREIGN_KEY_CHECKS=0;") foreach ($t in $drop) { [void]$sb.AppendLine("DROP TABLE IF EXISTS ``$t``;") } [void]$sb.AppendLine("SET FOREIGN_KEY_CHECKS=1;") $sqlFile = "E:\wwwroot\ops\sql\_keep_rbac_only.sql" [System.IO.File]::WriteAllText($sqlFile, $sb.ToString()) & $mysql -h $hostn -P 3306 -u $user "-p$pass" $db -e "source $($sqlFile -replace '\\','/')" 2>&1 | Where-Object { $_ -notmatch "Warning|password" } | Out-Null $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 "RBAC_ONLY_OK"