Modules/businessdev.ALbuild.Apps/Public/Start-BcContainerAppDataUpgrade.ps1

function Start-BcContainerAppDataUpgrade {
    <#
    .SYNOPSIS
        Runs the data upgrade for a published AL app version in a Business Central container.
 
    .DESCRIPTION
        When a tenant already holds data written by an EARLIER version of an app, installing the new
        version is refused - BC requires the app's upgrade codeunits to migrate that data first. That is
        Start-NAVAppDataUpgrade, and it is the step 'Install-NAVApp' tells you about only after failing:
        "an earlier version was already installed. Run Start-NAVAppDataUpgrade".
 
        Publish and sync the new version first (Publish-BcContainerApp -Sync, without -Install), then call
        this. It replaces the install for that version: a successful data upgrade leaves the app installed
        at the new version.
 
    .PARAMETER Name
        Container name.
 
    .PARAMETER AppName
        The app name.
 
    .PARAMETER AppVersion
        The published version to upgrade to. Omit to let BC pick the newest published version.
 
    .PARAMETER ServerInstance
        BC server instance. Default 'BC'.
 
    .PARAMETER Tenant
        Tenant. Default 'default'.
 
    .PARAMETER DockerExecutable
        The Docker executable to use (default 'docker').
 
    .EXAMPLE
        Start-BcContainerAppDataUpgrade -Name albmcp01480a -AppName '365 business Banking' -AppVersion 18.4.0.0
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory)] [Alias('ContainerName')] [string] $Name,
        [Parameter(Mandatory)] [string] $AppName,
        [string] $AppVersion,
        [string] $ServerInstance = 'BC',
        [string] $Tenant = 'default',
        [string] $DockerExecutable = 'docker'
    )
    if (-not $PSCmdlet.ShouldProcess($Name, "Data upgrade $AppName $AppVersion")) { return }
    $output = Invoke-BcContainerCommand -ContainerName $Name -DockerExecutable $DockerExecutable -Variables @{
        ServerInstance = $ServerInstance; AppName = $AppName; AppVersion = $AppVersion; Tenant = $Tenant
    } -ScriptBlock {
        $params = @{ ServerInstance = $ServerInstance; Name = $AppName; Tenant = $Tenant; ErrorAction = 'Stop' }
        if ($AppVersion) { $params['Version'] = $AppVersion }
        Start-NAVAppDataUpgrade @params
        Write-Output "Upgraded data for $AppName $AppVersion"
    }
    Write-ALbuildLog -Level Success ($output.Trim())
}