HOW TO Synchronize a tenant database schema in a NAV Container

In order to work with NAV containers I highly recommend to use NAVContainerHelper PowerShell module.
In the version 0.5.0.0 a new really powerfull function Invoke-ScriptInNavContainer
has been released, it allows you to invoke a PowerShell scriptblock in a NAV container.

Using NAVContainerHelper you do not have to care about loading NAV modules because they are already part of it and loaded, so to return to the title of this post, the script to synchronize database schema is:

$containerName = "scadev"
Invoke-ScriptInNavContainer -containerName $containerName -ScriptBlock {        
    Get-NAVTenant NAV | Sync-NavTenant -Mode Sync -Force 
}

Invoke-ScriptInNavContainer has two mandatory parameters, the name of the container and the script block. In the script block I’m using two standard PowerShell functions included in the NAV module Microsoft.Dynamics.Nav.Management:

  1. Get-NAVTenant to retrieve the tenant
  2. Sync-NavTenant to synchronizes the database schema

Did my HOW TO help you? Leave a reply.

HOW TO Refresh NAV database in NAV Container without the need to Recreate the container

This post actually started with an Issue I created in NavContainerHelper GitHub.

💡 I’m working in a script for Continuous Integration and although the creation of a container requires few minutes I was looking how to optimize the time of execution of the whole build process.

Freddy Kristiansen is a Microsoft Evangelist that is working hard to make it easier to work with NAV using Docker Containers, thanks again Freddy.

Freddy initially marked my issue with label “WONTFIX” 🙁 but after I explained to him my motivation he changed the label to “ENHANCEMENT” 😀

He did not include any new functionality to NavContainerHelper but he kindly provided me with the script to copy and paste, and as I always do, I like to share good things.

Solution:
Right after the creation of a NAV Container, run the following script:

# To add just after the creation of the NAV Container
$containerName = "scadev"
$config = Get-NavContainerServerConfiguration -ContainerName $containerName
Invoke-ScriptInNavContainer -containerName $containerName -scriptblock { 
  Param($DatabaseServer, $DatabaseInstance, $DatabaseName, $NewDatabaseName)
    Copy-navDatabase -DatabaseServer $DatabaseServer -DatabaseInstance 
  $DatabaseInstance -SourceDatabaseName $DatabaseName -DestinationDatabaseName 
  $NewDatabaseName
} -argumentList $config.DatabaseServer, $config.DatabaseInstance, $config.DatabaseName, "backup"

So next time you run the build process, just verify that the Container exists and in case replace the database with the backup taken previously (it takes 10-20 seconds)

$containerName = "scadev"
if (Test-NavContainer $containerName) {
  $config = Get-NavContainerServerConfiguration -ContainerName $containerName
  Invoke-ScriptInNavContainer -containerName $containerName -scriptblock { 
    Param($DatabaseServer, $DatabaseInstance, $DatabaseName, $NewDatabaseName)
    Copy-navDatabase -DatabaseServer $DatabaseServer -DatabaseInstance 
    $DatabaseInstance -SourceDatabaseName $DatabaseName -DestinationDatabaseName 
    $NewDatabaseName
  } -argumentList $config.DatabaseServer, $config.DatabaseInstance, "backup", $config.DatabaseName
} else {
# Creation of NAV Container
}

Source: https://github.com/Microsoft/navcontainerhelper/issues/315

Update 08/09/2019: With the release of ContainerHelper 0.6.4.1 the logic described in this post has been integrated as a new feature as described in Freddy’s post section “Speed up repetitive container generation
https://freddysblog.com/2019/09/08/containerhelper-0-6-4-1/

Did my HOW TO help you? Leave a reply.