Skip to content

Commit

Permalink
refactor: simplify scripts even further
Browse files Browse the repository at this point in the history
  • Loading branch information
tnotheis committed Oct 1, 2024
1 parent 99e3ed0 commit e752d92
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'..."
Expand Down
37 changes: 9 additions & 28 deletions scripts/windows/dumps/dump-postgres.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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."
36 changes: 9 additions & 27 deletions scripts/windows/dumps/dump-sqlserver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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."
2 changes: 1 addition & 1 deletion scripts/windows/dumps/load-postgres.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion scripts/windows/dumps/load-sqlserver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e752d92

Please sign in to comment.