From 217512fc20befd70b0cadd4c800eb60a93c8d3ec Mon Sep 17 00:00:00 2001 From: Steffen Date: Tue, 7 Mar 2023 13:56:50 +0100 Subject: [PATCH 1/7] added get local instance version filter --- CHANGELOG.md | 4 ++++ docs/Get-SqlTestLocalInstance.md | 19 +++++++++++++++++-- src/Public/Clear-LocalInstance.ps1 | 2 +- src/Public/Get-LocalInstance.ps1 | 12 ++++++++---- src/Public/New-LocalInstance.ps1 | 8 ++------ src/Public/Remove-LocalInstance.ps1 | 2 +- test/Get-LocalInstance.Tests.ps1 | 9 +++++++++ test/New-LocalInstance.Tests.ps1 | 11 +++++++++++ 8 files changed, 53 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ceb21dc..340bb09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Added Version filter parameter for `Get-LocalInstance`. + ## [1.5.0] - 2022-09-30 ### Added diff --git a/docs/Get-SqlTestLocalInstance.md b/docs/Get-SqlTestLocalInstance.md index f25022b..f165717 100644 --- a/docs/Get-SqlTestLocalInstance.md +++ b/docs/Get-SqlTestLocalInstance.md @@ -13,7 +13,7 @@ Returns connection parameters to a available localDb. ## SYNTAX ``` -Get-SqlTestLocalInstance [[-First] ] [] +Get-SqlTestLocalInstance [[-First] ] [[-Version] ] [] ``` ## DESCRIPTION @@ -37,7 +37,7 @@ Version v11.0 ## PARAMETERS ### -First -Specifies maximum number of returned instances. +Specifies maximum number of returned sql server instances. ```yaml Type: Int32 @@ -51,6 +51,21 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -Version +Filters the version of the returned sql server instance. + +```yaml +Type: Version +Parameter Sets: (All) +Aliases: + +Required: False +Position: 2 +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### CommonParameters This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). diff --git a/src/Public/Clear-LocalInstance.ps1 b/src/Public/Clear-LocalInstance.ps1 index 1f611fc..b2b434c 100644 --- a/src/Public/Clear-LocalInstance.ps1 +++ b/src/Public/Clear-LocalInstance.ps1 @@ -18,7 +18,7 @@ function Clear-LocalInstance { [switch] $WhatIf ) - Import-Module PsSqlLocalDb -MinimumVersion 0.3 -ErrorAction Stop + Import-Module PsSqlLocalDb -MinimumVersion 0.4 -ErrorAction Stop Get-LocalInstance -First $null | Where-Object Name -Like test-* | Remove-LocalInstance -WhatIf:$WhatIf } diff --git a/src/Public/Get-LocalInstance.ps1 b/src/Public/Get-LocalInstance.ps1 index e86fab7..4d51e46 100644 --- a/src/Public/Get-LocalInstance.ps1 +++ b/src/Public/Get-LocalInstance.ps1 @@ -23,19 +23,23 @@ function Get-LocalInstance { [CmdletBinding()] param ( - # Specifies maximum number of returned instances. + # Specifies maximum number of returned sql server instances. [Parameter()] - [int] $First = 1 + [int] $First = 1, + + # Filters the version of the returned sql server instance. + [Parameter()] + [System.Version] $Version ) - Import-Module PsSqlLocalDb -MinimumVersion 0.3 -ErrorAction Stop + Import-Module PsSqlLocalDb -MinimumVersion 0.4 -ErrorAction Stop $selectParam = @{} if ( $First ) { $selectParam.First = $First } - Get-LocalDbInstance | Select-Object @selectParam | ForEach-Object { + Get-LocalDbInstance -Version:$Version | Select-Object @selectParam | ForEach-Object { $instance = $PSItem # add metadata diff --git a/src/Public/New-LocalInstance.ps1 b/src/Public/New-LocalInstance.ps1 index f8a64bb..a20b5da 100644 --- a/src/Public/New-LocalInstance.ps1 +++ b/src/Public/New-LocalInstance.ps1 @@ -33,14 +33,10 @@ function New-LocalInstance { [System.Version] $Version ) - Import-Module PsSqlLocalDb -MinimumVersion 0.3 -ErrorAction Stop + Import-Module PsSqlLocalDb -MinimumVersion 0.4 -ErrorAction Stop # create instance - if ( $Version ) { - $instance = New-LocalDbInstance -Name $Name -Version $Version - } else { - $instance = New-LocalDbInstance -Name $Name - } + $instance = New-LocalDbInstance -Name $Name -Version:$Version # add metadata $instance | Add-Member 'DataSource' "(LocalDb)\$( $instance.Name )" diff --git a/src/Public/Remove-LocalInstance.ps1 b/src/Public/Remove-LocalInstance.ps1 index 5ae4d7c..cef0bfe 100644 --- a/src/Public/Remove-LocalInstance.ps1 +++ b/src/Public/Remove-LocalInstance.ps1 @@ -28,7 +28,7 @@ function Remove-LocalInstance { ) begin { - Import-Module PsSqlLocalDb -MinimumVersion 0.3 -ErrorAction Stop + Import-Module PsSqlLocalDb -MinimumVersion 0.4 -ErrorAction Stop } process { diff --git a/test/Get-LocalInstance.Tests.ps1 b/test/Get-LocalInstance.Tests.ps1 index 6328fe5..7230a65 100644 --- a/test/Get-LocalInstance.Tests.ps1 +++ b/test/Get-LocalInstance.Tests.ps1 @@ -17,6 +17,15 @@ Describe Get-LocalInstance -Tag SqlLocalDB { $result.ConnectionString | Should -Not -BeNullOrEmpty } + It 'Returns values by version' { + $result = Get-SqlTestLocalInstance -Version 15.0 + + $result | Should -Not -BeNullOrEmpty + $result.Count | Should -Be 1 + $result.DataSource | Should -Not -BeNullOrEmpty + $result.ConnectionString | Should -Not -BeNullOrEmpty + } + BeforeDiscovery { $PsSqlClient = Import-Module PsSqlClient -PassThru -ErrorAction SilentlyContinue } diff --git a/test/New-LocalInstance.Tests.ps1 b/test/New-LocalInstance.Tests.ps1 index 06b5249..dcbf901 100644 --- a/test/New-LocalInstance.Tests.ps1 +++ b/test/New-LocalInstance.Tests.ps1 @@ -30,6 +30,17 @@ Describe New-LocalInstance -Tag SqlLocalDB { } It 'Provides a SQL server with version' { + $Instance = New-SqlTestLocalInstance -Version $Version + + $Instance | Should -Not -BeNullOrEmpty + $Instance.Version | Should -Not -BeNullOrEmpty + $Instance.Version.Major | Should -Be $Version.Major + $Instance.Version.Minor | Should -Be $Version.Minor + $Instance.ConnectionString | Should -Not -BeNullOrEmpty + $Instance.DataSource | Should -Not -BeNullOrEmpty + } + + It 'Provides a SQL server with major and minor version' { $Instance = New-SqlTestLocalInstance -Version "$( $Version.Major ).$( $Version.Minor )" $Instance | Should -Not -BeNullOrEmpty From d5392c44d8e18d52bf4758259e3b6f6e4d3da11b Mon Sep 17 00:00:00 2001 From: Steffen Date: Tue, 7 Mar 2023 13:59:55 +0100 Subject: [PATCH 2/7] version bump --- CHANGELOG.md | 2 ++ src/PsSqlTestServer.psd1 | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 340bb09..08e7f24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.6.0] - 2023-03-07 + ### Added - Added Version filter parameter for `Get-LocalInstance`. diff --git a/src/PsSqlTestServer.psd1 b/src/PsSqlTestServer.psd1 index bdecb35..39f88dd 100644 --- a/src/PsSqlTestServer.psd1 +++ b/src/PsSqlTestServer.psd1 @@ -12,7 +12,7 @@ RootModule = 'PsSqlTestServer.psm1' # Version number of this module. -ModuleVersion = '1.5.0' +ModuleVersion = '1.6.0' # Supported PSEditions # CompatiblePSEditions = @() From 5c5174e829cd889bed74b057747440966bb320f7 Mon Sep 17 00:00:00 2001 From: Steffen Date: Thu, 23 Mar 2023 15:34:41 +0100 Subject: [PATCH 3/7] added connected switch --- CHANGELOG.md | 1 + docs/New-SqlTestLocalInstance.md | 17 +++++++++++- src/Public/New-LocalInstance.ps1 | 11 +++++++- test/Get-LocalInstance.Tests.ps1 | 6 +---- test/New-LocalInstance.Tests.ps1 | 45 ++++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08e7f24..e4d2eae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added Version filter parameter for `Get-LocalInstance`. +- Parameter to create a SqlClientConnection in `New-LocalInstance`. ## [1.5.0] - 2022-09-30 diff --git a/docs/New-SqlTestLocalInstance.md b/docs/New-SqlTestLocalInstance.md index 0bc624a..28fc211 100644 --- a/docs/New-SqlTestLocalInstance.md +++ b/docs/New-SqlTestLocalInstance.md @@ -13,7 +13,7 @@ Returns connection parameters to new localDb. ## SYNTAX ``` -New-SqlTestLocalInstance [[-Name] ] [[-Version] ] [] +New-SqlTestLocalInstance [[-Name] ] [[-Version] ] [-Connected] [] ``` ## DESCRIPTION @@ -66,6 +66,21 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -Connected +Speficies if a SqlClient connection should be created. + +```yaml +Type: SwitchParameter +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: False +Accept pipeline input: False +Accept wildcard characters: False +``` + ### CommonParameters This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). diff --git a/src/Public/New-LocalInstance.ps1 b/src/Public/New-LocalInstance.ps1 index a20b5da..798196c 100644 --- a/src/Public/New-LocalInstance.ps1 +++ b/src/Public/New-LocalInstance.ps1 @@ -30,7 +30,11 @@ function New-LocalInstance { # Specifies the version of the sql server. [Parameter()] - [System.Version] $Version + [System.Version] $Version, + + # Speficies if a SqlClient connection should be created. + [Parameter()] + [switch] $Connected ) Import-Module PsSqlLocalDb -MinimumVersion 0.4 -ErrorAction Stop @@ -44,6 +48,11 @@ function New-LocalInstance { $instance | Add-Member 'ConnectionString' "Data Source=$( $instance.DataSource );Connect Timeout=$( $instance.ConnectTimeout );Integrated Security=True" $instance | Add-Member 'IsLocalDb' $true + # connect instance if needed + if ( $Connected.IsPresent ) { + $instance | Add-Member 'Connection' ( $instance | Connect-TSqlInstance ) + } + # return $instance | Write-Output } diff --git a/test/Get-LocalInstance.Tests.ps1 b/test/Get-LocalInstance.Tests.ps1 index 7230a65..17401c7 100644 --- a/test/Get-LocalInstance.Tests.ps1 +++ b/test/Get-LocalInstance.Tests.ps1 @@ -26,11 +26,7 @@ Describe Get-LocalInstance -Tag SqlLocalDB { $result.ConnectionString | Should -Not -BeNullOrEmpty } - BeforeDiscovery { - $PsSqlClient = Import-Module PsSqlClient -PassThru -ErrorAction SilentlyContinue - } - - Context PsSqlClient -Skip:( -Not $PsSqlClient ) { + Context PsSqlClient { BeforeAll { $LocalDb = Get-SqlTestLocalInstance diff --git a/test/New-LocalInstance.Tests.ps1 b/test/New-LocalInstance.Tests.ps1 index dcbf901..3b00557 100644 --- a/test/New-LocalInstance.Tests.ps1 +++ b/test/New-LocalInstance.Tests.ps1 @@ -52,6 +52,51 @@ Describe New-LocalInstance -Tag SqlLocalDB { } } + Context PsSqlClient { + + Context Disconnected { + + BeforeAll { + $LocalDb = New-SqlTestLocalInstance + } + + It 'Connects by Pipeline' { + $SqlConnection = $LocalDb | Connect-TSqlInstance + Invoke-TSqlCommand -Connection $SqlConnection -Text 'SELECT 1' + } + + It 'Connects by DataSource' { + $SqlConnection = Connect-TSqlInstance -DataSource $LocalDb.DataSource -ConnectTimeout $LocalDb.ConnectTimeout + Invoke-TSqlCommand -Connection $SqlConnection -Text 'SELECT 1' + } + + It 'Connects by ConnectionString' { + $SqlConnection = Connect-TSqlInstance -ConnectionString $LocalDb.ConnectionString + Invoke-TSqlCommand -Connection $SqlConnection -Text 'SELECT 1' + } + + AfterEach { + if ( $SqlConnection ) { + Disconnect-TSqlInstance -Connection $SqlConnection + } + } + + } + + Context Connected { + + BeforeAll { + $LocalDb = New-SqlTestLocalInstance -Connected + } + + It 'is connected' { + ( Invoke-TSqlCommand -Connection $LocalDb.Connection -Text 'SELECT 1' ).Column1 | Should -Be 1 + } + + } + + } + AfterEach { if ( $Instance ) { $Instance | Remove-SqlTestInstance From 3d55929a2d3ab0c17515349fe97a254b6726147d Mon Sep 17 00:00:00 2001 From: Steffen Date: Thu, 23 Mar 2023 17:24:49 +0100 Subject: [PATCH 4/7] new connected database --- CHANGELOG.md | 1 + docs/New-SqlTestDatabase.md | 23 +++++-- src/Public/New-Database.ps1 | 35 ++++++---- src/Public/New-LocalInstance.ps1 | 10 +-- test/New-Database.Tests.ps1 | 106 ++++++++++++++++++++----------- test/New-LocalInstance.Tests.ps1 | 4 +- 6 files changed, 121 insertions(+), 58 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4d2eae..041e6a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added Version filter parameter for `Get-LocalInstance`. - Parameter to create a SqlClientConnection in `New-LocalInstance`. +- Parameter to create a SqlClientConnection in `New-Database`. ## [1.5.0] - 2022-09-30 diff --git a/docs/New-SqlTestDatabase.md b/docs/New-SqlTestDatabase.md index 7d0cbdc..a69b1fe 100644 --- a/docs/New-SqlTestDatabase.md +++ b/docs/New-SqlTestDatabase.md @@ -14,7 +14,7 @@ Creates a new database and returns the connection parameters. ``` New-SqlTestDatabase [-Instance] [-InstanceConnection] [[-Name] ] - [] + [-Connected] [] ``` ## DESCRIPTION @@ -50,7 +50,7 @@ Aliases: Required: True Position: 1 Default value: None -Accept pipeline input: False +Accept pipeline input: True (ByValue) Accept wildcard characters: False ``` @@ -60,12 +60,12 @@ Specifies a open sql connection to the instance. ```yaml Type: SqlConnection Parameter Sets: (All) -Aliases: +Aliases: Connection Required: True Position: 2 Default value: None -Accept pipeline input: False +Accept pipeline input: True (ByPropertyName) Accept wildcard characters: False ``` @@ -84,6 +84,21 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -Connected +Speficies if a SqlClient connection should be created. + +```yaml +Type: SwitchParameter +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: False +Accept pipeline input: False +Accept wildcard characters: False +``` + ### CommonParameters This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). diff --git a/src/Public/New-Database.ps1 b/src/Public/New-Database.ps1 index aa63ac5..ea2c105 100644 --- a/src/Public/New-Database.ps1 +++ b/src/Public/New-Database.ps1 @@ -26,30 +26,43 @@ function New-Database { [CmdletBinding()] param ( # Specifies the instance, to create the database in. - [Parameter( Mandatory )] + [Parameter( Mandatory, ValueFromPipeline )] [ValidateNotNullOrEmpty()] [PsObject] $Instance, # Specifies a open sql connection to the instance. - [Parameter( Mandatory )] + [Parameter( Mandatory, ValueFromPipelineByPropertyName )] [ValidateNotNullOrEmpty()] + [Alias('Connection')] [Microsoft.Data.SqlClient.SqlConnection] $InstanceConnection, # Specifies the name of the database to create. [Parameter()] [ValidateNotNullOrEmpty()] - [string] $Name = ( New-DatabaseName ) + [string] $Name = ( New-DatabaseName ), + + # Speficies if a SqlClient connection should be created. + [Parameter()] + [switch] $Connected ) - $Database = $Instance.PsObject.Copy() - $Database | Add-Member InstanceName $Database.Name - $Database.Name = $Name + process { + $Database = $Instance.PsObject.Copy() + $Database | Add-Member InstanceName $Database.Name + $Database.Name = $Name + + Invoke-TSqlCommand "CREATE DATABASE [$Name]" -Connection $InstanceConnection - Invoke-TSqlCommand "CREATE DATABASE [$Name]" -Connection $InstanceConnection + $Database | Add-Member InitialCatalog $Name + $Database.ConnectionString += ";Initial Catalog=$( $Database.InitialCatalog )" + $Database | Add-Member InstanceConnection $InstanceConnection - $Database | Add-Member InitialCatalog $Name - $Database.ConnectionString += ";Initial Catalog=$( $Database.InitialCatalog )" - $Database | Add-Member InstanceConnection $InstanceConnection + # connect instance if needed + if ( $Connected.IsPresent ) { + $DatabaseConnection = $Database | Connect-TSqlInstance + $Database.Connection = $DatabaseConnection + } - Write-Output $Database + Write-Output $Database + } } diff --git a/src/Public/New-LocalInstance.ps1 b/src/Public/New-LocalInstance.ps1 index 798196c..d501dcb 100644 --- a/src/Public/New-LocalInstance.ps1 +++ b/src/Public/New-LocalInstance.ps1 @@ -43,14 +43,14 @@ function New-LocalInstance { $instance = New-LocalDbInstance -Name $Name -Version:$Version # add metadata - $instance | Add-Member 'DataSource' "(LocalDb)\$( $instance.Name )" - $instance | Add-Member 'ConnectTimeout' 30 - $instance | Add-Member 'ConnectionString' "Data Source=$( $instance.DataSource );Connect Timeout=$( $instance.ConnectTimeout );Integrated Security=True" - $instance | Add-Member 'IsLocalDb' $true + $instance | Add-Member DataSource "(LocalDb)\$( $instance.Name )" + $instance | Add-Member ConnectTimeout 30 + $instance | Add-Member ConnectionString "Data Source=$( $instance.DataSource );Connect Timeout=$( $instance.ConnectTimeout );Integrated Security=True" + $instance | Add-Member IsLocalDb $true # connect instance if needed if ( $Connected.IsPresent ) { - $instance | Add-Member 'Connection' ( $instance | Connect-TSqlInstance ) + $instance | Add-Member Connection ( $instance | Connect-TSqlInstance ) } # return diff --git a/test/New-Database.Tests.ps1 b/test/New-Database.Tests.ps1 index 946e17f..3e32c43 100644 --- a/test/New-Database.Tests.ps1 +++ b/test/New-Database.Tests.ps1 @@ -17,14 +17,11 @@ Describe New-Database { } } - BeforeDiscovery { - $PsSqlClient = Import-Module PsSqlClient -PassThru -ErrorAction SilentlyContinue - } - - Context PsSqlClient -Skip:( -Not $PsSqlClient ) { + Context PsSqlClient { BeforeAll { $InstanceConnection = $Instance | Connect-TSqlInstance + $Instance | Add-Member Connection $InstanceConnection } AfterAll { @@ -34,48 +31,85 @@ Describe New-Database { } Context Database { - BeforeAll { - $Database = New-SqlTestDatabase -Instance $Instance -InstanceConnection $InstanceConnection - } - AfterAll { - if ( $Database ) { - $Database | Remove-SqlTestDatabase + Context Disconnected { + + BeforeAll { + $Database = New-SqlTestDatabase -Instance $Instance -InstanceConnection $InstanceConnection } - } - It 'Creates a new database' { - $Database | Should -Not -BeNullOrEmpty - $Database.ConnectionString | Should -Not -BeNullOrEmpty - $Database.DataSource | Should -Not -BeNullOrEmpty - $Database.InitialCatalog | Should -Not -BeNullOrEmpty - $Database.InstanceConnection | Should -Not -BeNullOrEmpty - } + AfterAll { + if ( $Database ) { + $Database | Remove-SqlTestDatabase + } + } - It 'Connects by properties' { - $SqlConnection = Connect-TSqlInstance ` - -DataSource $Database.DataSource ` - -InitialCatalog $Database.InitialCatalog ` - -ConnectTimeout $Database.ConnectTimeout + It 'Creates a new database' { + $Database | Should -Not -BeNullOrEmpty + $Database.ConnectionString | Should -Not -BeNullOrEmpty + $Database.DataSource | Should -Not -BeNullOrEmpty + $Database.InitialCatalog | Should -Not -BeNullOrEmpty + $Database.InstanceConnection | Should -Not -BeNullOrEmpty + } - $SqlConnection.Database | Should -Be $Database.InitialCatalog - } + It 'Connects by properties' { + $SqlConnection = Connect-TSqlInstance ` + -DataSource $Database.DataSource ` + -InitialCatalog $Database.InitialCatalog ` + -ConnectTimeout $Database.ConnectTimeout - It 'Connects by connection string' { - $SqlConnection = Connect-TSqlInstance -ConnectionString $Database.ConnectionString + $SqlConnection.Database | Should -Be $Database.InitialCatalog + } - $SqlConnection.Database | Should -Be $Database.InitialCatalog - } + It 'Connects by connection string' { + $SqlConnection = Connect-TSqlInstance -ConnectionString $Database.ConnectionString - It 'Connects by pipeline' { - $SqlConnection = $Database | Connect-TSqlInstance + $SqlConnection.Database | Should -Be $Database.InitialCatalog + } - $SqlConnection.Database | Should -Be $Database.InitialCatalog + It 'Connects by pipeline' { + $SqlConnection = $Database | Connect-TSqlInstance + + $SqlConnection.Database | Should -Be $Database.InitialCatalog + } + + AfterEach { + if ( $SqlConnection ) { + Disconnect-TSqlInstance -Connection $SqlConnection + } + } } - AfterEach { - if ( $SqlConnection ) { - Disconnect-TSqlInstance -Connection $SqlConnection + Context Connected { + + AfterEach { + if ( $Database ) { + $Database | Remove-SqlTestDatabase + } + } + + It 'Creates a new database with connection' { + $Database = New-SqlTestDatabase -Instance $Instance -InstanceConnection $InstanceConnection -Connected + + $Database | Should -Not -BeNullOrEmpty + $Database.ConnectionString | Should -Not -BeNullOrEmpty + $Database.DataSource | Should -Not -BeNullOrEmpty + $Database.InitialCatalog | Should -Not -BeNullOrEmpty + $Database.InstanceConnection | Should -Not -BeNullOrEmpty + $Database.Connection | Should -Not -BeNullOrEmpty + ( Invoke-TSqlCommand -Connection $Database.Connection -Text 'SELECT 1' ).Column1 | Should -Be 1 + } + + It 'Creates a new database with connection in pipeline' { + $Database = $Instance | New-SqlTestDatabase -Connected + + $Database | Should -Not -BeNullOrEmpty + $Database.ConnectionString | Should -Not -BeNullOrEmpty + $Database.DataSource | Should -Not -BeNullOrEmpty + $Database.InitialCatalog | Should -Not -BeNullOrEmpty + $Database.InstanceConnection | Should -Not -BeNullOrEmpty + $Database.Connection | Should -Not -BeNullOrEmpty + ( Invoke-TSqlCommand -Connection $Database.Connection -Text 'SELECT 1' ).Column1 | Should -Be 1 } } } diff --git a/test/New-LocalInstance.Tests.ps1 b/test/New-LocalInstance.Tests.ps1 index 3b00557..0326697 100644 --- a/test/New-LocalInstance.Tests.ps1 +++ b/test/New-LocalInstance.Tests.ps1 @@ -86,11 +86,11 @@ Describe New-LocalInstance -Tag SqlLocalDB { Context Connected { BeforeAll { - $LocalDb = New-SqlTestLocalInstance -Connected + $Instance = New-SqlTestLocalInstance -Connected } It 'is connected' { - ( Invoke-TSqlCommand -Connection $LocalDb.Connection -Text 'SELECT 1' ).Column1 | Should -Be 1 + ( Invoke-TSqlCommand -Connection $Instance.Connection -Text 'SELECT 1' ).Column1 | Should -Be 1 } } From c20f5303ba4e3f51c5cdb3289debb74643c5acef Mon Sep 17 00:00:00 2001 From: Steffen Date: Tue, 25 Apr 2023 09:12:48 +0200 Subject: [PATCH 5/7] test connection --- src/PsSqlTestServer.psd1 | 2 +- src/Public/New-Database.ps1 | 6 +++++- src/Public/New-LocalInstance.ps1 | 20 +++++++++++++------- test/New-Database.Tests.ps1 | 9 +++++++-- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/PsSqlTestServer.psd1 b/src/PsSqlTestServer.psd1 index 39f88dd..e813ccc 100644 --- a/src/PsSqlTestServer.psd1 +++ b/src/PsSqlTestServer.psd1 @@ -51,7 +51,7 @@ Description = 'Cmdlets to provide and manage SQL servers for tests.' # ProcessorArchitecture = '' # Modules that must be imported into the global environment prior to importing this module -RequiredModules = @('PsSqlClient') +RequiredModules = @(@{ ModuleName = 'PsSqlClient'; ModuleVersion = '1.5.0' }) # Assemblies that must be loaded prior to importing this module # RequiredAssemblies = @() diff --git a/src/Public/New-Database.ps1 b/src/Public/New-Database.ps1 index ea2c105..565ccfa 100644 --- a/src/Public/New-Database.ps1 +++ b/src/Public/New-Database.ps1 @@ -60,7 +60,11 @@ function New-Database { # connect instance if needed if ( $Connected.IsPresent ) { $DatabaseConnection = $Database | Connect-TSqlInstance - $Database.Connection = $DatabaseConnection + if ( $DatabaseConnection | Test-TSqlConnection ) { + $Database.Connection = $DatabaseConnection + } else { + Write-Error "Failed to connect database." + } } Write-Output $Database diff --git a/src/Public/New-LocalInstance.ps1 b/src/Public/New-LocalInstance.ps1 index d501dcb..eb480b5 100644 --- a/src/Public/New-LocalInstance.ps1 +++ b/src/Public/New-LocalInstance.ps1 @@ -40,19 +40,25 @@ function New-LocalInstance { Import-Module PsSqlLocalDb -MinimumVersion 0.4 -ErrorAction Stop # create instance - $instance = New-LocalDbInstance -Name $Name -Version:$Version + $Instance = New-LocalDbInstance -Name $Name -Version:$Version # add metadata - $instance | Add-Member DataSource "(LocalDb)\$( $instance.Name )" - $instance | Add-Member ConnectTimeout 30 - $instance | Add-Member ConnectionString "Data Source=$( $instance.DataSource );Connect Timeout=$( $instance.ConnectTimeout );Integrated Security=True" - $instance | Add-Member IsLocalDb $true + $Instance | Add-Member DataSource "(LocalDb)\$( $Instance.Name )" + $Instance | Add-Member ConnectTimeout 30 + $Instance | Add-Member ConnectionString "Data Source=$( $Instance.DataSource );Connect Timeout=$( $Instance.ConnectTimeout );Integrated Security=True" + $Instance | Add-Member IsLocalDb $true # connect instance if needed if ( $Connected.IsPresent ) { - $instance | Add-Member Connection ( $instance | Connect-TSqlInstance ) + $InstanceConnection = $Instance | Connect-TSqlInstance + if ( $InstanceConnection | Test-TSqlConnection ) { + $Instance | Add-Member Connection $InstanceConnection + } else { + Write-Error "Failed to connect instance." + } + } # return - $instance | Write-Output + $Instance | Write-Output } diff --git a/test/New-Database.Tests.ps1 b/test/New-Database.Tests.ps1 index 3e32c43..c087a88 100644 --- a/test/New-Database.Tests.ps1 +++ b/test/New-Database.Tests.ps1 @@ -59,18 +59,21 @@ Describe New-Database { -ConnectTimeout $Database.ConnectTimeout $SqlConnection.Database | Should -Be $Database.InitialCatalog + $SqlConnection | Test-TSqlConnection | Should -Be $true } It 'Connects by connection string' { $SqlConnection = Connect-TSqlInstance -ConnectionString $Database.ConnectionString $SqlConnection.Database | Should -Be $Database.InitialCatalog + $SqlConnection | Test-TSqlConnection | Should -Be $true } It 'Connects by pipeline' { $SqlConnection = $Database | Connect-TSqlInstance $SqlConnection.Database | Should -Be $Database.InitialCatalog + $SqlConnection | Test-TSqlConnection | Should -Be $true } AfterEach { @@ -97,7 +100,8 @@ Describe New-Database { $Database.InitialCatalog | Should -Not -BeNullOrEmpty $Database.InstanceConnection | Should -Not -BeNullOrEmpty $Database.Connection | Should -Not -BeNullOrEmpty - ( Invoke-TSqlCommand -Connection $Database.Connection -Text 'SELECT 1' ).Column1 | Should -Be 1 + + $Database.Connection | Test-TSqlConnection | Should -Be $true } It 'Creates a new database with connection in pipeline' { @@ -109,7 +113,8 @@ Describe New-Database { $Database.InitialCatalog | Should -Not -BeNullOrEmpty $Database.InstanceConnection | Should -Not -BeNullOrEmpty $Database.Connection | Should -Not -BeNullOrEmpty - ( Invoke-TSqlCommand -Connection $Database.Connection -Text 'SELECT 1' ).Column1 | Should -Be 1 + + $Database.Connection | Test-TSqlConnection | Should -Be $true } } } From fa54bee18fc3b265573db04ad3606e12463c7663 Mon Sep 17 00:00:00 2001 From: Steffen Date: Tue, 25 Apr 2023 09:13:17 +0200 Subject: [PATCH 6/7] fixed verion test --- src/Public/Test-LocalDb.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Public/Test-LocalDb.ps1 b/src/Public/Test-LocalDb.ps1 index f362dfb..dff174e 100644 --- a/src/Public/Test-LocalDb.ps1 +++ b/src/Public/Test-LocalDb.ps1 @@ -20,7 +20,7 @@ function Test-LocalDb { [CmdletBinding()] param () - if ( -Not ( Import-Module PsSqlLocalDb -MinimumVersion 0.3 -PassThru -ErrorAction SilentlyContinue )) { + if ( -Not ( Import-Module PsSqlLocalDb -MinimumVersion 0.4 -PassThru -ErrorAction SilentlyContinue )) { Write-Warning 'PsSqlLocalDb is not installed in required version.' return $false } From 462091c1a0c22062d9518ce5378c8cfbf8016955 Mon Sep 17 00:00:00 2001 From: Steffen Date: Thu, 27 Apr 2023 08:10:30 +0200 Subject: [PATCH 7/7] disabled prereleased dependencies --- tasks/Dependencies.Tasks.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tasks/Dependencies.Tasks.ps1 b/tasks/Dependencies.Tasks.ps1 index d76d6f1..d3ae907 100644 --- a/tasks/Dependencies.Tasks.ps1 +++ b/tasks/Dependencies.Tasks.ps1 @@ -1,14 +1,14 @@ task InstallBuildDependencies -Jobs { Install-Module platyPs -Scope CurrentUser -ErrorAction Stop -Verbose - Install-Module PsSqlClient -Scope CurrentUser -ErrorAction Stop -Verbose -AllowPrerelease -AllowClobber + Install-Module PsSqlClient -Scope CurrentUser -ErrorAction Stop -Verbose -AllowPrerelease:$AllowPrerelease -AllowClobber } task InstallTestDependencies -Jobs { - Install-Module psdocker -Scope CurrentUser -ErrorAction Stop -Verbose -AllowPrerelease - Install-Module PsSqlLocalDb -Scope CurrentUser -MinimumVersion 0.3 -ErrorAction Stop -Verbose -AllowPrerelease - Install-Module PsSqlClient -Scope CurrentUser -ErrorAction Stop -Verbose -AllowPrerelease -AllowClobber + Install-Module psdocker -Scope CurrentUser -ErrorAction Stop -Verbose -AllowPrerelease:$AllowPrerelease + Install-Module PsSqlLocalDb -Scope CurrentUser -MinimumVersion 0.3 -ErrorAction Stop -Verbose -AllowPrerelease:$AllowPrerelease + Install-Module PsSqlClient -Scope CurrentUser -ErrorAction Stop -Verbose -AllowPrerelease:$AllowPrerelease -AllowClobber } task InstallReleaseDependencies -Jobs { - Install-Module PsSqlClient -Scope CurrentUser -ErrorAction Stop -Verbose -AllowPrerelease -AllowClobber + Install-Module PsSqlClient -Scope CurrentUser -ErrorAction Stop -Verbose -AllowPrerelease:$AllowPrerelease -AllowClobber }