Skip to content

Commit 79c0cc0

Browse files
authored
Suspend-SqlDscDatabase/Resume-SqlDscDatabase: Command proposals (#2355)
1 parent 9bf5f46 commit 79c0cc0

10 files changed

+1382
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Added comprehensive set of settable database properties that were previously
1212
only available in `Set-SqlDscDatabaseProperty`
1313
([issue #2190](https://github.com/dsccommunity/SqlServerDsc/issues/2190)).
14+
- Added public command `Resume-SqlDscDatabase` to bring a database online using
15+
SMO `Database.SetOnline()`. Supports Server and Database pipeline input
16+
([issue #2191](https://github.com/dsccommunity/SqlServerDsc/issues/2191)).
17+
- Added public command `Suspend-SqlDscDatabase` to take a database offline using
18+
SMO `Database.SetOffline()`. Supports Server and Database pipeline input;
19+
includes `Force` to disconnect active users
20+
([issue #2192](https://github.com/dsccommunity/SqlServerDsc/issues/2192)).
1421

1522
### Changed
1623

azure-pipelines.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ stages:
341341
'tests/Integration/Commands/ConvertFrom-SqlDscDatabasePermission.Integration.Tests.ps1'
342342
'tests/Integration/Commands/New-SqlDscDatabase.Integration.Tests.ps1'
343343
'tests/Integration/Commands/New-SqlDscDatabaseSnapshot.Integration.Tests.ps1'
344+
'tests/Integration/Commands/Resume-SqlDscDatabase.Integration.Tests.ps1'
345+
'tests/Integration/Commands/Suspend-SqlDscDatabase.Integration.Tests.ps1'
344346
'tests/Integration/Commands/Get-SqlDscCompatibilityLevel.Integration.Tests.ps1'
345347
'tests/Integration/Commands/Set-SqlDscDatabaseProperty.Integration.Tests.ps1'
346348
'tests/Integration/Commands/Set-SqlDscDatabaseOwner.Integration.Tests.ps1'
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
<#
2+
.SYNOPSIS
3+
Brings a SQL Server database back online.
4+
5+
.DESCRIPTION
6+
This command brings a SQL Server database back online, making it available
7+
to users again after maintenance or downtime. The command uses the SMO
8+
Database.SetOnline() method to resume the database.
9+
10+
.PARAMETER ServerObject
11+
Specifies current server connection object.
12+
13+
.PARAMETER Name
14+
Specifies the name of the database to bring online.
15+
16+
.PARAMETER DatabaseObject
17+
Specifies the database object to bring online (from Get-SqlDscDatabase).
18+
19+
.PARAMETER Refresh
20+
Specifies that the **ServerObject**'s databases should be refreshed before
21+
trying to get the database object. This is helpful when databases could have been
22+
modified outside of the **ServerObject**, for example through T-SQL. But
23+
on instances with a large amount of databases it might be better to make
24+
sure the **ServerObject** is recent enough.
25+
26+
This parameter is only used when resuming a database using **ServerObject** and
27+
**Name** parameters.
28+
29+
.PARAMETER Force
30+
Specifies that the database should be brought online without any confirmation.
31+
32+
.PARAMETER PassThru
33+
Specifies that the database object should be returned after the operation.
34+
35+
.EXAMPLE
36+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
37+
Resume-SqlDscDatabase -ServerObject $serverObject -Name 'MyDatabase'
38+
39+
Brings the database named **MyDatabase** back online.
40+
41+
.EXAMPLE
42+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
43+
$databaseObject = $serverObject | Get-SqlDscDatabase -Name 'MyDatabase'
44+
Resume-SqlDscDatabase -DatabaseObject $databaseObject -Force
45+
46+
Brings the database online using a database object without prompting for confirmation.
47+
48+
.EXAMPLE
49+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
50+
Resume-SqlDscDatabase -ServerObject $serverObject -Name 'MyDatabase' -PassThru
51+
52+
Brings the database online and returns the updated database object.
53+
54+
.EXAMPLE
55+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
56+
$serverObject | Get-SqlDscDatabase -Name 'MyDatabase' | Resume-SqlDscDatabase -Force
57+
58+
Brings the database online using pipeline input without prompting for confirmation.
59+
60+
.INPUTS
61+
Microsoft.SqlServer.Management.Smo.Server
62+
63+
The server object from Connect-SqlDscDatabaseEngine.
64+
65+
.INPUTS
66+
Microsoft.SqlServer.Management.Smo.Database
67+
68+
The database object to bring online (from Get-SqlDscDatabase).
69+
70+
.OUTPUTS
71+
None.
72+
73+
By default, no output is returned.
74+
75+
.OUTPUTS
76+
Microsoft.SqlServer.Management.Smo.Database
77+
78+
When PassThru is specified, the updated database object is returned.
79+
#>
80+
function Resume-SqlDscDatabase
81+
{
82+
[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.')]
83+
[OutputType()]
84+
[OutputType([Microsoft.SqlServer.Management.Smo.Database])]
85+
[CmdletBinding(DefaultParameterSetName = 'ServerObjectSet', SupportsShouldProcess = $true, ConfirmImpact = 'High')]
86+
param
87+
(
88+
[Parameter(ParameterSetName = 'ServerObjectSet', Mandatory = $true, ValueFromPipeline = $true)]
89+
[Microsoft.SqlServer.Management.Smo.Server]
90+
$ServerObject,
91+
92+
[Parameter(ParameterSetName = 'ServerObjectSet', Mandatory = $true)]
93+
[ValidateNotNullOrEmpty()]
94+
[System.String]
95+
$Name,
96+
97+
[Parameter(ParameterSetName = 'ServerObjectSet')]
98+
[System.Management.Automation.SwitchParameter]
99+
$Refresh,
100+
101+
[Parameter(ParameterSetName = 'DatabaseObjectSet', Mandatory = $true, ValueFromPipeline = $true)]
102+
[Microsoft.SqlServer.Management.Smo.Database]
103+
$DatabaseObject,
104+
105+
[Parameter()]
106+
[System.Management.Automation.SwitchParameter]
107+
$Force,
108+
109+
[Parameter()]
110+
[System.Management.Automation.SwitchParameter]
111+
$PassThru
112+
)
113+
114+
begin
115+
{
116+
if ($Force.IsPresent -and -not $Confirm)
117+
{
118+
$ConfirmPreference = 'None'
119+
}
120+
}
121+
122+
process
123+
{
124+
# Get the database object based on the parameter set
125+
switch ($PSCmdlet.ParameterSetName)
126+
{
127+
'ServerObjectSet'
128+
{
129+
$previousErrorActionPreference = $ErrorActionPreference
130+
$ErrorActionPreference = 'Stop'
131+
132+
$sqlDatabaseObject = $ServerObject |
133+
Get-SqlDscDatabase -Name $Name -Refresh:$Refresh -ErrorAction 'Stop'
134+
135+
$ErrorActionPreference = $previousErrorActionPreference
136+
}
137+
138+
'DatabaseObjectSet'
139+
{
140+
$sqlDatabaseObject = $DatabaseObject
141+
}
142+
}
143+
144+
$descriptionMessage = $script:localizedData.Database_Resume_ShouldProcessVerboseDescription -f $sqlDatabaseObject.Name, $sqlDatabaseObject.Parent.InstanceName
145+
$confirmationMessage = $script:localizedData.Database_Resume_ShouldProcessVerboseWarning -f $sqlDatabaseObject.Name
146+
$captionMessage = $script:localizedData.Database_Resume_ShouldProcessCaption
147+
148+
if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage))
149+
{
150+
<#
151+
Refresh the database object to get the current status if using DatabaseObject
152+
and if Refresh was specified. If ServerObject and Name parameters are used, the
153+
database object is already fresh as Refresh was passed to Get-SqlDscDatabase.
154+
#>
155+
if ($PSCmdlet.ParameterSetName -eq 'DatabaseObjectSet' -and $Refresh.IsPresent)
156+
{
157+
$sqlDatabaseObject.Refresh()
158+
}
159+
160+
# Check if database has a status other than offline (idempotence)
161+
if (-not $sqlDatabaseObject.Status.HasFlag([Microsoft.SqlServer.Management.Smo.DatabaseStatus]::Offline))
162+
{
163+
Write-Debug -Message ($script:localizedData.Database_AlreadyOnline -f $sqlDatabaseObject.Name, ($sqlDatabaseObject.Status -join ', '))
164+
}
165+
else
166+
{
167+
Write-Debug -Message ($script:localizedData.Database_BringingOnline -f $sqlDatabaseObject.Name)
168+
169+
try
170+
{
171+
$sqlDatabaseObject.SetOnline()
172+
}
173+
catch
174+
{
175+
$errorMessage = $script:localizedData.Database_ResumeFailed -f $sqlDatabaseObject.Name
176+
177+
$PSCmdlet.ThrowTerminatingError(
178+
[System.Management.Automation.ErrorRecord]::new(
179+
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
180+
'RSDD0001', # cspell: disable-line
181+
[System.Management.Automation.ErrorCategory]::InvalidOperation,
182+
$sqlDatabaseObject
183+
)
184+
)
185+
}
186+
187+
Write-Debug -Message ($script:localizedData.Database_BroughtOnline -f $sqlDatabaseObject.Name)
188+
}
189+
190+
if ($PassThru.IsPresent)
191+
{
192+
# Refresh the database object to get the updated Status property
193+
$sqlDatabaseObject.Refresh()
194+
195+
$sqlDatabaseObject
196+
}
197+
}
198+
}
199+
}

0 commit comments

Comments
 (0)