Skip to content

Commit

Permalink
Fixed: creating a new migration also creates an unused schema.ps1, al…
Browse files Browse the repository at this point in the history
…ong with a warning to add schema.ps1 to source control.
  • Loading branch information
splatteredbits committed May 3, 2023
1 parent 2e05eaf commit c1e8d10
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

## 0.20.0

> Released 3 May 2023
### Added

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

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


## 0.19.0
Expand Down
34 changes: 29 additions & 5 deletions Rivet/Functions/Checkpoint-Migration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,41 @@ function Checkpoint-Migration

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

if ((Test-Path -Path $OutputPath) -and -not $Force)
$schemaPs1Exists = Test-Path -Path $schemaPs1Path
if (($schemaPs1Exists) -and -not $Force)
{
Write-Error "Checkpoint output path ""$($OutputPath)"" already exists. Use the -Force switch to overwrite."
Write-Error "Checkpoint output path ""$($schemaPs1Path)"" already exists. Use the -Force switch to overwrite."
return
}

Write-Debug "Checkpoint-Migration: Exporting migration on database $($databaseItem.Name)"
$databaseName = $databaseItem.Name
Write-Debug "Checkpoint-Migration: Exporting migration on database ${databaseName}"
$migration = Export-Migration -Session $Session -Database $databaseItem.Name -Checkpoint
$migration = $migration -join [Environment]::NewLine
Set-Content -Path $OutputPath -Value $migration
Set-Content -Path $schemaPs1Path -Value $migration

if (-not $schemaPs1Exists)
{
$displayPath = $schemaPs1Path | Resolve-Path -Relative
if ($displayPath -match '\.\.[\\/]')
{
$displayPath = $schemaPs1Path
}
if ($displayPath -match '\s')
{
$displayPath = """${displayPath}"""
}

$displayName = $databaseName
if ($displayName -match '\s')
{
$displayName = """${displayName}"""
}
$msg = "Rivet created the ${displayName} database's baseline schema file ${displayPath}. Please check " +
'this file into source control.'
Write-Information $msg -InformationAction Continue
}
}
}
2 changes: 1 addition & 1 deletion Rivet/Functions/Initialize-Database.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function Initialize-Database

# Add schema.ps1 file from database's migration directory if it exists
$databaseItem = $Session.CurrentDatabase
$schemaFilePath = Join-Path -Path $databaseItem.MigrationsRoot -ChildPath 'schema.ps1'
$schemaFilePath = Join-Path -Path $databaseItem.MigrationsRoot -ChildPath $script:schemaFileName
if( (Test-Path -Path $schemaFilePath) )
{
$migrationPaths.Add($schemaFilePath) | Out-Null
Expand Down
23 changes: 7 additions & 16 deletions Rivet/Functions/New-Migration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function New-Migration
<#
.SYNOPSIS
Creates a new migration script.
.DESCRIPTION
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`.
#>
Expand All @@ -13,7 +13,7 @@ function New-Migration
[string[]]
# The name of the migration to create.
$Name,

[Parameter(Mandatory=$true)]
[string]
# The path to the directory where the migration should be saved.
Expand Down Expand Up @@ -41,23 +41,14 @@ function New-Migration
$migrationPath = [IO.Path]::GetFullPath( $migrationPath )
New-Item -Path $migrationPath -Force -ItemType File

$schemaPs1Path = Join-Path -Path $Path -ChildPath $script:schemaFileName
if (-not (Test-Path -Path $schemaPs1Path))
{
$script:defaultSchemaPs1Content | Set-Content -Path $schemaPs1Path
$msg = "Rivet created ""$($schemaPs1Path | Resolve-Path -Relative)"", a file where Rivet stores the " +
'database''s baseline schema. PLEASE CHECK THIS FILE INTO SOURCE CONTROL.'
Write-Warning $msg
}

$template = @"
<#
Your migration is ready to go! For the best development experience, please
write your migration in the PowerShell 3 ISE. Run the following at a
Your migration is ready to go! For the best development experience, please
write your migration in the PowerShell 3 ISE. Run the following at a
PowerShell prompt:
PS> ise "{0}"
or right-click the migration in Windows Explorer and choose "Edit".
The PowerShell ISE gives you intellisense, auto-complete, and other features
Expand All @@ -66,7 +57,7 @@ import Rivet and get intellisense/auto-complete:
PSISE> {1}
The ISE has a "Show Command" add-on which will let you build your migration
The ISE has a "Show Command" add-on which will let you build your migration
with a GUI. Once you've got Rivet imported, choose View > Show Command Add-on.
When the Show Command Add-on appears, choose 'Rivet' from the module. Click on
a migration operation to build it with the Show Command GUI.
Expand All @@ -78,7 +69,7 @@ function Push-Migration
function Pop-Migration
{{
}}
"@ -f $migrationPath,$importRivetPath
"@ -f $migrationPath,$importRivetPath

$template | Set-Content -Path $migrationPath
}
Expand Down
19 changes: 10 additions & 9 deletions Test/Checkpoint-Migration.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#Requires -Version 5.1
Set-StrictMode -Version 'Latest'

Expand Down Expand Up @@ -121,7 +122,7 @@ BeforeAll {
Migration = $schemaFileContents
}
$script:checkpointedMigrations.Add($checkpointedMigration)

Remove-RivetTestDatabase -Name $databaseName
# Now, check that the schema.ps1 script is runnable
Invoke-RTRivet -InitializeSchema -Database $databaseName -ErrorAction Stop
Expand Down Expand Up @@ -151,7 +152,7 @@ BeforeAll {
{
foreach( $path in $script:migrationPaths )
{
Set-Content -Path (Join-Path -Path $path.Directory.FullName -ChildPath 'schema.ps1') -Value $existingSchemaContents
Set-Content -Path (Join-Path -Path $path.Directory.FullName -ChildPath 'schema.ps1') -Value $existingSchemaContents
}
}

Expand Down Expand Up @@ -218,7 +219,7 @@ function Pop-Migration
'@ -ContainsRowsFor 'CheckpointMigration' -Database ($script:database)
ThenNoErrors
}

It 'should fail when schema.ps1 file already exists' {
GivenMigrationContent -Name 'CheckpointMigration' -Content @'
function Push-Migration
Expand All @@ -237,7 +238,7 @@ function Pop-Migration
WhenCheckpointingMigration -ExistingSchemaFile -ErrorAction SilentlyContinue
ThenFailed -WithError 'schema.ps1" already exists.'
}

It 'should overwrite contents when schema.ps1 file already exists but -Force switch is given' {
GivenMigrationContent -Name 'CheckpointMigration' -Content @'
function Push-Migration
Expand Down Expand Up @@ -265,7 +266,7 @@ function Pop-Migration
'@ -ContainsRowsFor 'CheckpointMigration' -Database $RTDatabaseName
ThenNoErrors
}

It 'should pass when checkpointing a migration' {
GivenMigrationContent -Name 'CheckpointMigration' -Content @'
function Push-Migration
Expand All @@ -289,7 +290,7 @@ function Push-Migration
New-Column -DataType 'text' -Name 'textcolumn' -Description 'a text column'
New-Column -DataType 'ntext' -Name 'ntextcolumn' -NotNull
New-Column -DataType 'image' -Name 'imagecolumn'
New-Column -DataType 'sysname' -Name 'sysnamecolumn' -NotNull
New-Column -DataType 'sysname' -Name 'sysnamecolumn' -NotNull
New-Column -DataType 'sql_variant' -Name 'sql_variantcolumn' -Sparse
New-Column -DataType 'CID' -Name 'CID' -NotNull
varbinary 'VarBinDefault' -Size 1
Expand Down Expand Up @@ -374,7 +375,7 @@ ON [dbo].[Migrations] for insert as select 1
ThenSchema -HasContent @'
Add-Row -SchemaName 'rivet' -TableName 'Migrations' -Column @{
'@ -ContainsRowsFor 'CheckpointMigration' -Database $RTDatabaseName

ThenSchema -HasContent 'Remove-Table -Name ''Migrations''' -Database $RTDatabaseName
ThenSchema -Not -HasContent 'Remove-Schema' -Database $RTDatabaseName
ThenSchema -Not -HasContent 'Remove-PrimaryKey' -Database $RTDatabaseName
Expand All @@ -385,7 +386,7 @@ ON [dbo].[Migrations] for insert as select 1
ThenSchema -Not -HasContent 'Remove-Trigger' -Database $RTDatabaseName
ThenNoErrors
}

It 'should only checkpoint pushed migrations when there are multiple migrations but only one has been pushed' {
GivenMigrationContent -Name 'CheckpointMigration' -Content @'
function Push-Migration
Expand Down Expand Up @@ -477,7 +478,7 @@ function Pop-Migration
'@ -ContainsRowsFor 'CheckpointMigration', 'CheckpointMigration2' -Database $RTDatabaseName
ThenNoErrors
}

It 'should do nothing when no migrations have been pushed' {
# Nothing to push here. Just running push here to initialize database with rivet.migrations table.
Invoke-RTRivet -Push -Database $RTDatabaseName
Expand Down
2 changes: 2 additions & 0 deletions Test/RivetTest/RivetTest.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

using module '..\..\Rivet'

param(
[String]$RivetRoot
)
Expand Down

0 comments on commit c1e8d10

Please sign in to comment.