Skip to content

Commit

Permalink
Give Option to Choose SQL PowerShell Module When Restoring From BacPac (
Browse files Browse the repository at this point in the history
#3763)

Fixes #3762 

This is a change to how Restore-BcDatabaseFromArtifacts chooses which
PowerShell module to use. I have added an optional parameter,
SqlModuleToUse, with two possible values, sqlps and sqlserver, and a
default value of sqlps to preserve existing functionality.

I've also added the same parameter to New-BcContainer.

---------

Co-authored-by: Matt Traxinger <[email protected]>
  • Loading branch information
MattTraxinger and Matt Traxinger authored Nov 16, 2024
1 parent c47d8ca commit 08e2f35
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions BC.HelperFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ function Get-ContainerHelperConfig {
"IsAzureDevOps" = ($env:TF_BUILD -eq "true")
"IsGitLab" = ($env:GITLAB_CI -eq "true")
"useApproximateVersion" = $false
"useSqlServerModule" = $false
}

if ($isInsider) {
Expand Down
16 changes: 11 additions & 5 deletions Bacpac/Restore-BcDatabaseFromArtifacts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
Include this parameter if you want to restore the database asynchronous. A file called <databasePrefix>databasescreated.txt will be created in the containerhelper folder when done
.Parameter sqlTimeout
SQL Timeout for database restore operations
.Parameter useSqlServerModule
Switch, forces the use of the sqlserver module instead of the sqlps module. Default is to use the sqlps module. The default can be changed in the bcContainerHelperConfig file by setting "useSqlServerModule" = $false.
#>
function Restore-BcDatabaseFromArtifacts {
Param(
Expand All @@ -39,7 +41,8 @@ function Restore-BcDatabaseFromArtifacts {
[string] $bakFile,
[switch] $multitenant,
[switch] $async,
[int] $sqlTimeout = -1
[int] $sqlTimeout = -1,
[switch] $useSqlServerModule = $bcContainerHelperConfig.useSqlServerModule
)

$telemetryScope = InitTelemetryScope -name $MyInvocation.InvocationName -parameterValues $PSBoundParameters -includeParameters @()
Expand All @@ -57,7 +60,7 @@ try {
$containerHelperPath = (Get-Item (Join-Path $PSScriptRoot "..\Import-BcContainerHelper.ps1")).FullName
Write-Host $containerHelperPath

$job = Start-Job -ScriptBlock { Param( $containerHelperPath, $artifactUrl, $databaseServer, $databaseInstance, $databasePrefix, $databaseName, $multitenant, $successFileName, $bakFile, $sqlTimeout )
$job = Start-Job -ScriptBlock { Param( $containerHelperPath, $artifactUrl, $databaseServer, $databaseInstance, $databasePrefix, $databaseName, $multitenant, $successFileName, $bakFile, $sqlTimeout, $useSqlServerModule )
$ErrorActionPreference = "Stop"
try {
. "$containerHelperPath"
Expand Down Expand Up @@ -98,8 +101,11 @@ try {
if ($multitenant) {
$dbName = "$($databasePrefix)tenant"
}
Import-Module sqlps -ErrorAction SilentlyContinue
$sqlpsModule = get-module sqlps
$sqlpsModule = $null
if(-not $useSqlServerModule) {
Import-Module sqlps -ErrorAction SilentlyContinue
$sqlpsModule = get-module sqlps
}
if (-not $sqlpsModule) {
import-module SqlServer
$SqlModule = get-module SqlServer
Expand Down Expand Up @@ -222,7 +228,7 @@ try {
throw
}

} -ArgumentList $containerHelperPath, $artifactUrl, $databaseServer, $databaseInstance, $databasePrefix, $databaseName, $multitenant, $successFileName, $bakFile, $sqlTimeout
} -ArgumentList $containerHelperPath, $artifactUrl, $databaseServer, $databaseInstance, $databasePrefix, $databaseName, $multitenant, $successFileName, $bakFile, $sqlTimeout, $useSqlServerModule.IsPresent

if (!$async) {
While ($job.State -eq "Running") {
Expand Down
7 changes: 5 additions & 2 deletions ContainerHandling/New-NavContainer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@
Use Get-AlLanguageExtensionFromArtifacts -artifactUrl (Get-BCArtifactUrl -select NextMajor -accept_insiderEula) to get latest insider .vsix
.Parameter sqlTimeout
SQL Timeout for database restore operations
.Parameter useSqlServerModule
Switch, forces the use of the sqlserver module instead of the sqlps module. Default is to use the sqlps module. The default can be changed in the bcContainerHelperConfig file by setting "useSqlServerModule" = $false.
.Example
New-BcContainer -accept_eula -containerName test
.Example
Expand Down Expand Up @@ -287,7 +289,8 @@ function New-BcContainer {
[switch] $doNotUseRuntimePackages = $true,
[string] $vsixFile = "",
[string] $applicationInsightsKey,
[scriptblock] $finalizeDatabasesScriptBlock
[scriptblock] $finalizeDatabasesScriptBlock,
[switch] $useSqlServerModule = $bcContainerHelperConfig.useSqlServerModule
)

$telemetryScope = InitTelemetryScope `
Expand Down Expand Up @@ -526,7 +529,7 @@ try {
$multitenant = $bcContainerHelperConfig.sandboxContainersAreMultitenantByDefault
}
Remove-BcDatabase -databaseServer $databaseServer -databaseInstance $databaseInstance -databaseName "$($databasePrefix)%"
Restore-BcDatabaseFromArtifacts -artifactUrl $artifactUrl -databaseServer $databaseServer -databaseInstance $databaseInstance -databasePrefix $databasePrefix -databaseName $databaseName -multitenant:$multitenant -bakFile $bakFile -async
Restore-BcDatabaseFromArtifacts -artifactUrl $artifactUrl -databaseServer $databaseServer -databaseInstance $databaseInstance -databasePrefix $databasePrefix -databaseName $databaseName -multitenant:$multitenant -bakFile $bakFile -useSqlServerModule:$useSqlServerModule.IsPresent -async
$createTenantAndUserInExternalDatabase = $true
$bakFile = ""
$successFileName = Join-Path $bcContainerHelperConfig.containerHelperFolder "$($databasePrefix)databasescreated.txt"
Expand Down
4 changes: 4 additions & 0 deletions ReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
6.0.30
Issue 3762 Give Option to Choose SQL PowerShell Module When Restoring From BacPac
There are instances where sqlps does not work as expected when it is installed. This change adds a switch parameter, useSqlServerModule, to Restore-BcDatabaseFromArtifacts, New-NavContainer, and the BcContainerHelper config file.

6.0.29
Issue 3591 When using Publish-NAVApp to publish an app, which fails compilation in the service, the command might hang forever - the fix for this is a temporary hack put in place for the versions which doesn't work.
Improve performance and reduce memory consumption when creating and pushing NuGet packages
Expand Down

0 comments on commit 08e2f35

Please sign in to comment.