Skip to content

Commit 561a2db

Browse files
Merge pull request #68 from webmd-health-services/bugfix/schema-ps1-created-when-creating-migration
The schema.ps1 file created when creating migration
2 parents cccdca2 + 41ce47a commit 561a2db

File tree

7 files changed

+53
-36
lines changed

7 files changed

+53
-36
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
## 0.20.0
77

8+
> Released 19 May 2023
9+
810
### Added
911

1012
* `New-RivetSession` function for creating a Rivet session. A `Rivet_Session` is the object used by Rivet to keep track
@@ -21,6 +23,8 @@ of current state.
2123

2224
* Shows too many errors.
2325
* `Remove-ForeignKey` prompts for `TableName` and `ReferencesTableName` arguments.
26+
* Creating a new migration also creates an unused schema.ps1 file and writes a warning that it should be added to source
27+
control
2428

2529

2630
## 0.19.0

Rivet/Functions/Checkpoint-Migration.ps1

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,41 @@ function Checkpoint-Migration
4646

4747
foreach( $databaseItem in $Session.Databases )
4848
{
49-
$OutputPath = Join-Path -Path $databaseItem.MigrationsRoot -ChildPath "schema.ps1"
49+
$schemaPs1Path = Join-Path -Path $databaseItem.MigrationsRoot -ChildPath $script:schemaFileName
5050

51-
if ((Test-Path -Path $OutputPath) -and -not $Force)
51+
$schemaPs1Exists = Test-Path -Path $schemaPs1Path
52+
if (($schemaPs1Exists) -and -not $Force)
5253
{
53-
Write-Error "Checkpoint output path ""$($OutputPath)"" already exists. Use the -Force switch to overwrite."
54+
Write-Error "Checkpoint output path ""$($schemaPs1Path)"" already exists. Use the -Force switch to overwrite."
5455
return
5556
}
5657

57-
Write-Debug "Checkpoint-Migration: Exporting migration on database $($databaseItem.Name)"
58+
$databaseName = $databaseItem.Name
59+
Write-Debug "Checkpoint-Migration: Exporting migration on database ${databaseName}"
5860
$migration = Export-Migration -Session $Session -Database $databaseItem.Name -Checkpoint
5961
$migration = $migration -join [Environment]::NewLine
60-
Set-Content -Path $OutputPath -Value $migration
62+
Set-Content -Path $schemaPs1Path -Value $migration
63+
64+
if (-not $schemaPs1Exists)
65+
{
66+
$displayPath = $schemaPs1Path | Resolve-Path -Relative
67+
if ($displayPath -match '\.\.[\\/]')
68+
{
69+
$displayPath = $schemaPs1Path
70+
}
71+
if ($displayPath -match '\s')
72+
{
73+
$displayPath = """${displayPath}"""
74+
}
75+
76+
$displayName = $databaseName
77+
if ($displayName -match '\s')
78+
{
79+
$displayName = """${displayName}"""
80+
}
81+
$msg = "Rivet created the ${displayName} database's baseline schema file ${displayPath}. Please check " +
82+
'this file into source control.'
83+
Write-Information $msg -InformationAction Continue
84+
}
6185
}
6286
}

Rivet/Functions/Initialize-Database.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function Initialize-Database
2121

2222
# Add schema.ps1 file from database's migration directory if it exists
2323
$databaseItem = $Session.CurrentDatabase
24-
$schemaFilePath = Join-Path -Path $databaseItem.MigrationsRoot -ChildPath 'schema.ps1'
24+
$schemaFilePath = Join-Path -Path $databaseItem.MigrationsRoot -ChildPath $script:schemaFileName
2525
if( (Test-Path -Path $schemaFilePath) )
2626
{
2727
$migrationPaths.Add($schemaFilePath) | Out-Null

Rivet/Functions/New-Migration.ps1

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function New-Migration
44
<#
55
.SYNOPSIS
66
Creates a new migration script.
7-
7+
88
.DESCRIPTION
99
Creates a migration script with a given name. The script is prefixed with the current timestamp (e.g. yyyyMMddHHmmss). The script is created in `$Path\$Database\Migrations`.
1010
#>
@@ -13,7 +13,7 @@ function New-Migration
1313
[string[]]
1414
# The name of the migration to create.
1515
$Name,
16-
16+
1717
[Parameter(Mandatory=$true)]
1818
[string]
1919
# The path to the directory where the migration should be saved.
@@ -41,23 +41,14 @@ function New-Migration
4141
$migrationPath = [IO.Path]::GetFullPath( $migrationPath )
4242
New-Item -Path $migrationPath -Force -ItemType File
4343

44-
$schemaPs1Path = Join-Path -Path $Path -ChildPath $script:schemaFileName
45-
if (-not (Test-Path -Path $schemaPs1Path))
46-
{
47-
$script:defaultSchemaPs1Content | Set-Content -Path $schemaPs1Path
48-
$msg = "Rivet created ""$($schemaPs1Path | Resolve-Path -Relative)"", a file where Rivet stores the " +
49-
'database''s baseline schema. PLEASE CHECK THIS FILE INTO SOURCE CONTROL.'
50-
Write-Warning $msg
51-
}
52-
5344
$template = @"
5445
<#
55-
Your migration is ready to go! For the best development experience, please
56-
write your migration in the PowerShell 3 ISE. Run the following at a
46+
Your migration is ready to go! For the best development experience, please
47+
write your migration in the PowerShell 3 ISE. Run the following at a
5748
PowerShell prompt:
5849
5950
PS> ise "{0}"
60-
51+
6152
or right-click the migration in Windows Explorer and choose "Edit".
6253
6354
The PowerShell ISE gives you intellisense, auto-complete, and other features
@@ -66,7 +57,7 @@ import Rivet and get intellisense/auto-complete:
6657
6758
PSISE> {1}
6859
69-
The ISE has a "Show Command" add-on which will let you build your migration
60+
The ISE has a "Show Command" add-on which will let you build your migration
7061
with a GUI. Once you've got Rivet imported, choose View > Show Command Add-on.
7162
When the Show Command Add-on appears, choose 'Rivet' from the module. Click on
7263
a migration operation to build it with the Show Command GUI.
@@ -78,7 +69,7 @@ function Push-Migration
7869
function Pop-Migration
7970
{{
8071
}}
81-
"@ -f $migrationPath,$importRivetPath
72+
"@ -f $migrationPath,$importRivetPath
8273

8374
$template | Set-Content -Path $migrationPath
8475
}

Rivet/Rivet.psm1

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ class Rivet_Session
4040
[Rivet.Configuration.Database] $CurrentDatabase
4141
}
4242

43-
enum Rivet_QueryKeyword
44-
{
45-
Default = 1
46-
}
47-
4843
$RivetSchemaName = 'rivet'
4944
$RivetMigrationsTableName = 'Migrations'
5045
$RivetMigrationsTableFullName = "[$($RivetSchemaName)].[$($RivetMigrationsTableName)]"

Test/Checkpoint-Migration.Tests.ps1

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
#Requires -Version 5.1
23
Set-StrictMode -Version 'Latest'
34

@@ -121,7 +122,7 @@ BeforeAll {
121122
Migration = $schemaFileContents
122123
}
123124
$script:checkpointedMigrations.Add($checkpointedMigration)
124-
125+
125126
Remove-RivetTestDatabase -Name $databaseName
126127
# Now, check that the schema.ps1 script is runnable
127128
Invoke-RTRivet -InitializeSchema -Database $databaseName -ErrorAction Stop
@@ -151,7 +152,7 @@ BeforeAll {
151152
{
152153
foreach( $path in $script:migrationPaths )
153154
{
154-
Set-Content -Path (Join-Path -Path $path.Directory.FullName -ChildPath 'schema.ps1') -Value $existingSchemaContents
155+
Set-Content -Path (Join-Path -Path $path.Directory.FullName -ChildPath 'schema.ps1') -Value $existingSchemaContents
155156
}
156157
}
157158

@@ -218,7 +219,7 @@ function Pop-Migration
218219
'@ -ContainsRowsFor 'CheckpointMigration' -Database ($script:database)
219220
ThenNoErrors
220221
}
221-
222+
222223
It 'should fail when schema.ps1 file already exists' {
223224
GivenMigrationContent -Name 'CheckpointMigration' -Content @'
224225
function Push-Migration
@@ -237,7 +238,7 @@ function Pop-Migration
237238
WhenCheckpointingMigration -ExistingSchemaFile -ErrorAction SilentlyContinue
238239
ThenFailed -WithError 'schema.ps1" already exists.'
239240
}
240-
241+
241242
It 'should overwrite contents when schema.ps1 file already exists but -Force switch is given' {
242243
GivenMigrationContent -Name 'CheckpointMigration' -Content @'
243244
function Push-Migration
@@ -265,7 +266,7 @@ function Pop-Migration
265266
'@ -ContainsRowsFor 'CheckpointMigration' -Database $RTDatabaseName
266267
ThenNoErrors
267268
}
268-
269+
269270
It 'should pass when checkpointing a migration' {
270271
GivenMigrationContent -Name 'CheckpointMigration' -Content @'
271272
function Push-Migration
@@ -289,7 +290,7 @@ function Push-Migration
289290
New-Column -DataType 'text' -Name 'textcolumn' -Description 'a text column'
290291
New-Column -DataType 'ntext' -Name 'ntextcolumn' -NotNull
291292
New-Column -DataType 'image' -Name 'imagecolumn'
292-
New-Column -DataType 'sysname' -Name 'sysnamecolumn' -NotNull
293+
New-Column -DataType 'sysname' -Name 'sysnamecolumn' -NotNull
293294
New-Column -DataType 'sql_variant' -Name 'sql_variantcolumn' -Sparse
294295
New-Column -DataType 'CID' -Name 'CID' -NotNull
295296
varbinary 'VarBinDefault' -Size 1
@@ -374,7 +375,7 @@ ON [dbo].[Migrations] for insert as select 1
374375
ThenSchema -HasContent @'
375376
Add-Row -SchemaName 'rivet' -TableName 'Migrations' -Column @{
376377
'@ -ContainsRowsFor 'CheckpointMigration' -Database $RTDatabaseName
377-
378+
378379
ThenSchema -HasContent 'Remove-Table -Name ''Migrations''' -Database $RTDatabaseName
379380
ThenSchema -Not -HasContent 'Remove-Schema' -Database $RTDatabaseName
380381
ThenSchema -Not -HasContent 'Remove-PrimaryKey' -Database $RTDatabaseName
@@ -385,7 +386,7 @@ ON [dbo].[Migrations] for insert as select 1
385386
ThenSchema -Not -HasContent 'Remove-Trigger' -Database $RTDatabaseName
386387
ThenNoErrors
387388
}
388-
389+
389390
It 'should only checkpoint pushed migrations when there are multiple migrations but only one has been pushed' {
390391
GivenMigrationContent -Name 'CheckpointMigration' -Content @'
391392
function Push-Migration
@@ -477,7 +478,7 @@ function Pop-Migration
477478
'@ -ContainsRowsFor 'CheckpointMigration', 'CheckpointMigration2' -Database $RTDatabaseName
478479
ThenNoErrors
479480
}
480-
481+
481482
It 'should do nothing when no migrations have been pushed' {
482483
# Nothing to push here. Just running push here to initialize database with rivet.migrations table.
483484
Invoke-RTRivet -Push -Database $RTDatabaseName

Test/RivetTest/RivetTest.psm1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
using module '..\..\Rivet'
3+
24
param(
35
[String]$RivetRoot
46
)

0 commit comments

Comments
 (0)