Skip to content

Commit 665ae20

Browse files
authored
Merge pull request #66 from webmd-health-services/feature/Checkpoint-Migration-Revision
Feature/checkpoint migration revision
2 parents 5ef5abf + a7f3ed1 commit 665ae20

File tree

10 files changed

+435
-225
lines changed

10 files changed

+435
-225
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
<!--markdownlint-disable MD012 no-multiple-blanks -->
22
<!--markdownlint-disable MD024 no-duplicate-heading/no-duplicate-header -->
33

4+
# 0.19.0
5+
6+
* `Checkpoint-Migration` will no longer delete migration scripts that have been pushed to the database. Instead it
7+
will now export rows from the `rivet.Migrations` table and include them in the `schema.ps1` file.
8+
* Added an `InitializeSchema` switch to the `Invoke-Rivet` script that is used to initialize database(s) with the
9+
`schema.ps1` file that is generated from `Checkpoint-Migration`.
10+
11+
412
# 0.18.0
513

614
* Fixed: Rivet doesn't use the CommandTimeout property in rivet.json configuration file.

Rivet/Functions/Checkpoint-Migration.ps1

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,13 @@ function Checkpoint-Migration
4646
return
4747
}
4848

49-
$query = @"
50-
SELECT CONCAT( FORMAT(ID, '00000000000000'), '_', Name) as MigrationFileName
51-
FROM rivet.Migrations
52-
WHERE ID > $($script:firstMigrationId)
53-
"@
54-
55-
try
56-
{
57-
Connect-Database -SqlServerName $settings.SqlServerName `
58-
-Database $databaseItem.Name `
59-
-ConnectionTimeout $settings.ConnectionTimeout
60-
61-
$pushedMigrations = Invoke-Query -Query $query
62-
}
63-
finally
64-
{
65-
Disconnect-Database
66-
}
67-
6849
Write-Debug "Checkpoint-Migration: Exporting migration on database $($databaseItem.Name)"
69-
$migration = Export-Migration -SqlServerName $settings.SqlServerName -Database $databaseItem.Name -ConfigFilePath $ConfigFilePath
50+
$migration = Export-Migration -SqlServerName $settings.SqlServerName `
51+
-Database $databaseItem.Name `
52+
-ConfigFilePath $ConfigFilePath `
53+
-Checkpoint
54+
7055
$migration = $migration -join [Environment]::NewLine
7156
Set-Content -Path $OutputPath -Value $migration
72-
73-
foreach( $migration in $pushedMigrations )
74-
{
75-
$migrationFilePath = Join-Path -Path $databaseItem.MigrationsRoot -ChildPath "$($migration.MigrationFileName).ps1"
76-
Remove-Item -Path $migrationFilePath
77-
}
7857
}
7958
}

Rivet/Functions/Export-Migration.ps1

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ function Export-Migration
4141
# The path to the Rivet configuration file to load. Defaults to `rivet.json` in the current directory.
4242
[String] $ConfigFilePath,
4343

44-
[Switch] $NoProgress
44+
[Switch] $NoProgress,
45+
46+
# Checkpoints the current state of the database so that it can be re-created.
47+
[Switch] $Checkpoint
4548
)
4649

4750
Set-StrictMode -Version 'Latest'
@@ -1619,6 +1622,30 @@ where
16191622
$exportedXmlSchemas[$ID] = $true
16201623
}
16211624

1625+
$rivetMigrationsTableQuery = "
1626+
SELECT *
1627+
FROM rivet.Migrations
1628+
WHERE ID > $($script:firstMigrationId)"
1629+
1630+
function Export-RivetMigrationsTable
1631+
{
1632+
[CmdletBinding()]
1633+
param()
1634+
1635+
foreach( $row in $rivetMigrationsTableData )
1636+
{
1637+
@"
1638+
Add-Row -SchemaName 'rivet' -TableName 'Migrations' -Column @{
1639+
ID = '$($row.Id)';
1640+
Name = '$($row.Name)';
1641+
Who = '$($row.Who)';
1642+
ComputerName = '$($row.ComputerName)';
1643+
AtUtc = '$($row.AtUtc.ToString("MM/dd/yyyy HH:mm:ss.fffffff"))';
1644+
}
1645+
"@
1646+
}
1647+
}
1648+
16221649
function Push-PopOperation
16231650
{
16241651
param(
@@ -1949,6 +1976,11 @@ where
19491976
Export-DataType
19501977
Export-Object
19511978
Export-Index
1979+
if( $Checkpoint )
1980+
{
1981+
$rivetMigrationsTableData = Invoke-Query -Query $rivetMigrationsTableQuery
1982+
Export-RivetMigrationsTable
1983+
}
19521984
'}'
19531985
''
19541986
'function Pop-Migration'

Rivet/Functions/Invoke-Rivet.ps1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function Invoke-Rivet
6363
[Parameter(ParameterSetName='Redo')]
6464
[Parameter(ParameterSetName='DropDatabase')]
6565
[Parameter(ParameterSetName='Checkpoint')]
66+
[Parameter(ParameterSetName='InitializeSchema')]
6667
[string[]]
6768
# The database(s) to migrate. Optional. Will operate on all databases otherwise.
6869
$Database,
@@ -76,6 +77,7 @@ function Invoke-Rivet
7677
[Parameter(ParameterSetName='Redo')]
7778
[Parameter(ParameterSetName='DropDatabase')]
7879
[Parameter(ParameterSetName='Checkpoint')]
80+
[Parameter(ParameterSetName='InitializeSchema')]
7981
[string]
8082
# The environment you're working in. Controls which settings Rivet loads from the `rivet.json` configuration file.
8183
$Environment,
@@ -89,6 +91,7 @@ function Invoke-Rivet
8991
[Parameter(ParameterSetName='Redo')]
9092
[Parameter(ParameterSetName='DropDatabase')]
9193
[Parameter(ParameterSetName='Checkpoint')]
94+
[Parameter(ParameterSetName='InitializeSchema')]
9295
[string]
9396
# 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.
9497
$ConfigFilePath,
@@ -101,7 +104,12 @@ function Invoke-Rivet
101104
[Parameter(ParameterSetName='Checkpoint')]
102105
[Switch]
103106
# Checkpoints the current state of the database so that it can be re-created.
104-
$Checkpoint
107+
$Checkpoint,
108+
109+
[Parameter(ParameterSetName='InitializeSchema')]
110+
[Switch]
111+
# Initializes the database, including baseline schema. Use the -Checkpoint switch to create a database baseline.
112+
$InitializeSchema
105113
)
106114

107115
Set-StrictMode -Version 'Latest'
@@ -193,6 +201,10 @@ Found no databases to migrate. This can be a few things:
193201
try
194202
{
195203
Initialize-Database -Configuration $settings
204+
if( $InitializeSchema )
205+
{
206+
continue
207+
}
196208

197209
$updateParams = @{
198210
Path = $dbMigrationsPath;

Rivet/Rivet.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
RootModule = 'Rivet.psm1'
1212

1313
# Version number of this module.
14-
ModuleVersion = '0.18.0'
14+
ModuleVersion = '0.19.0'
1515

1616
# ID used to uniquely identify this module
1717
GUID = '8af34b47-259b-4630-a945-75d38c33b94d'

Rivet/rivet.ps1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ param(
130130
[Parameter(ParameterSetName='Redo')]
131131
[Parameter(ParameterSetName='DropDatabase')]
132132
[Parameter(ParameterSetName='Checkpoint')]
133+
[Parameter(ParameterSetName='InitializeSchema')]
133134
[string[]]
134135
# The database(s) to migrate. Optional. Will operate on all databases otherwise.
135136
$Database,
@@ -143,6 +144,7 @@ param(
143144
[Parameter(ParameterSetName='Redo')]
144145
[Parameter(ParameterSetName='DropDatabase')]
145146
[Parameter(ParameterSetName='Checkpoint')]
147+
[Parameter(ParameterSetName='InitializeSchema')]
146148
[string]
147149
# The environment you're working in. Controls which settings Rivet loads from the `rivet.json` configuration file.
148150
$Environment,
@@ -156,6 +158,7 @@ param(
156158
[Parameter(ParameterSetName='Redo')]
157159
[Parameter(ParameterSetName='DropDatabase')]
158160
[Parameter(ParameterSetName='Checkpoint')]
161+
[Parameter(ParameterSetName='InitializeSchema')]
159162
[string]
160163
# 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.
161164
$ConfigFilePath,
@@ -168,7 +171,12 @@ param(
168171
[Parameter(ParameterSetName='Checkpoint')]
169172
[Switch]
170173
# Checkpoints the current state of the database so that it can be re-created.
171-
$Checkpoint
174+
$Checkpoint,
175+
176+
[Parameter(ParameterSetName='InitializeSchema')]
177+
[Switch]
178+
# Initializes the database, including baseline schema. Use the -Checkpoint switch to create a database baseline.
179+
$InitializeSchema
172180
)
173181

174182
Set-StrictMode -Version Latest

0 commit comments

Comments
 (0)