35 lines
1.2 KiB
PowerShell
35 lines
1.2 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[string]$ComposeFile,
|
|
[string]$MigrationDirectory
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$scriptDirectory = Split-Path -Parent $PSCommandPath
|
|
if ([string]::IsNullOrWhiteSpace($scriptDirectory)) {
|
|
$scriptDirectory = (Get-Location).Path
|
|
}
|
|
if ([string]::IsNullOrWhiteSpace($ComposeFile)) {
|
|
$ComposeFile = Join-Path $scriptDirectory '..\docker-compose.yml'
|
|
}
|
|
if ([string]::IsNullOrWhiteSpace($MigrationDirectory)) {
|
|
$MigrationDirectory = Join-Path $scriptDirectory '..\database\migrations'
|
|
}
|
|
|
|
if (-not (Test-Path -LiteralPath $MigrationDirectory -PathType Container)) {
|
|
throw "Das Migrationsverzeichnis '$MigrationDirectory' wurde nicht gefunden."
|
|
}
|
|
|
|
$composeFile = (Resolve-Path -LiteralPath $ComposeFile).Path
|
|
$migrations = Get-ChildItem -LiteralPath $MigrationDirectory -Filter '*.sql' -File | Sort-Object Name
|
|
|
|
foreach ($migration in $migrations) {
|
|
$sql = Get-Content -LiteralPath $migration.FullName -Raw
|
|
$sql | & docker compose -f $composeFile exec -T db sh -lc 'exec mariadb -u"$MARIADB_USER" -p"$MARIADB_PASSWORD" "$MARIADB_DATABASE"'
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "Migration '$($migration.Name)' ist fehlgeschlagen."
|
|
}
|
|
|
|
Write-Host "Migration ausgeführt: $($migration.Name)"
|
|
}
|