Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection String Parsing Enhancement for Upgrade Script #802

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

DmarshalTU
Copy link

Connection String Parsing Enhancement for Upgrade Script

Problem

The current upgrade script fails when parsing connection strings due to rigid string parsing that doesn't handle different connection string formats. This causes the upgrade process to fail with the error:

Exception calling "Substring" with "2" argument(s): "length ('-168') must be a non-negative value. (Parameter 'length') Actual value was -168."

Installation process:

Set-AzContext -Subscription SUB
wget https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh -version 8.0.303
$ENV:PATH="$HOME/.dotnet:$ENV:PATH"
dotnet tool install --global dotnet-ef --version 8.0.0
git clone https://github.com/Azure/Commercial-Marketplace-SaaS-Accelerator.git --depth 1
cd ./Commercial-Marketplace-SaaS-Accelerator/deployment
.\Upgrade.ps1 -WebAppNamePrefix "PREFIX" -ResourceGroupForDeployment "RSG"

Key Changes

  1. Improved String-Between Function:

    • Added null checks for string indices
    • Added try-catch block with detailed error messages
    • Handle cases where end delimiter isn't found
  2. Enhanced Connection String Parsing:

    • Support multiple format variations:
      • Data Source= or Server=
      • Initial Catalog= or Database=
      • User Id= or User=
      • Password= or pwd=
  3. Better Error Handling:

    • Added try-catch blocks around critical operations
    • Added validation for parsed components
    • Improved error messages with expected formats
    • Added cleanup in finally block

Code Changes

# Before
$Server = String-Between -source $ConnectionString -start "Data Source=" -end ";"
$Database = String-Between -source $ConnectionString -start "Initial Catalog=" -end ";"
$User = String-Between -source $ConnectionString -start "User Id=" -end ";"
$Pass = String-Between -source $ConnectionString -start "Password=" -end ";"

# After
try {
    $Server = String-Between -source $ConnectionString -start "Data Source=" -end ";"
    if (!$Server) { $Server = String-Between -source $ConnectionString -start "Server=" -end ";" }
    
    $Database = String-Between -source $ConnectionString -start "Initial Catalog=" -end ";"
    if (!$Database) { $Database = String-Between -source $ConnectionString -start "Database=" -end ";" }
    
    $User = String-Between -source $ConnectionString -start "User Id=" -end ";"
    if (!$User) { $User = String-Between -source $ConnectionString -start "User=" -end ";" }
    
    $Pass = String-Between -source $ConnectionString -start "Password=" -end ";"
    if (!$Pass) { $Pass = String-Between -source $ConnectionString -start "pwd=" -end ";" }

    if (!$Server -or !$Database -or !$User -or !$Pass) {
        throw "Could not parse all required connection string components"
    }
}
catch {
    Write-Host "Failed to parse connection string. Please verify the connection string format in KeyVault" -ForegroundColor Red
    Write-Host "Expected format: 'Data Source=server;Initial Catalog=database;User Id=user;Password=pass;'" -ForegroundColor Yellow
    throw
}

@DmarshalTU
Copy link
Author

@microsoft-github-policy-service agree company="CodeWizard"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant