JavaProjectRepo/framework/db-init/scripts/import.ps1

46 lines
1.5 KiB
PowerShell
Raw Normal View History

param(
[string]$DB_HOST = '43.138.168.68',
[int]$DB_PORT = 3306,
[string]$DB_NAME = 'frameworkdb2025',
[string]$DB_USER = 'root',
[string]$DB_PASSWORD = 'ylfw20230626@'
)
$ErrorActionPreference = 'Stop'
$env:MYSQL_PWD = $DB_PASSWORD
$schemaPath = Join-Path $PSScriptRoot '..\sql\base-schema.sql'
$dataPath = Join-Path $PSScriptRoot '..\sql\base-data.sql'
function Invoke-MySqlFile([string]$filePath) {
Write-Host ("Processing SQL file: {0}" -f $filePath)
$sql = Get-Content -Raw $filePath
# Strip UTF-8 BOM if present
$bom = [char]0xFEFF
if ($sql.StartsWith($bom)) { $sql = $sql.Substring(1) }
# Sanitize dump headers and environment-specific statements
$lines = $sql -split "`r?`n"
$cleanLines = $lines | Where-Object {
$t = $_.Trim()
if ($t -eq '') { return $false }
if ($t -match '^(--|/\*|\*/|/\*!|[-]+$)') { return $false }
if ($t -match '^USE\s+') { return $false }
if ($t -match '^CREATE\s+DATABASE') { return $false }
return $true
}
$cleanSql = ($cleanLines -join "`n")
if ([string]::IsNullOrWhiteSpace($cleanSql)) {
Write-Warning ("Sanitized script is empty, skipping: {0}" -f $filePath)
return
}
Write-Host ("Importing via mysql from: {0}" -f $filePath)
$cleanSql | & mysql -h $DB_HOST -P $DB_PORT -u $DB_USER $DB_NAME
if ($LASTEXITCODE -ne 0) {
throw ("Import failed for {0} with exit code {1}" -f $filePath, $LASTEXITCODE)
}
}
Invoke-MySqlFile $schemaPath
Invoke-MySqlFile $dataPath
Write-Host 'Import completed'