42 lines
1.3 KiB
PowerShell
42 lines
1.3 KiB
PowerShell
param (
|
|
[ValidateSet("migrate", "info", "validate", "clean", "baseline", "repair")]
|
|
[string]$Command = "info"
|
|
)
|
|
|
|
$envFile = ".env"
|
|
if (-not (Test-Path $envFile)) {
|
|
Write-Error ".env file not found in the current directory."
|
|
exit 1
|
|
}
|
|
|
|
Get-Content $envFile | ForEach-Object {
|
|
if ($_ -match "^\s*([A-Za-z_][A-Za-z0-9_]*)=(.*)$") {
|
|
$name = $matches[1]
|
|
$value = $matches[2]
|
|
[System.Environment]::SetEnvironmentVariable($name, $value)
|
|
}
|
|
}
|
|
|
|
$MYSQL_HOST = $env:MYSQL_HOST
|
|
$MYSQL_PORT = $env:MYSQL_PORT
|
|
$MYSQL_DATABASE = $env:MYSQL_DATABASE
|
|
|
|
if (-not $MYSQL_HOST -or -not $MYSQL_PORT -or -not $MYSQL_DATABASE) {
|
|
Write-Error "MYSQL_HOST, MYSQL_PORT, or MYSQL_DATABASE missing from .env"
|
|
exit 1
|
|
}
|
|
|
|
$env:FLYWAY_URL = "jdbc:mysql://${MYSQL_HOST}:${MYSQL_PORT}/${MYSQL_DATABASE}"
|
|
$env:FLYWAY_USER = $env:MYSQL_USER
|
|
$env:FLYWAY_PASSWORD = $env:MYSQL_PASSWORD
|
|
|
|
Write-Host "Ensuring database '$MYSQL_DATABASE' exists..."
|
|
$mysqlCommand = "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
|
|
& mysql --host=$MYSQL_HOST --port=$MYSQL_PORT --user=$env:MYSQL_USER --password=$env:MYSQL_PASSWORD -e $mysqlCommand
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Error "Failed to create database or connect to MySQL. Check your credentials or server status."
|
|
exit 1
|
|
}
|
|
|
|
.\mvnw flyway:$Command |