[CmdletBinding()] param( [Parameter(Mandatory = $true)][ValidateSet('verify-source', 'preview-source', 'import-source')][string]$Command, [string]$SourceDatabase, [string]$SourceServer, [string]$IdempotencyKey ) $ErrorActionPreference = 'Stop' $settingsPath = Join-Path $PSScriptRoot 'backend\appsettings.Production.json' $transfer = Join-Path $PSScriptRoot 'tools\Inogen.DataTransfer\Inogen.DataTransfer.exe' if (-not (Test-Path -LiteralPath $settingsPath) -or -not (Test-Path -LiteralPath $transfer)) { throw 'This command requires the internal package and its Production configuration.' } $settings = Get-Content -LiteralPath $settingsPath -Raw | ConvertFrom-Json $targetServer = @($settings.TargetDatabaseGuard.AllowedServers) $targetDatabase = @($settings.TargetDatabaseGuard.AllowedDatabases) if ($targetServer.Count -ne 1 -or $targetDatabase.Count -ne 1 -or [string]::IsNullOrWhiteSpace($targetServer[0]) -or [string]::IsNullOrWhiteSpace($targetDatabase[0])) { throw 'Configure exactly one target server and database in backend/appsettings.Production.json.' } # The private runtime connection is the same destination selected for customer installation. $targetConnection = $env:ConnectionStrings__TargetDatabase if ([string]::IsNullOrWhiteSpace($targetConnection)) { throw 'Set the private ConnectionStrings__TargetDatabase environment variable for this installation.' } $target = New-Object System.Data.SqlClient.SqlConnectionStringBuilder($targetConnection) if ($target.DataSource -ine $targetServer[0] -or $target.InitialCatalog -ine $targetDatabase[0]) { throw 'The configured runtime connection and target allowlist refer to different databases.' } if ([string]::IsNullOrWhiteSpace($SourceServer)) { $SourceServer = Read-Host 'Exact old SQL Server name' } if ([string]::IsNullOrWhiteSpace($SourceDatabase)) { $SourceDatabase = Read-Host 'Exact old database name' } if ([string]::IsNullOrWhiteSpace($SourceServer) -or [string]::IsNullOrWhiteSpace($SourceDatabase)) { throw 'Source server and database names are required.' } if ($SourceServer -ieq $targetServer[0] -and $SourceDatabase -ieq $targetDatabase[0]) { throw 'Source and destination must be different databases.' } # Credentials are supplied privately; only the exact operator-selected source is accepted. $sourceConnection = $env:INOGEN_SOURCE_READONLY_CONNECTION if ([string]::IsNullOrWhiteSpace($sourceConnection)) { throw 'Set INOGEN_SOURCE_READONLY_CONNECTION with ApplicationIntent=ReadOnly before running.' } $source = New-Object System.Data.SqlClient.SqlConnectionStringBuilder($sourceConnection) if ($source.DataSource -ine $SourceServer -or $source.InitialCatalog -ine $SourceDatabase -or $source.ApplicationIntent -ne [System.Data.SqlClient.ApplicationIntent]::ReadOnly) { throw 'Source connection must match the entered server/database and use ApplicationIntent=ReadOnly.' } if ($Command -eq 'import-source') { if ([string]::IsNullOrWhiteSpace($IdempotencyKey)) { throw 'Supply an IdempotencyKey after reviewing the source preview.' } $selection = Read-Host "Import from '$SourceServer/$SourceDatabase' to '$($targetServer[0])/$($targetDatabase[0])'? Type the source database name" if ($selection -cne $SourceDatabase) { throw 'Source confirmation did not match; import stopped.' } } try { $env:INOGEN_TARGET_CONNECTION = $targetConnection $env:INOGEN_TARGET_ALLOWED_SERVER = $targetServer[0] $env:INOGEN_TARGET_ALLOWED_DATABASE = $targetDatabase[0] $env:INOGEN_SOURCE_ALLOWED_SERVER = $SourceServer $env:INOGEN_SOURCE_ALLOWED_DATABASE = $SourceDatabase if ($Command -eq 'import-source') { $env:INOGEN_MIGRATION_IDEMPOTENCY_KEY = $IdempotencyKey } Write-Host "Source: $SourceServer/$SourceDatabase" Write-Host "Target: $($targetServer[0])/$($targetDatabase[0])" & $transfer $Command if ($LASTEXITCODE -ne 0) { throw "$Command failed; no other source was selected." } } finally { foreach ($name in @('INOGEN_TARGET_CONNECTION', 'INOGEN_TARGET_ALLOWED_SERVER', 'INOGEN_TARGET_ALLOWED_DATABASE', 'INOGEN_SOURCE_ALLOWED_SERVER', 'INOGEN_SOURCE_ALLOWED_DATABASE', 'INOGEN_MIGRATION_IDEMPOTENCY_KEY')) { Remove-Item "Env:$name" -ErrorAction SilentlyContinue } }