Skip to content

Commit

Permalink
Merge pull request #71 from webmd-health-services/bugfix/no-db-connec…
Browse files Browse the repository at this point in the history
…tion-when-plugins-run-when-get-migration-called

No database connection when plugins run from Get-Migration
  • Loading branch information
splatteredbits authored Jul 6, 2023
2 parents ca41d9e + 8bd8705 commit f2b6d50
Show file tree
Hide file tree
Showing 72 changed files with 2,010 additions and 2,769 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@

# Rivet Changelog

## 0.22.0

> Released 6 Jul 2023
### Added

* Verbose messages when connecting to SQL Server and switching databases that shows the current SQL Server and database
name.

### Changed

* Verbose messages that show queries now show query duration in seconds.
* Improvements to Rivet internals.

## Fixed

* `Get-Migration` fails when plugins use Rivet's database connection.
* Rivet fails if a plugin returns non-operation objects to the pipeline.

### Removed

* Function `Connect-RivetSession`. Improved internal Rivet connection management so this function is no longer
necessary.


## 0.21.1

> Released 24 May 2023
Expand Down
39 changes: 23 additions & 16 deletions Rivet/Functions/Connect-Database.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,36 @@ function Connect-Database
Disconnect-Database -Session $Session

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

$connection.Open()
Write-Verbose -Message "[$($connection.DataSource)].[$($connection.Database)]"
}

if ($connection.Database -ne 'master')
try
{
$connection.ChangeDatabase('master')
}
# We're already connected to the database.
if ($connection.Database -eq $Name)
{
return
}

$query = "select 1 from sys.databases where name='${Name}'"
$dbExists = Invoke-Query -Session $Session -Query $query -AsScalar
if (-not $dbExists)
{
Write-Debug -Message ('Creating database {0}.{1}.' -f $SqlServerName,$Name)
$query = "create database [${Name}]"
Invoke-Query -Session $Session -Query $query -NonQuery | Out-Null
}

$query = "select 1 from sys.databases where name='${Name}'"
$dbExists = Invoke-Query -Session $Session -Query $query -AsScalar
if (-not $dbExists)
$connection.ChangeDatabase($Name)
Write-Verbose -Message "[$($connection.DataSource)].[$($connection.Database)]"
$Session.CurrentDatabase = $Session.Databases | Where-Object 'Name' -EQ $Name
}
finally
{
Write-Debug -Message ('Creating database {0}.{1}.' -f $SqlServerName,$Name)
$query = "create database [${Name}]"
Invoke-Query -Session $Session -Query $query -NonQuery
Write-Debug -Message ('{0,8} (ms) Connect-Database' -f ([int]((Get-Date) - $startedAt).TotalMilliseconds))
}

$connection.ChangeDatabase($Name)
$Session.CurrentDatabase = $Session.Databases | Where-Object 'Name' -EQ $Name

Write-Debug -Message ('{0,8} (ms) Connect-Database' -f ([int]((Get-Date) - $startedAt).TotalMilliseconds))
return $true
}
33 changes: 0 additions & 33 deletions Rivet/Functions/Connect-RivetSession.ps1

This file was deleted.

44 changes: 26 additions & 18 deletions Rivet/Functions/Convert-FileInfoToMigration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,13 @@ function Convert-FileInfoToMigration
Use-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState

Write-Timing -Message 'Convert-FileInfoToMigration BEGIN' -Indent
function Clear-Migration
{
('function:Push-Migration','function:Pop-Migration') |
Where-Object { Test-Path -Path $_ } |
Remove-Item -WhatIf:$false -Confirm:$false
}

Clear-Migration
}

process
{
function Add-Operation
{
param(
[Parameter(Mandatory)]
[Rivet.Migration] $Migration,

# The migration object to invoke.
[Parameter(Mandatory, ValueFromPipeline)]
[Object] $Operation,
Expand All @@ -62,7 +54,7 @@ function Convert-FileInfoToMigration
# Set CommandTimeout on operation to value from Rivet configuration.
$operationItem.CommandTimeout = $Session.CommandTimeout

$pluginParameter = @{ Migration = $m ; Operation = $_ }
$pluginParameter = @{ Migration = $Migration ; Operation = $_ }

[Rivet.Operations.Operation[]]$operations = & {
Invoke-RivetPlugin -Session $Session `
Expand All @@ -81,13 +73,29 @@ function Convert-FileInfoToMigration
}
}

function Clear-Migration
{
('function:Push-Migration','function:Pop-Migration') |
Where-Object { Test-Path -Path $_ } |
Remove-Item -WhatIf:$false -Confirm:$false
}

Clear-Migration

Import-RivetPlugin -Path $Session.PluginPaths -ModuleName $Session.PluginModules
}

process
{
foreach( $fileInfo in $InputObject )
{
$dbName = Split-Path -Parent -Path $fileInfo.FullName
$dbName = Split-Path -Parent -Path $dbName
$dbName = Split-Path -Leaf -Path $dbName
$dbName = $fileInfo.DatabaseName

Connect-Database -Session $Session -Name $dbName

$m = [Rivet.Migration]::New($fileInfo.MigrationID, $fileInfo.MigrationName, $fileInfo.FullName, $dbName)
$m | Add-Member -Name 'IsRivetMigration' -MemberType NoteProperty -Value $fileInfo.IsRivetMigration

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

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

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

Pop-Migration | Add-Operation -OperationsList $m.PopOperations
Pop-Migration | Add-Operation -Migration $m -OperationsList $m.PopOperations
if( $m.PopOperations.Count -eq 0 )
{
return
Expand Down
2 changes: 1 addition & 1 deletion Rivet/Functions/Export-Migration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,7 @@ where
}
$event = $null

Connect-Database -Session $Session -Name $Database -ErrorAction Stop | Out-Null
Connect-Database -Session $Session -Name $Database -ErrorAction Stop
try
{
#region QUERIES
Expand Down
50 changes: 25 additions & 25 deletions Rivet/Functions/Get-Migration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function Get-Migration
[CmdletBinding(DefaultParameterSetName='External')]
[OutputType([Rivet.Migration])]
param(
# The database whose migrations to get.np
# The database names whose migrations to get. The default is to get migrations from all databases.
[Parameter(ParameterSetName='External')]
[String[]] $Database,

Expand Down Expand Up @@ -102,44 +102,43 @@ function Get-Migration
}

Clear-Migration

Write-Timing -Message 'Get-Migration Clear-Migration'

$getRivetConfigParams = @{ }
if( $Database )
$session = New-RivetSession -ConfigurationPath $ConfigFilePath -Environment $Environment -Database $Database
if( -not $session )
{
$getRivetConfigParams['Database'] = $Database
return
}

if( $ConfigFilePath )
if ($null -eq $Database)
{
$getRivetConfigParams['Path'] = $ConfigFilePath
$Database = @()
}

if( $Environment )
$getMigrationFileParams = @{}
if ($PSBoundParameters.ContainsKey('Include'))
{
$getRivetConfigParams['Environment'] = $Environment
$getMigrationFileParams['Include'] = $Include
}

$session = New-RivetSession -ConfigurationPath $ConfigFilePath -Environment $Environment -Database $Database
if( -not $session )
if ($PSBoundParameters.ContainsKey('Exclude'))
{
return
$getMigrationFileParams['Exclude'] = $Exclude
}

Import-RivetPlugin -Path $session.PluginPaths -ModuleName $session.PluginModules
Write-Timing -Message 'Get-Migration Import-RivetPlugin'
$session.Databases |
Where-Object {
if (-not $Database.Length)
{
return $true
}

$getMigrationFileParams = @{}
@( 'Include', 'Exclude' ) | ForEach-Object {
if( $PSBoundParameters.ContainsKey($_) )
{
$getMigrationFileParams[$_] = $PSBoundParameters[$_]
}
}

Get-MigrationFile -Session $session @getMigrationFileParams |
return $_.Name -in $Database
} |
Get-MigrationFile @getMigrationFileParams |
Where-Object {
if( $PSBoundParameters.ContainsKey( 'Before' ) )
if ($PSBoundParameters.ContainsKey('Before'))
{
$beforeTimestamp = [uint64]$Before.ToString('yyyyMMddHHmmss')
if( $_.MigrationID -gt $beforeTimestamp )
Expand All @@ -148,7 +147,7 @@ function Get-Migration
}
}

if( $PSBoundParameters.ContainsKey( 'After' ) )
if ($PSBoundParameters.ContainsKey('After'))
{
$afterTimestamp = [uint64]$After.ToString('yyyyMMddHHmmss')
if( $_.MigrationID -lt $afterTimestamp )
Expand All @@ -158,7 +157,8 @@ function Get-Migration
}
return $true
} |
Convert-FileInfoToMigration -Session $Session
Convert-FileInfoToMigration -Session $session |
Write-Output

Write-Timing -Message 'Get-Migration END' -Outdent
}
Loading

0 comments on commit f2b6d50

Please sign in to comment.