Skip to content

Commit d6111a3

Browse files
authored
Test-SqlDscIsDatabase: Check database existence (#2325)
1 parent 4423284 commit d6111a3

13 files changed

+450
-477
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8+
### Removed
9+
10+
- BREAKING CHANGE: Removed public command `Test-SqlDscDatabase`. Use
11+
`Test-SqlDscIsDatabase` to check existence. For property checks, use
12+
`Test-SqlDscDatabaseProperty`. See [issue #2201](https://github.com/dsccommunity/SqlServerDsc/issues/2201).
13+
814
### Added
915

16+
- Added public command `Test-SqlDscIsDatabase` to test if a database exists on a
17+
SQL Server Database Engine instance ([issue #2201](https://github.com/dsccommunity/SqlServerDsc/issues/2201)).
1018
- Added public command `Get-SqlDscSetupLog` to retrieve SQL Server setup bootstrap
1119
logs (Summary.txt) from the most recent setup operation. This command can be used
1220
interactively for troubleshooting or within integration tests to help diagnose

azure-pipelines.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ stages:
336336
'tests/Integration/Commands/ConvertFrom-SqlDscDatabasePermission.Integration.Tests.ps1'
337337
'tests/Integration/Commands/New-SqlDscDatabase.Integration.Tests.ps1'
338338
'tests/Integration/Commands/Set-SqlDscDatabase.Integration.Tests.ps1'
339-
'tests/Integration/Commands/Test-SqlDscDatabase.Integration.Tests.ps1'
339+
'tests/Integration/Commands/Test-SqlDscIsDatabase.Integration.Tests.ps1'
340340
'tests/Integration/Commands/Test-SqlDscDatabaseProperty.Integration.Tests.ps1'
341341
'tests/Integration/Commands/Get-SqlDscDatabasePermission.Integration.Tests.ps1'
342342
'tests/Integration/Commands/Set-SqlDscDatabasePermission.Integration.Tests.ps1'

source/Public/Test-SqlDscDatabase.ps1

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

source/Public/Test-SqlDscDatabaseProperty.ps1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
.PARAMETER DatabaseObject
2727
Specifies the database object to test properties for (from Get-SqlDscDatabase).
2828
29+
.PARAMETER Refresh
30+
Specifies that the **ServerObject**'s databases should be refreshed before
31+
trying to get the database object. This is helpful when databases could have been
32+
modified outside of the **ServerObject**, for example through T-SQL. But
33+
on instances with a large amount of databases it might be better to make
34+
sure the **ServerObject** is recent enough.
35+
36+
This parameter is only used when testing properties using **ServerObject** and
37+
**Name** parameters.
38+
2939
.PARAMETER Collation
3040
Specifies the default collation for the database.
3141
@@ -505,6 +515,10 @@ function Test-SqlDscDatabaseProperty
505515
[System.String]
506516
$Name,
507517

518+
[Parameter(ParameterSetName = 'ServerObjectSet')]
519+
[System.Management.Automation.SwitchParameter]
520+
$Refresh,
521+
508522
[Parameter(ParameterSetName = 'DatabaseObjectSet', Mandatory = $true, ValueFromPipeline = $true)]
509523
[Microsoft.SqlServer.Management.Smo.Database]
510524
$DatabaseObject,
@@ -1109,7 +1123,7 @@ function Test-SqlDscDatabaseProperty
11091123

11101124
$previousErrorActionPreference = $ErrorActionPreference
11111125
$ErrorActionPreference = 'Stop'
1112-
$sqlDatabaseObject = $ServerObject | Get-SqlDscDatabase -Name $Name -ErrorAction 'Stop'
1126+
$sqlDatabaseObject = $ServerObject | Get-SqlDscDatabase -Name $Name -Refresh:$Refresh -ErrorAction 'Stop'
11131127
$ErrorActionPreference = $previousErrorActionPreference
11141128
}
11151129

@@ -1127,7 +1141,7 @@ function Test-SqlDscDatabaseProperty
11271141
$boundParameters = Remove-CommonParameter -Hashtable $PSBoundParameters
11281142

11291143
# Remove function-specific parameters
1130-
foreach ($parameterToRemove in @('ServerObject', 'Name', 'DatabaseObject'))
1144+
foreach ($parameterToRemove in @('ServerObject', 'Name', 'DatabaseObject', 'Refresh'))
11311145
{
11321146
$boundParameters.Remove($parameterToRemove)
11331147
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<#
2+
.SYNOPSIS
3+
Tests if a database exists on a SQL Server Database Engine instance.
4+
5+
.DESCRIPTION
6+
This command tests if a database exists on a SQL Server Database Engine instance.
7+
8+
.PARAMETER ServerObject
9+
Specifies current server connection object.
10+
11+
.PARAMETER Name
12+
Specifies the name of the database to test for existence.
13+
14+
.PARAMETER Refresh
15+
Specifies that the **ServerObject**'s databases should be refreshed before
16+
testing the database existence. This is helpful when databases could have been
17+
modified outside of the **ServerObject**, for example through T-SQL. But
18+
on instances with a large amount of databases it might be better to make
19+
sure the **ServerObject** is recent enough.
20+
21+
.EXAMPLE
22+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
23+
$serverObject | Test-SqlDscIsDatabase -Name 'MyDatabase'
24+
25+
Tests if the database named **MyDatabase** exists on the instance.
26+
27+
.EXAMPLE
28+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
29+
Test-SqlDscIsDatabase -ServerObject $serverObject -Name 'MyDatabase' -Refresh
30+
31+
Tests if the database named **MyDatabase** exists on the instance, refreshing
32+
the server object's databases collection first.
33+
34+
.OUTPUTS
35+
`[System.Boolean]`
36+
37+
Returns `$true` if the target object is a database; otherwise, `$false`.
38+
39+
.INPUTS
40+
`[Microsoft.SqlServer.Management.Smo.Server]`
41+
42+
The server object can be provided via the pipeline to **ServerObject**.
43+
#>
44+
function Test-SqlDscIsDatabase
45+
{
46+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')]
47+
[OutputType([System.Boolean])]
48+
[CmdletBinding()]
49+
param
50+
(
51+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
52+
[Microsoft.SqlServer.Management.Smo.Server]
53+
$ServerObject,
54+
55+
[Parameter(Mandatory = $true)]
56+
[ValidateNotNullOrEmpty()]
57+
[System.String]
58+
$Name,
59+
60+
[Parameter()]
61+
[System.Management.Automation.SwitchParameter]
62+
$Refresh
63+
)
64+
65+
process
66+
{
67+
Write-Verbose -Message ($script:localizedData.IsDatabase_Test -f $Name, $ServerObject.InstanceName)
68+
69+
# Check if database exists using Get-SqlDscDatabase
70+
$sqlDatabaseObject = Get-SqlDscDatabase -ServerObject $ServerObject -Name $Name -Refresh:$Refresh -ErrorAction 'SilentlyContinue'
71+
72+
if ($sqlDatabaseObject)
73+
{
74+
return $true
75+
}
76+
else
77+
{
78+
return $false
79+
}
80+
}
81+
}

source/en-US/SqlServerDsc.strings.psd1

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,8 @@ ConvertFrom-StringData @'
387387
Database_Remove_ShouldProcessCaption = Remove database from instance
388388
Remove_SqlDscDatabase_NotFound = Database '{0}' was not found.
389389
390-
## Test-SqlDscDatabase
391-
Database_Test = Testing the state of database '{0}' on instance '{1}'.
392-
Database_InDesiredStatePresent = Database '{0}' is present and in desired state.
393-
Database_InDesiredStateAbsent = Database '{0}' is absent as expected.
394-
Database_NotInDesiredStatePresent = Expected the database '{0}' to be present, but it was absent.
395-
Database_NotInDesiredStateAbsent = Expected the database '{0}' to be absent, but it was present.
396-
Database_CollationWrong = The database '{0}' exists and has the collation '{1}', but expected it to have the collation '{2}'.
397-
Database_CompatibilityLevelWrong = The database '{0}' exists and has the compatibility level '{1}', but expected it to have the compatibility level '{2}'.
398-
Database_RecoveryModelWrong = The database '{0}' exists and has the recovery model '{1}', but expected it to have the recovery model '{2}'.
399-
Database_OwnerNameWrong = The database '{0}' exists and has the owner '{1}', but expected it to have the owner '{2}'.
390+
## Test-SqlDscIsDatabase
391+
IsDatabase_Test = Testing if database '{0}' exists on instance '{1}'. (TSID0001)
400392
401393
## Test-SqlDscDatabaseProperty
402394
DatabaseProperty_TestingProperties = Testing properties of database '{0}' on instance '{1}'. (TSDDP0001)

tests/Integration/Commands/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ Get-SqlDscDatabase | 4 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTES
9292
ConvertFrom-SqlDscDatabasePermission | 4 | 0 (Prerequisites) | - | -
9393
New-SqlDscDatabase | 4 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | SqlDscIntegrationTestDatabase_Persistent database
9494
Set-SqlDscDatabase | 4 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
95-
Test-SqlDscDatabase | 4 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
95+
Test-SqlDscIsDatabase | 4 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
96+
Test-SqlDscDatabaseProperty | 4 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
9697
Get-SqlDscDatabasePermission | 4 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Test database, Test user
9798
ConvertTo-SqlDscDatabasePermission | 4 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
9899
Set-SqlDscDatabasePermission | 4 | 4 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -

0 commit comments

Comments
 (0)