From e752d92e0b9d0b0e1264058046640f17c4eb40f0 Mon Sep 17 00:00:00 2001 From: Timo Notheisen Date: Tue, 1 Oct 2024 09:35:23 +0200 Subject: [PATCH] refactor: simplify scripts even further --- .../scripts/windows/load-snapshot.ps1 | 2 - scripts/windows/dumps/dump-postgres.ps1 | 37 +++++-------------- scripts/windows/dumps/dump-sqlserver.ps1 | 36 +++++------------- scripts/windows/dumps/load-postgres.ps1 | 2 +- scripts/windows/dumps/load-sqlserver.ps1 | 2 +- 5 files changed, 20 insertions(+), 59 deletions(-) diff --git a/Applications/ConsumerApi/test/ConsumerApi.Tests.Performance/scripts/windows/load-snapshot.ps1 b/Applications/ConsumerApi/test/ConsumerApi.Tests.Performance/scripts/windows/load-snapshot.ps1 index 3e70d3894e..e64ae2c5b7 100644 --- a/Applications/ConsumerApi/test/ConsumerApi.Tests.Performance/scripts/windows/load-snapshot.ps1 +++ b/Applications/ConsumerApi/test/ConsumerApi.Tests.Performance/scripts/windows/load-snapshot.ps1 @@ -18,12 +18,10 @@ if (-not $SnapshotName) { $repoRoot = git rev-parse --show-toplevel $snapshotZipPath = "$repoRoot\Applications\ConsumerApi\test\ConsumerApi.Tests.Performance\snapshots\$SnapshotName.zip" -# Check if the file exists if (-not (Test-Path $snapshotZipPath)) { throw "Snapshot file '$SnapshotName' not found in the 'snapshots' folder." } - # Extract the zip file try { Write-Host "Extracting '$SnapshotName'..." diff --git a/scripts/windows/dumps/dump-postgres.ps1 b/scripts/windows/dumps/dump-postgres.ps1 index 927b72b680..66bede7ceb 100644 --- a/scripts/windows/dumps/dump-postgres.ps1 +++ b/scripts/windows/dumps/dump-postgres.ps1 @@ -3,38 +3,19 @@ param ( [string]$Hostname = "host.docker.internal", [string]$Username = "postgres", [string]$Password = "admin", - [string]$DbName = "enmeshed", - [string]$DumpFile = "enmeshed.pg" + [string]$DbName = "enmeshed" ) -$ContainerName = "tmp-postgres-container" +$DumpFile = "enmeshed.pg" -# Run a PostgreSQL container -$volumeArg = ($PSScriptRoot + "\dump-files") + ":/tmp/df"; -Write-Host "Creating container $ContainerName for pg_dump execution" -docker container run --name $ContainerName -v $volumeArg -e POSTGRES_PASSWORD="admin" -d postgres +docker run --rm -v "$PSScriptRoot\dump-files:/dump" --env PGPASSWORD="admin" postgres pg_dump -h $Hostname -U $Username $DbName -f /dump/$DumpFile -# Perform the dump -docker container exec --env PGPASSWORD=$Password -it $ContainerName pg_dump -h $Hostname -U $Username $DbName -f /tmp/df/$DumpFile - -if ($LASTEXITCODE -eq 0) { - Write-Host "Database export to $DumpFile successful." - - # Check if the file was successfully copied - if (Test-Path "./dump-files/$DumpFile") { - Write-Host "File found at ./dump-files/$DumpFile" - } - else { - Write-Host "Error: Failed to locate the $DumpFile file on the host." - } -} -else { - Write-Host "Error: Database export to $DumpFile failed." +if ($LASTEXITCODE -ne 0) { + throw "Error: Database export to $DumpFile failed." } +if (-not (Test-Path $PSScriptRoot\dump-files\$DumpFile)) { + throw "Snapshot file not found in the 'snapshots' folder." +} -# Stop and remove the container -docker container stop $ContainerName -docker container rm $ContainerName - -Write-Host "Container $ContainerName stopped and removed" +Write-Host "Database export to $DumpFile successful." diff --git a/scripts/windows/dumps/dump-sqlserver.ps1 b/scripts/windows/dumps/dump-sqlserver.ps1 index 5ab57dc2c9..12cfc61a61 100644 --- a/scripts/windows/dumps/dump-sqlserver.ps1 +++ b/scripts/windows/dumps/dump-sqlserver.ps1 @@ -2,38 +2,20 @@ param ( [string]$Hostname = "host.docker.internal", [string]$Username = "SA", - [string]$DbName = "enmeshed", - [string]$DumpFile = "enmeshed.bacpac", # Use .bacpac as extension [string]$Password = "Passw0rd" + [string]$DbName = "enmeshed", ) -$ContainerName = "tmp-mssql-server" -$HostDumpDir = "./dump-files" # Directory on the host where the .bacpac file is located -$ContainerDumpDir = "/tmp/df" # Directory in the container where the .bacpac will be accessible +$DumpFile = "enmeshed.bacpac" -$volumeArg = ($PSScriptRoot + "\" + $HostDumpDir) + ":/" + $ContainerDumpDir; +docker run --rm -v "$PSScriptRoot\dump-files:/dump" ormico/sqlpackage sqlpackage /Action:Export /SourceServerName:$Hostname /SourceDatabaseName:$DbName /TargetFile:/tmp/$DumpFile /SourceUser:$Username /SourcePassword:$Password /SourceTrustServerCertificate:True -docker container run -v $volumeArg -ti --name $ContainerName ormico/sqlpackage sqlpackage /Action:Export /SourceServerName:$Hostname /SourceDatabaseName:$DbName /TargetFile:/tmp/$DumpFile /SourceUser:$Username /SourcePassword:$Password /SourceTrustServerCertificate:True - -# Check if the export command was successful -if ($LASTEXITCODE -eq 0) { - Write-Host "Database export to BACPAC successful. Proceeding to copy the file..." - - # Copy the BACPAC file to the host - docker cp "${ContainerName}:/tmp/$DumpFile" "./dump-files/$DumpFile" - - # Check if the file was successfully copied - if (Test-Path "./dump-files/$DumpFile") { - Write-Host "Database export completed and saved to ./dump-files/$DumpFile" - } - else { - Write-Host "Error: Failed to copy the BACPAC file to the host." - } +if ($LASTEXITCODE -ne 0) { + throw "Error: Database export to $DumpFile failed." } -else { - Write-Host "Error: Database export to BACPAC failed." + +if (-not (Test-Path $PSScriptRoot\dump-files\$DumpFile)) { + throw "Snapshot file not found in the 'snapshots' folder." } -# Stop and remove the container -docker stop $ContainerName -docker rm $ContainerName +Write-Host "Database export to $DumpFile successful." diff --git a/scripts/windows/dumps/load-postgres.ps1 b/scripts/windows/dumps/load-postgres.ps1 index 2aab1858b2..7cc1e7bb90 100644 --- a/scripts/windows/dumps/load-postgres.ps1 +++ b/scripts/windows/dumps/load-postgres.ps1 @@ -9,7 +9,7 @@ param ( $ContainerName = "tmp-postgres-container" -docker run -d --rm --name $ContainerName -v $PSScriptRoot\dump-files:/dump -e POSTGRES_PASSWORD="admin" postgres +docker run -d --rm --name $ContainerName -v "$PSScriptRoot\dump-files:/dump" -e POSTGRES_PASSWORD="admin" postgres docker exec --env PGPASSWORD=$Password -it $containerName psql -h $Hostname -U $Username postgres -c "DROP DATABASE IF EXISTS $DbName" docker exec --env PGPASSWORD=$Password -it $containerName psql -h $Hostname -U $Username postgres -c "CREATE DATABASE $DbName" diff --git a/scripts/windows/dumps/load-sqlserver.ps1 b/scripts/windows/dumps/load-sqlserver.ps1 index 72ca308645..f6b823326f 100644 --- a/scripts/windows/dumps/load-sqlserver.ps1 +++ b/scripts/windows/dumps/load-sqlserver.ps1 @@ -9,7 +9,7 @@ param ( $ContainerName = "tmp-mssql-server" -docker run -d --rm --name $ContainerName -v $PSScriptRoot\dump-files:/dump ormico/sqlpackage sleep infinity +docker run -d --rm --name $ContainerName -v "$PSScriptRoot\dump-files:/dump" ormico/sqlpackage sleep infinity docker exec -it $ContainerName /opt/mssql-tools/bin/sqlcmd -S $Hostname -U $Username -P $Password -Q "DROP DATABASE IF EXISTS $DbName" docker exec -ti $ContainerName sqlpackage /Action:Import /TargetServerName:$Hostname /TargetDatabaseName:$DbName /SourceFile:$ContainerDumpDir/$DumpFile /TargetUser:$Username /TargetPassword:$Password /TargetTrustServerCertificate:True