param(
|
[string]$Source = (Join-Path $PSScriptRoot '..\battery_detection.sql'),
|
[string]$Output = (Join-Path $PSScriptRoot '..\ruoyi-admin\src\main\resources\db\migration\V1__ruoyi_platform_baseline.sql')
|
)
|
|
$sourcePath = [System.IO.Path]::GetFullPath($Source)
|
$outputPath = [System.IO.Path]::GetFullPath($Output)
|
$sql = [System.IO.File]::ReadAllText($sourcePath)
|
|
$tablePattern = '(?ms)^CREATE TABLE `(?<name>[^`]+)`\s+\(.*?^\).*?;\s*$'
|
$platformTables = [regex]::Matches($sql, $tablePattern) |
|
Where-Object {
|
$name = $_.Groups['name'].Value
|
$name.StartsWith('QRTZ_') -or $name.StartsWith('gen_') -or $name.StartsWith('sys_')
|
}
|
|
if ($platformTables.Count -eq 0) {
|
throw "No platform table definitions found in $sourcePath"
|
}
|
|
$seedRules = @{
|
'sys_config' = '^INSERT INTO `sys_config`'
|
'sys_dept' = '^INSERT INTO `sys_dept` VALUES \((100|101|103),'
|
'sys_dict_data' = '^INSERT INTO `sys_dict_data`'
|
'sys_dict_type' = '^INSERT INTO `sys_dict_type`'
|
'sys_menu' = '^INSERT INTO `sys_menu`'
|
'sys_post' = '^INSERT INTO `sys_post` VALUES \(1,'
|
'sys_role' = '^INSERT INTO `sys_role` VALUES \(1,'
|
'sys_user' = '^INSERT INTO `sys_user` VALUES \(1,'
|
'sys_user_post' = '^INSERT INTO `sys_user_post` VALUES \(1,'
|
'sys_user_role' = '^INSERT INTO `sys_user_role` VALUES \(1,'
|
}
|
|
$sourceLines = [System.IO.File]::ReadAllLines($sourcePath)
|
$seedLines = foreach ($rule in $seedRules.GetEnumerator() | Sort-Object Name) {
|
$sourceLines | Where-Object { $_ -match $rule.Value }
|
}
|
|
$parts = [System.Collections.Generic.List[string]]::new()
|
$parts.Add('-- Generated by scripts/Build-FlywayBaseline.ps1 from the checked-in schema dump.')
|
$parts.Add('-- Contains platform structure and minimal development seed data only.')
|
$parts.Add('SET NAMES utf8mb4;')
|
$parts.Add('SET FOREIGN_KEY_CHECKS = 0;')
|
$parts.Add('')
|
|
foreach ($table in $platformTables) {
|
$parts.Add($table.Value.Trim())
|
$parts.Add('')
|
}
|
|
$parts.Add('-- Minimal development seed data')
|
foreach ($line in $seedLines) {
|
$parts.Add($line)
|
}
|
$parts.Add('')
|
$parts.Add('SET FOREIGN_KEY_CHECKS = 1;')
|
$parts.Add('')
|
|
$outputDirectory = [System.IO.Path]::GetDirectoryName($outputPath)
|
[System.IO.Directory]::CreateDirectory($outputDirectory) | Out-Null
|
[System.IO.File]::WriteAllText($outputPath, ($parts -join "`r`n"), [System.Text.UTF8Encoding]::new($false))
|
|
Write-Output $outputPath
|