-
Notifications
You must be signed in to change notification settings - Fork 228
Set-SqlDscDatabaseDefaultFileGroup: New command proposal
#2354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
johlju
wants to merge
8
commits into
dsccommunity:main
Choose a base branch
from
johlju:fix/issue-#2328
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b2a68d8
`Set-SqlDscDatabaseDefaultFileGroup`: New command proposal
johlju 0735b68
Refactor Set-SqlDscDatabaseDefaultFileGroup to improve verbose messag…
johlju 96666a1
Fix parameter name in Invoke-SqlDscQuery calls for consistency in dat…
johlju 25a764a
Update error handling in integration tests to use -Force with Invoke-…
johlju 0ecd07b
Enhance documentation and tests for Set-SqlDscDatabaseDefaultFileGroup
johlju 5d2ff3d
Merge branch 'main' into fix/issue-#2328
johlju e400597
Dynamically set file paths for database file creation in integration …
johlju 2084e75
Refactor Set-SqlDscDatabaseDefaultFileGroup to simplify refresh logic…
johlju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Sets the default filegroup for a database in a SQL Server Database Engine instance. | ||
|
|
||
| .DESCRIPTION | ||
| This command sets the default filegroup or default FILESTREAM filegroup for a | ||
| database in a SQL Server Database Engine instance. | ||
|
|
||
| The filegroup must exist in the database. The command uses the SetDefaultFileGroup() | ||
| or SetDefaultFileStreamFileGroup() methods on the SMO Database object to change | ||
| the default filegroup. | ||
|
|
||
| .PARAMETER ServerObject | ||
| Specifies current server connection object. | ||
|
|
||
| .PARAMETER Name | ||
| Specifies the name of the database to modify. | ||
|
|
||
| .PARAMETER DatabaseObject | ||
| Specifies the database object to modify (from Get-SqlDscDatabase). | ||
|
|
||
| .PARAMETER Refresh | ||
| Specifies that the **ServerObject**'s databases should be refreshed before | ||
| trying to get the database object. This is helpful when databases could have been | ||
| modified outside of the **ServerObject**, for example through T-SQL. But | ||
| on instances with a large amount of databases it might be better to make | ||
| sure the **ServerObject** is recent enough. | ||
|
|
||
| This parameter is only used when setting the default filegroup using **ServerObject** | ||
| and **Name** parameters. | ||
|
|
||
| .PARAMETER DefaultFileGroup | ||
| Specifies the name of the filegroup that should be set as the default filegroup | ||
| for the database. This is mutually exclusive with **DefaultFileStreamFileGroup**. | ||
|
|
||
| .PARAMETER DefaultFileStreamFileGroup | ||
| Specifies the name of the filegroup that should be set as the default FILESTREAM | ||
| filegroup for the database. This is mutually exclusive with **DefaultFileGroup**. | ||
|
|
||
| .PARAMETER Force | ||
| Specifies that the default filegroup should be modified without any confirmation. | ||
|
|
||
| .PARAMETER PassThru | ||
| Specifies that the database object should be returned after modification. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| Set-SqlDscDatabaseDefaultFileGroup -ServerObject $serverObject -Name 'MyDatabase' -DefaultFileGroup 'UserData' | ||
|
|
||
| Sets the default filegroup of the database named **MyDatabase** to **UserData**. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| Set-SqlDscDatabaseDefaultFileGroup -ServerObject $serverObject -Name 'MyDatabase' -DefaultFileStreamFileGroup 'FileStreamData' | ||
|
|
||
| Sets the default FILESTREAM filegroup of the database named **MyDatabase** to **FileStreamData**. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| $databaseObject = $serverObject | Get-SqlDscDatabase -Name 'MyDatabase' | ||
| Set-SqlDscDatabaseDefaultFileGroup -DatabaseObject $databaseObject -DefaultFileGroup 'UserData' -Force | ||
|
|
||
| Sets the default filegroup of the database using a database object without prompting for confirmation. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| Set-SqlDscDatabaseDefaultFileGroup -ServerObject $serverObject -Name 'MyDatabase' -DefaultFileGroup 'UserData' -PassThru | ||
|
|
||
| Sets the default filegroup and returns the updated database object. | ||
|
|
||
| .INPUTS | ||
| Microsoft.SqlServer.Management.Smo.Database | ||
|
|
||
| The database object to modify (from Get-SqlDscDatabase). | ||
|
|
||
| .OUTPUTS | ||
| None. | ||
|
|
||
| When PassThru is specified the output is [Microsoft.SqlServer.Management.Smo.Database]. | ||
| #> | ||
| function Set-SqlDscDatabaseDefaultFileGroup | ||
| { | ||
| [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.')] | ||
| [OutputType()] | ||
| [OutputType([Microsoft.SqlServer.Management.Smo.Database])] | ||
| [CmdletBinding(DefaultParameterSetName = 'ServerObjectSet', SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] | ||
| param | ||
| ( | ||
| [Parameter(ParameterSetName = 'ServerObjectSet', Mandatory = $true)] | ||
| [Parameter(ParameterSetName = 'ServerObjectSetFileStream', Mandatory = $true)] | ||
| [Microsoft.SqlServer.Management.Smo.Server] | ||
| $ServerObject, | ||
|
|
||
| [Parameter(ParameterSetName = 'ServerObjectSet', Mandatory = $true)] | ||
| [Parameter(ParameterSetName = 'ServerObjectSetFileStream', Mandatory = $true)] | ||
| [ValidateNotNullOrEmpty()] | ||
| [System.String] | ||
| $Name, | ||
|
|
||
| [Parameter(ParameterSetName = 'ServerObjectSet')] | ||
| [Parameter(ParameterSetName = 'ServerObjectSetFileStream')] | ||
| [System.Management.Automation.SwitchParameter] | ||
| $Refresh, | ||
|
|
||
| [Parameter(ParameterSetName = 'DatabaseObjectSet', Mandatory = $true, ValueFromPipeline = $true)] | ||
| [Parameter(ParameterSetName = 'DatabaseObjectSetFileStream', Mandatory = $true, ValueFromPipeline = $true)] | ||
| [Microsoft.SqlServer.Management.Smo.Database] | ||
| $DatabaseObject, | ||
|
|
||
| [Parameter(ParameterSetName = 'ServerObjectSet', Mandatory = $true)] | ||
| [Parameter(ParameterSetName = 'DatabaseObjectSet', Mandatory = $true)] | ||
| [ValidateNotNullOrEmpty()] | ||
| [System.String] | ||
| $DefaultFileGroup, | ||
|
|
||
| [Parameter(ParameterSetName = 'ServerObjectSetFileStream', Mandatory = $true)] | ||
| [Parameter(ParameterSetName = 'DatabaseObjectSetFileStream', Mandatory = $true)] | ||
| [ValidateNotNullOrEmpty()] | ||
| [System.String] | ||
| $DefaultFileStreamFileGroup, | ||
|
|
||
| [Parameter()] | ||
| [System.Management.Automation.SwitchParameter] | ||
| $Force, | ||
|
|
||
| [Parameter()] | ||
| [System.Management.Automation.SwitchParameter] | ||
| $PassThru | ||
| ) | ||
|
|
||
| begin | ||
| { | ||
| if ($Force.IsPresent -and -not $Confirm) | ||
| { | ||
| $ConfirmPreference = 'None' | ||
| } | ||
| } | ||
|
|
||
| process | ||
| { | ||
| # Get the database object based on the parameter set | ||
| switch -Wildcard ($PSCmdlet.ParameterSetName) | ||
| { | ||
| 'ServerObjectSet*' | ||
| { | ||
| $previousErrorActionPreference = $ErrorActionPreference | ||
| $ErrorActionPreference = 'Stop' | ||
|
|
||
| $sqlDatabaseObject = $ServerObject | | ||
| Get-SqlDscDatabase -Name $Name -Refresh:$Refresh -ErrorAction 'Stop' | ||
|
|
||
| $ErrorActionPreference = $previousErrorActionPreference | ||
| } | ||
|
|
||
| 'DatabaseObjectSet*' | ||
| { | ||
| $sqlDatabaseObject = $DatabaseObject | ||
| } | ||
| } | ||
|
|
||
| # Determine which filegroup type to set | ||
| if ($PSBoundParameters.ContainsKey('DefaultFileGroup')) | ||
| { | ||
| $fileGroupType = 'DefaultFileGroup' | ||
| $fileGroupName = $DefaultFileGroup | ||
| $currentFileGroup = $sqlDatabaseObject.DefaultFileGroup | ||
| } | ||
| else | ||
| { | ||
| $fileGroupType = 'DefaultFileStreamFileGroup' | ||
| $fileGroupName = $DefaultFileStreamFileGroup | ||
| $currentFileGroup = $sqlDatabaseObject.DefaultFileStreamFileGroup | ||
| } | ||
|
|
||
| $verboseDescriptionMessage = $script:localizedData."DatabaseDefaultFileGroup_Set_ShouldProcessVerboseDescription_$fileGroupType" -f $sqlDatabaseObject.Name, $fileGroupName, $sqlDatabaseObject.Parent.InstanceName | ||
| $verboseWarningMessage = $script:localizedData."DatabaseDefaultFileGroup_Set_ShouldProcessVerboseWarning_$fileGroupType" -f $sqlDatabaseObject.Name, $fileGroupName | ||
| $captionMessage = $script:localizedData.DatabaseDefaultFileGroup_Set_ShouldProcessCaption | ||
|
|
||
| if ($PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage)) | ||
| { | ||
| # Check if the default filegroup is already correct (idempotence) | ||
| if ($currentFileGroup -eq $fileGroupName) | ||
| { | ||
| Write-Debug -Message ($script:localizedData."DatabaseDefaultFileGroup_AlreadyCorrect_$fileGroupType" -f $sqlDatabaseObject.Name, $fileGroupName) | ||
| } | ||
| else | ||
| { | ||
| Write-Debug -Message ($script:localizedData."DatabaseDefaultFileGroup_Updating_$fileGroupType" -f $sqlDatabaseObject.Name, $fileGroupName) | ||
|
|
||
| try | ||
| { | ||
| if ($fileGroupType -eq 'DefaultFileGroup') | ||
| { | ||
| $sqlDatabaseObject.SetDefaultFileGroup($fileGroupName) | ||
| } | ||
| else | ||
| { | ||
| $sqlDatabaseObject.SetDefaultFileStreamFileGroup($fileGroupName) | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| $errorMessage = $script:localizedData."DatabaseDefaultFileGroup_SetFailed_$fileGroupType" -f $sqlDatabaseObject.Name, $fileGroupName | ||
|
|
||
| $PSCmdlet.ThrowTerminatingError( | ||
| [System.Management.Automation.ErrorRecord]::new( | ||
| [System.InvalidOperationException]::new($errorMessage, $_.Exception), | ||
| 'SSDDFG0004', # cspell: disable-line | ||
| [System.Management.Automation.ErrorCategory]::InvalidOperation, | ||
| $sqlDatabaseObject | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| Write-Debug -Message ($script:localizedData."DatabaseDefaultFileGroup_Updated_$fileGroupType" -f $sqlDatabaseObject.Name, $fileGroupName) | ||
| } | ||
|
|
||
| <# | ||
| Refresh the database object to get the updated default filegroup property if: | ||
| - PassThru is specified (user wants the updated object back) | ||
| - Using DatabaseObject parameter set (user's object reference should be updated) | ||
|
|
||
| Refresh even if no change was made to ensure the object is up to date. | ||
| #> | ||
| if ($PassThru.IsPresent -or $PSCmdlet.ParameterSetName -match 'DatabaseObjectSet') | ||
| { | ||
| $sqlDatabaseObject.Refresh() | ||
| } | ||
|
|
||
| if ($PassThru.IsPresent) | ||
| { | ||
| return $sqlDatabaseObject | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.