Skip to content

Commit

Permalink
Merge pull request #66 from webmd-health-services/feature/Checkpoint-…
Browse files Browse the repository at this point in the history
…Migration-Revision

Feature/checkpoint migration revision
  • Loading branch information
KhoiKy authored Apr 3, 2023
2 parents 5ef5abf + a7f3ed1 commit 665ae20
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 225 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<!--markdownlint-disable MD012 no-multiple-blanks -->
<!--markdownlint-disable MD024 no-duplicate-heading/no-duplicate-header -->

# 0.19.0

* `Checkpoint-Migration` will no longer delete migration scripts that have been pushed to the database. Instead it
will now export rows from the `rivet.Migrations` table and include them in the `schema.ps1` file.
* Added an `InitializeSchema` switch to the `Invoke-Rivet` script that is used to initialize database(s) with the
`schema.ps1` file that is generated from `Checkpoint-Migration`.


# 0.18.0

* Fixed: Rivet doesn't use the CommandTimeout property in rivet.json configuration file.
Expand Down
31 changes: 5 additions & 26 deletions Rivet/Functions/Checkpoint-Migration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,13 @@ function Checkpoint-Migration
return
}

$query = @"
SELECT CONCAT( FORMAT(ID, '00000000000000'), '_', Name) as MigrationFileName
FROM rivet.Migrations
WHERE ID > $($script:firstMigrationId)
"@

try
{
Connect-Database -SqlServerName $settings.SqlServerName `
-Database $databaseItem.Name `
-ConnectionTimeout $settings.ConnectionTimeout

$pushedMigrations = Invoke-Query -Query $query
}
finally
{
Disconnect-Database
}

Write-Debug "Checkpoint-Migration: Exporting migration on database $($databaseItem.Name)"
$migration = Export-Migration -SqlServerName $settings.SqlServerName -Database $databaseItem.Name -ConfigFilePath $ConfigFilePath
$migration = Export-Migration -SqlServerName $settings.SqlServerName `
-Database $databaseItem.Name `
-ConfigFilePath $ConfigFilePath `
-Checkpoint

$migration = $migration -join [Environment]::NewLine
Set-Content -Path $OutputPath -Value $migration

foreach( $migration in $pushedMigrations )
{
$migrationFilePath = Join-Path -Path $databaseItem.MigrationsRoot -ChildPath "$($migration.MigrationFileName).ps1"
Remove-Item -Path $migrationFilePath
}
}
}
34 changes: 33 additions & 1 deletion Rivet/Functions/Export-Migration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ function Export-Migration
# The path to the Rivet configuration file to load. Defaults to `rivet.json` in the current directory.
[String] $ConfigFilePath,

[Switch] $NoProgress
[Switch] $NoProgress,

# Checkpoints the current state of the database so that it can be re-created.
[Switch] $Checkpoint
)

Set-StrictMode -Version 'Latest'
Expand Down Expand Up @@ -1619,6 +1622,30 @@ where
$exportedXmlSchemas[$ID] = $true
}

$rivetMigrationsTableQuery = "
SELECT *
FROM rivet.Migrations
WHERE ID > $($script:firstMigrationId)"

function Export-RivetMigrationsTable
{
[CmdletBinding()]
param()

foreach( $row in $rivetMigrationsTableData )
{
@"
Add-Row -SchemaName 'rivet' -TableName 'Migrations' -Column @{
ID = '$($row.Id)';
Name = '$($row.Name)';
Who = '$($row.Who)';
ComputerName = '$($row.ComputerName)';
AtUtc = '$($row.AtUtc.ToString("MM/dd/yyyy HH:mm:ss.fffffff"))';
}
"@
}
}

function Push-PopOperation
{
param(
Expand Down Expand Up @@ -1949,6 +1976,11 @@ where
Export-DataType
Export-Object
Export-Index
if( $Checkpoint )
{
$rivetMigrationsTableData = Invoke-Query -Query $rivetMigrationsTableQuery
Export-RivetMigrationsTable
}
'}'
''
'function Pop-Migration'
Expand Down
14 changes: 13 additions & 1 deletion Rivet/Functions/Invoke-Rivet.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function Invoke-Rivet
[Parameter(ParameterSetName='Redo')]
[Parameter(ParameterSetName='DropDatabase')]
[Parameter(ParameterSetName='Checkpoint')]
[Parameter(ParameterSetName='InitializeSchema')]
[string[]]
# The database(s) to migrate. Optional. Will operate on all databases otherwise.
$Database,
Expand All @@ -76,6 +77,7 @@ function Invoke-Rivet
[Parameter(ParameterSetName='Redo')]
[Parameter(ParameterSetName='DropDatabase')]
[Parameter(ParameterSetName='Checkpoint')]
[Parameter(ParameterSetName='InitializeSchema')]
[string]
# The environment you're working in. Controls which settings Rivet loads from the `rivet.json` configuration file.
$Environment,
Expand All @@ -89,6 +91,7 @@ function Invoke-Rivet
[Parameter(ParameterSetName='Redo')]
[Parameter(ParameterSetName='DropDatabase')]
[Parameter(ParameterSetName='Checkpoint')]
[Parameter(ParameterSetName='InitializeSchema')]
[string]
# The path to the Rivet configuration file. Default behavior is to look in the current directory for a `rivet.json` file. See `about_Rivet_Configuration` for more information.
$ConfigFilePath,
Expand All @@ -101,7 +104,12 @@ function Invoke-Rivet
[Parameter(ParameterSetName='Checkpoint')]
[Switch]
# Checkpoints the current state of the database so that it can be re-created.
$Checkpoint
$Checkpoint,

[Parameter(ParameterSetName='InitializeSchema')]
[Switch]
# Initializes the database, including baseline schema. Use the -Checkpoint switch to create a database baseline.
$InitializeSchema
)

Set-StrictMode -Version 'Latest'
Expand Down Expand Up @@ -193,6 +201,10 @@ Found no databases to migrate. This can be a few things:
try
{
Initialize-Database -Configuration $settings
if( $InitializeSchema )
{
continue
}

$updateParams = @{
Path = $dbMigrationsPath;
Expand Down
2 changes: 1 addition & 1 deletion Rivet/Rivet.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
RootModule = 'Rivet.psm1'

# Version number of this module.
ModuleVersion = '0.18.0'
ModuleVersion = '0.19.0'

# ID used to uniquely identify this module
GUID = '8af34b47-259b-4630-a945-75d38c33b94d'
Expand Down
10 changes: 9 additions & 1 deletion Rivet/rivet.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ param(
[Parameter(ParameterSetName='Redo')]
[Parameter(ParameterSetName='DropDatabase')]
[Parameter(ParameterSetName='Checkpoint')]
[Parameter(ParameterSetName='InitializeSchema')]
[string[]]
# The database(s) to migrate. Optional. Will operate on all databases otherwise.
$Database,
Expand All @@ -143,6 +144,7 @@ param(
[Parameter(ParameterSetName='Redo')]
[Parameter(ParameterSetName='DropDatabase')]
[Parameter(ParameterSetName='Checkpoint')]
[Parameter(ParameterSetName='InitializeSchema')]
[string]
# The environment you're working in. Controls which settings Rivet loads from the `rivet.json` configuration file.
$Environment,
Expand All @@ -156,6 +158,7 @@ param(
[Parameter(ParameterSetName='Redo')]
[Parameter(ParameterSetName='DropDatabase')]
[Parameter(ParameterSetName='Checkpoint')]
[Parameter(ParameterSetName='InitializeSchema')]
[string]
# The path to the Rivet configuration file. Default behavior is to look in the current directory for a `rivet.json` file. See `about_Rivet_Configuration` for more information.
$ConfigFilePath,
Expand All @@ -168,7 +171,12 @@ param(
[Parameter(ParameterSetName='Checkpoint')]
[Switch]
# Checkpoints the current state of the database so that it can be re-created.
$Checkpoint
$Checkpoint,

[Parameter(ParameterSetName='InitializeSchema')]
[Switch]
# Initializes the database, including baseline schema. Use the -Checkpoint switch to create a database baseline.
$InitializeSchema
)

Set-StrictMode -Version Latest
Expand Down
Loading

0 comments on commit 665ae20

Please sign in to comment.