Skip to content

Commit f2b6d50

Browse files
Merge pull request #71 from webmd-health-services/bugfix/no-db-connection-when-plugins-run-when-get-migration-called
No database connection when plugins run from Get-Migration
2 parents ca41d9e + 8bd8705 commit f2b6d50

File tree

72 files changed

+2010
-2769
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+2010
-2769
lines changed

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@
33

44
# Rivet Changelog
55

6+
## 0.22.0
7+
8+
> Released 6 Jul 2023
9+
10+
### Added
11+
12+
* Verbose messages when connecting to SQL Server and switching databases that shows the current SQL Server and database
13+
name.
14+
15+
### Changed
16+
17+
* Verbose messages that show queries now show query duration in seconds.
18+
* Improvements to Rivet internals.
19+
20+
## Fixed
21+
22+
* `Get-Migration` fails when plugins use Rivet's database connection.
23+
* Rivet fails if a plugin returns non-operation objects to the pipeline.
24+
25+
### Removed
26+
27+
* Function `Connect-RivetSession`. Improved internal Rivet connection management so this function is no longer
28+
necessary.
29+
30+
631
## 0.21.1
732

833
> Released 24 May 2023

Rivet/Functions/Connect-Database.ps1

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,36 @@ function Connect-Database
4545
Disconnect-Database -Session $Session
4646

4747
$connString =
48-
"Server=${sqlServerName};Database=master;Integrated Security=True;Connection Timeout=${connTimeout}"
48+
"Server=${sqlServerName};Integrated Security=True;Connection Timeout=${connTimeout}"
4949
$Session.Connection = $connection = [Data.SqlClient.SqlConnection]::New($connString)
5050

5151
$connection.Open()
52+
Write-Verbose -Message "[$($connection.DataSource)].[$($connection.Database)]"
5253
}
5354

54-
if ($connection.Database -ne 'master')
55+
try
5556
{
56-
$connection.ChangeDatabase('master')
57-
}
57+
# We're already connected to the database.
58+
if ($connection.Database -eq $Name)
59+
{
60+
return
61+
}
62+
63+
$query = "select 1 from sys.databases where name='${Name}'"
64+
$dbExists = Invoke-Query -Session $Session -Query $query -AsScalar
65+
if (-not $dbExists)
66+
{
67+
Write-Debug -Message ('Creating database {0}.{1}.' -f $SqlServerName,$Name)
68+
$query = "create database [${Name}]"
69+
Invoke-Query -Session $Session -Query $query -NonQuery | Out-Null
70+
}
5871

59-
$query = "select 1 from sys.databases where name='${Name}'"
60-
$dbExists = Invoke-Query -Session $Session -Query $query -AsScalar
61-
if (-not $dbExists)
72+
$connection.ChangeDatabase($Name)
73+
Write-Verbose -Message "[$($connection.DataSource)].[$($connection.Database)]"
74+
$Session.CurrentDatabase = $Session.Databases | Where-Object 'Name' -EQ $Name
75+
}
76+
finally
6277
{
63-
Write-Debug -Message ('Creating database {0}.{1}.' -f $SqlServerName,$Name)
64-
$query = "create database [${Name}]"
65-
Invoke-Query -Session $Session -Query $query -NonQuery
78+
Write-Debug -Message ('{0,8} (ms) Connect-Database' -f ([int]((Get-Date) - $startedAt).TotalMilliseconds))
6679
}
67-
68-
$connection.ChangeDatabase($Name)
69-
$Session.CurrentDatabase = $Session.Databases | Where-Object 'Name' -EQ $Name
70-
71-
Write-Debug -Message ('{0,8} (ms) Connect-Database' -f ([int]((Get-Date) - $startedAt).TotalMilliseconds))
72-
return $true
7380
}

Rivet/Functions/Connect-RivetSession.ps1

Lines changed: 0 additions & 33 deletions
This file was deleted.

Rivet/Functions/Convert-FileInfoToMigration.ps1

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,13 @@ function Convert-FileInfoToMigration
2323
Use-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
2424

2525
Write-Timing -Message 'Convert-FileInfoToMigration BEGIN' -Indent
26-
function Clear-Migration
27-
{
28-
('function:Push-Migration','function:Pop-Migration') |
29-
Where-Object { Test-Path -Path $_ } |
30-
Remove-Item -WhatIf:$false -Confirm:$false
31-
}
32-
33-
Clear-Migration
34-
}
3526

36-
process
37-
{
3827
function Add-Operation
3928
{
4029
param(
30+
[Parameter(Mandatory)]
31+
[Rivet.Migration] $Migration,
32+
4133
# The migration object to invoke.
4234
[Parameter(Mandatory, ValueFromPipeline)]
4335
[Object] $Operation,
@@ -62,7 +54,7 @@ function Convert-FileInfoToMigration
6254
# Set CommandTimeout on operation to value from Rivet configuration.
6355
$operationItem.CommandTimeout = $Session.CommandTimeout
6456

65-
$pluginParameter = @{ Migration = $m ; Operation = $_ }
57+
$pluginParameter = @{ Migration = $Migration ; Operation = $_ }
6658

6759
[Rivet.Operations.Operation[]]$operations = & {
6860
Invoke-RivetPlugin -Session $Session `
@@ -81,13 +73,29 @@ function Convert-FileInfoToMigration
8173
}
8274
}
8375

76+
function Clear-Migration
77+
{
78+
('function:Push-Migration','function:Pop-Migration') |
79+
Where-Object { Test-Path -Path $_ } |
80+
Remove-Item -WhatIf:$false -Confirm:$false
81+
}
82+
83+
Clear-Migration
84+
85+
Import-RivetPlugin -Path $Session.PluginPaths -ModuleName $Session.PluginModules
86+
}
87+
88+
process
89+
{
8490
foreach( $fileInfo in $InputObject )
8591
{
86-
$dbName = Split-Path -Parent -Path $fileInfo.FullName
87-
$dbName = Split-Path -Parent -Path $dbName
88-
$dbName = Split-Path -Leaf -Path $dbName
92+
$dbName = $fileInfo.DatabaseName
93+
94+
Connect-Database -Session $Session -Name $dbName
95+
96+
$m = [Rivet.Migration]::New($fileInfo.MigrationID, $fileInfo.MigrationName, $fileInfo.FullName, $dbName)
97+
$m | Add-Member -Name 'IsRivetMigration' -MemberType NoteProperty -Value $fileInfo.IsRivetMigration
8998

90-
$m = New-Object 'Rivet.Migration' $fileInfo.MigrationID, $fileInfo.MigrationName, $fileInfo.FullName, $dbName
9199
Write-Timing -Message ('Convert-FileInfoToMigration {0}' -f $m.FullName)
92100

93101
# Do not remove. It's a variable expected in some migrations.
@@ -111,7 +119,7 @@ Push-Migration function not found. All migrations are required to have a Push-Mi
111119
'@)
112120
}
113121

114-
Push-Migration | Add-Operation -OperationsList $m.PushOperations
122+
Push-Migration | Add-Operation -Migration $m -OperationsList $m.PushOperations
115123
if( $m.PushOperations.Count -eq 0 )
116124
{
117125
return
@@ -130,7 +138,7 @@ Pop-Migration function not found. All migrations are required to have a Pop-Migr
130138
return
131139
}
132140

133-
Pop-Migration | Add-Operation -OperationsList $m.PopOperations
141+
Pop-Migration | Add-Operation -Migration $m -OperationsList $m.PopOperations
134142
if( $m.PopOperations.Count -eq 0 )
135143
{
136144
return

Rivet/Functions/Export-Migration.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,7 @@ where
17781778
}
17791779
$event = $null
17801780

1781-
Connect-Database -Session $Session -Name $Database -ErrorAction Stop | Out-Null
1781+
Connect-Database -Session $Session -Name $Database -ErrorAction Stop
17821782
try
17831783
{
17841784
#region QUERIES

Rivet/Functions/Get-Migration.ps1

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function Get-Migration
6262
[CmdletBinding(DefaultParameterSetName='External')]
6363
[OutputType([Rivet.Migration])]
6464
param(
65-
# The database whose migrations to get.np
65+
# The database names whose migrations to get. The default is to get migrations from all databases.
6666
[Parameter(ParameterSetName='External')]
6767
[String[]] $Database,
6868

@@ -102,44 +102,43 @@ function Get-Migration
102102
}
103103

104104
Clear-Migration
105+
105106
Write-Timing -Message 'Get-Migration Clear-Migration'
106107

107-
$getRivetConfigParams = @{ }
108-
if( $Database )
108+
$session = New-RivetSession -ConfigurationPath $ConfigFilePath -Environment $Environment -Database $Database
109+
if( -not $session )
109110
{
110-
$getRivetConfigParams['Database'] = $Database
111+
return
111112
}
112113

113-
if( $ConfigFilePath )
114+
if ($null -eq $Database)
114115
{
115-
$getRivetConfigParams['Path'] = $ConfigFilePath
116+
$Database = @()
116117
}
117118

118-
if( $Environment )
119+
$getMigrationFileParams = @{}
120+
if ($PSBoundParameters.ContainsKey('Include'))
119121
{
120-
$getRivetConfigParams['Environment'] = $Environment
122+
$getMigrationFileParams['Include'] = $Include
121123
}
122124

123-
$session = New-RivetSession -ConfigurationPath $ConfigFilePath -Environment $Environment -Database $Database
124-
if( -not $session )
125+
if ($PSBoundParameters.ContainsKey('Exclude'))
125126
{
126-
return
127+
$getMigrationFileParams['Exclude'] = $Exclude
127128
}
128129

129-
Import-RivetPlugin -Path $session.PluginPaths -ModuleName $session.PluginModules
130-
Write-Timing -Message 'Get-Migration Import-RivetPlugin'
130+
$session.Databases |
131+
Where-Object {
132+
if (-not $Database.Length)
133+
{
134+
return $true
135+
}
131136

132-
$getMigrationFileParams = @{}
133-
@( 'Include', 'Exclude' ) | ForEach-Object {
134-
if( $PSBoundParameters.ContainsKey($_) )
135-
{
136-
$getMigrationFileParams[$_] = $PSBoundParameters[$_]
137-
}
138-
}
139-
140-
Get-MigrationFile -Session $session @getMigrationFileParams |
137+
return $_.Name -in $Database
138+
} |
139+
Get-MigrationFile @getMigrationFileParams |
141140
Where-Object {
142-
if( $PSBoundParameters.ContainsKey( 'Before' ) )
141+
if ($PSBoundParameters.ContainsKey('Before'))
143142
{
144143
$beforeTimestamp = [uint64]$Before.ToString('yyyyMMddHHmmss')
145144
if( $_.MigrationID -gt $beforeTimestamp )
@@ -148,7 +147,7 @@ function Get-Migration
148147
}
149148
}
150149

151-
if( $PSBoundParameters.ContainsKey( 'After' ) )
150+
if ($PSBoundParameters.ContainsKey('After'))
152151
{
153152
$afterTimestamp = [uint64]$After.ToString('yyyyMMddHHmmss')
154153
if( $_.MigrationID -lt $afterTimestamp )
@@ -158,7 +157,8 @@ function Get-Migration
158157
}
159158
return $true
160159
} |
161-
Convert-FileInfoToMigration -Session $Session
160+
Convert-FileInfoToMigration -Session $session |
161+
Write-Output
162162

163163
Write-Timing -Message 'Get-Migration END' -Outdent
164164
}

0 commit comments

Comments
 (0)