[CmdletBinding()] param( [string]$ComposeFile = (Join-Path $PSScriptRoot '..\docker-compose.yml'), [string]$SeedPath = (Join-Path $PSScriptRoot '..\database\contao-baseline.sql'), [switch]$ReplaceExisting ) $ErrorActionPreference = 'Stop' $tables = @('tl_theme', 'tl_layout', 'tl_module', 'tl_page', 'tl_article', 'tl_content', 'tl_form', 'tl_form_field') if (-not (Test-Path -LiteralPath $SeedPath)) { throw "Der Baseline-Seed '$SeedPath' wurde nicht gefunden." } $composeFile = (Resolve-Path -LiteralPath $ComposeFile).Path $countQuery = ($tables | ForEach-Object { "SELECT COUNT(*) FROM $_" }) -join ' UNION ALL ' $checkCommand = "mariadb -N -u`"`$MARIADB_USER`" -p`"`$MARIADB_PASSWORD`" `"`$MARIADB_DATABASE`" -e `"$countQuery`"" $existingRows = (& docker compose -f $composeFile exec -T db sh -lc $checkCommand | ForEach-Object { [int]$_ } | Measure-Object -Sum).Sum if ($existingRows -gt 0 -and -not $ReplaceExisting) { throw 'Die versionierten Contao-Tabellen enthalten bereits Daten. Der Import wurde nicht ausgeführt. Nutze -ReplaceExisting nur, wenn du die aktuelle Seitenstruktur bewusst durch den Git-Stand ersetzen möchtest.' } $seed = Get-Content -LiteralPath $SeedPath -Raw if ($ReplaceExisting) { $deleteStatements = @( 'SET FOREIGN_KEY_CHECKS=0;', 'DELETE FROM tl_content;', 'DELETE FROM tl_article;', 'DELETE FROM tl_form_field;', 'DELETE FROM tl_form;', 'DELETE FROM tl_module;', 'DELETE FROM tl_page;', 'DELETE FROM tl_layout;', 'DELETE FROM tl_theme;', 'SET FOREIGN_KEY_CHECKS=1;', '' ) -join [Environment]::NewLine $seed = $deleteStatements + $seed } $utf8WithoutBom = New-Object System.Text.UTF8Encoding($false) [Console]::OutputEncoding = $utf8WithoutBom $seed | & docker compose -f $composeFile exec -T db sh -lc 'exec mariadb -u"$MARIADB_USER" -p"$MARIADB_PASSWORD" "$MARIADB_DATABASE"' if ($LASTEXITCODE -ne 0) { throw 'Der Contao-Baseline-Import ist fehlgeschlagen.' } & docker compose -f $composeFile exec -T app sh -lc 'runuser -u www-data -- vendor/bin/contao-console cache:clear --env=dev' if ($LASTEXITCODE -ne 0) { throw 'Der Import war erfolgreich, aber der Contao-Cache konnte nicht geleert werden.' } Write-Host 'Contao-Baseline importiert und Cache geleert.'