96 lines
3.3 KiB
PowerShell
96 lines
3.3 KiB
PowerShell
|
|
param(
|
||
|
|
[string]$HostName = "127.0.0.1",
|
||
|
|
[int]$Port = 3306,
|
||
|
|
[string]$User = "root",
|
||
|
|
[string]$Password = "",
|
||
|
|
[string]$DatabaseName = "medical_consultation_agent",
|
||
|
|
[string]$MysqlExe = "mysql"
|
||
|
|
)
|
||
|
|
|
||
|
|
$ErrorActionPreference = "Stop"
|
||
|
|
|
||
|
|
function Resolve-ProjectPath {
|
||
|
|
$scriptDir = Split-Path -Parent $MyInvocation.ScriptName
|
||
|
|
return (Resolve-Path -Path (Join-Path $scriptDir "..")).Path
|
||
|
|
}
|
||
|
|
|
||
|
|
function Convert-ToMysqlSourcePath {
|
||
|
|
param([string]$Path)
|
||
|
|
return $Path.Replace("\", "/")
|
||
|
|
}
|
||
|
|
|
||
|
|
function Invoke-MysqlCommand {
|
||
|
|
param([string]$Sql)
|
||
|
|
& $MysqlExe -h $HostName -P $Port -u $User --default-character-set=utf8mb4 -e $Sql
|
||
|
|
if ($LASTEXITCODE -ne 0) {
|
||
|
|
throw "MySQL command failed: $Sql"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function Invoke-MysqlDatabaseCommand {
|
||
|
|
param([string]$Sql)
|
||
|
|
& $MysqlExe -h $HostName -P $Port -u $User --default-character-set=utf8mb4 -D $DatabaseName -e $Sql
|
||
|
|
if ($LASTEXITCODE -ne 0) {
|
||
|
|
throw "MySQL database command failed: $Sql"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$projectRoot = Resolve-ProjectPath
|
||
|
|
$schemaPath = Convert-ToMysqlSourcePath((Resolve-Path -Path (Join-Path $projectRoot "docs/sql/schema.sql")).Path)
|
||
|
|
$seedPath = Convert-ToMysqlSourcePath((Resolve-Path -Path (Join-Path $projectRoot "docs/sql/seed_pediatric_pneumonia.sql")).Path)
|
||
|
|
$tempDir = Join-Path $projectRoot "storage/mysql_import"
|
||
|
|
New-Item -ItemType Directory -Force -Path $tempDir | Out-Null
|
||
|
|
$tempSchemaPath = Join-Path $tempDir "schema.$DatabaseName.sql"
|
||
|
|
$tempSeedPath = Join-Path $tempDir "seed.$DatabaseName.sql"
|
||
|
|
|
||
|
|
(Get-Content -Path $schemaPath -Raw -Encoding UTF8).Replace("medical_consultation_agent", $DatabaseName) |
|
||
|
|
Set-Content -Path $tempSchemaPath -Encoding UTF8
|
||
|
|
(Get-Content -Path $seedPath -Raw -Encoding UTF8).Replace("medical_consultation_agent", $DatabaseName) |
|
||
|
|
Set-Content -Path $tempSeedPath -Encoding UTF8
|
||
|
|
|
||
|
|
$tempSchemaSource = Convert-ToMysqlSourcePath((Resolve-Path -Path $tempSchemaPath).Path)
|
||
|
|
$tempSeedSource = Convert-ToMysqlSourcePath((Resolve-Path -Path $tempSeedPath).Path)
|
||
|
|
|
||
|
|
if ([string]::IsNullOrWhiteSpace($Password)) {
|
||
|
|
$securePassword = Read-Host "MySQL password for $User@$HostName" -AsSecureString
|
||
|
|
$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword)
|
||
|
|
try {
|
||
|
|
$Password = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
|
||
|
|
}
|
||
|
|
finally {
|
||
|
|
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$env:MYSQL_PWD = $Password
|
||
|
|
|
||
|
|
try {
|
||
|
|
Write-Host "Checking MySQL connection..."
|
||
|
|
Invoke-MysqlCommand "SELECT VERSION() AS mysql_version;"
|
||
|
|
|
||
|
|
Write-Host "Creating schema in database $DatabaseName from docs/sql/schema.sql..."
|
||
|
|
Invoke-MysqlCommand "source $tempSchemaSource"
|
||
|
|
|
||
|
|
Write-Host "Seeding demo data into database $DatabaseName..."
|
||
|
|
Invoke-MysqlCommand "source $tempSeedSource"
|
||
|
|
|
||
|
|
Write-Host "Verifying tables and seed data..."
|
||
|
|
$verifySql = @"
|
||
|
|
SELECT COUNT(*) AS table_count
|
||
|
|
FROM information_schema.tables
|
||
|
|
WHERE table_schema = 'medical_consultation_agent';
|
||
|
|
SELECT id, case_code, title, difficulty
|
||
|
|
FROM cases;
|
||
|
|
SELECT item_code, item_name, item_type, is_key
|
||
|
|
FROM case_exam_items
|
||
|
|
ORDER BY display_order;
|
||
|
|
"@
|
||
|
|
$verifySql = $verifySql.Replace("medical_consultation_agent", $DatabaseName)
|
||
|
|
Invoke-MysqlDatabaseCommand $verifySql
|
||
|
|
|
||
|
|
Write-Host "MySQL demo schema initialized successfully."
|
||
|
|
}
|
||
|
|
finally {
|
||
|
|
Remove-Item Env:\MYSQL_PWD -ErrorAction SilentlyContinue
|
||
|
|
}
|