-
Notifications
You must be signed in to change notification settings - Fork 227
Set-SqlDscDatabaseDefault: Command for managing database defaults
#2189
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
Merged
Merged
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
6000673
Initial plan
Copilot 98100f5
Implement Set-SqlDscDatabaseDefault command with tests and documentation
Copilot 5b4faa9
Fix file endings and replace Should -Not -Throw with direct command c…
Copilot 8dadb42
Merge branch 'main' into copilot/fix-2178
johlju 116623e
Replace New-InvalidOperationException with PSCmdlet.ThrowTerminatingE…
Copilot 7e61feb
Fix integ test
johlju 430a830
Merge branch 'main' into copilot/fix-2178
johlju b6c41f6
fix instructions
johlju 950d028
Fix PSCmdlet.ThrowTerminatingError() to include exception parameter
Copilot bb57a89
Refine AI and DSC community guidelines for clarity and consistency
johlju c25798a
Update error handling guidelines to include try-catch for terminating…
johlju 3c125da
Refactor verbose message variables in Set-SqlDscDatabaseDefault funct…
johlju c4aa5ce
Add guideline to avoid Write-Verbose inside ShouldProcess block for s…
johlju 2c9585d
Enhance Set-SqlDscDatabaseDefault function with detailed ShouldProces…
johlju fc59c28
Consolidate PowerShell style and test guideline instructions in commu…
johlju 0d297cf
Refactor `Set-SqlDscDatabaseDefault` command documentation for clarit…
johlju 20de1f5
Update CHANGELOG.md to clarify the purpose of Set-SqlDscDatabaseDefau…
johlju 75fb8d8
Add INPUTS section to Set-SqlDscDatabaseDefault documentation for pip…
johlju b0dc201
Remove verbose messages for updating default file group, file stream …
johlju 1c5571b
Update database default object strings to use new identifier format
johlju a9117e5
Remove redundant OutputType attribute from Set-SqlDscDatabaseDefault …
johlju dfd2468
Refactor Set-SqlDscDatabaseDefault to improve error handling and stre…
johlju 4106fe0
Add "filegroup" to cSpell words in settings.json
johlju e7ba896
Update error code in Set-SqlDscDatabaseDefault and clean up test cases
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,2 @@ | ||
| # AI Instructions | ||
|
|
||
| This file provides AI agent guidance for the project. Each instruction file below | ||
| targets specific file glob patterns and use cases. | ||
|
|
||
| ## Build & Test Workflow | ||
| - Run in PowerShell, from repository root | ||
| - Build before running tests: `.\build.ps1 -Tasks build` | ||
| - Always run tests in new PowerShell session: `Invoke-Pester -Path @({test paths}) -Output Detailed` | ||
|
|
||
| ## Instructions Overview | ||
|
|
||
| The guidelines always take priority over existing code patterns in project. | ||
|
|
||
| # Requirements | ||
| - SqlServerDsc-specific guidelines override general project guidelines | ||
| - Follow PowerShell style guidelines | ||
| - Maintain localization requirements across all source files | ||
| - Follow test patterns strictly for maintainability | ||
|
|
||
| ## Core Project Guidelines | ||
|
|
||
| - Follow SqlServerDsc project specific guidelines: [./instructions/SqlServerDsc-guidelines.instructions.md](./instructions/SqlServerDsc-guidelines.instructions.md) | ||
| - Always follow PowerShell code style guidelines: [./instructions/dsc-community-style-guidelines-powershell.instructions.md](./instructions/dsc-community-style-guidelines-powershell.instructions.md) | ||
| - Follow Project-level guidelines: [./instructions/dsc-community-style-guidelines.instructions.md](./instructions/dsc-community-style-guidelines.instructions.md) | ||
| - Follow localization requirements: [./instructions/dsc-community-style-guidelines-localization.instructions.md](./instructions/dsc-community-style-guidelines-localization.instructions.md) | ||
| - Always add Unit testing according to: [./instructions/dsc-community-style-guidelines-unit-tests.instructions.md](./instructions/dsc-community-style-guidelines-unit-tests.instructions.md) | ||
| - Always add Integration testing according to: [./instructions/dsc-community-style-guidelines-integration-tests.instructions.md](./instructions/dsc-community-style-guidelines-integration-tests.instructions.md) | ||
| - Follow Markdown formatting requirements: [./instructions/dsc-community-style-guidelines-markdown.instructions.md](./instructions/dsc-community-style-guidelines-markdown.instructions.md) | ||
| - Always update CHANGELOG.md: [./instructions/dsc-community-style-guidelines-changelog.instructions.md](./instructions/dsc-community-style-guidelines-changelog.instructions.md) | ||
|
|
||
| ## Desired State Configuration (DSC) Resource Guidelines | ||
|
|
||
| New DSC resources should always be created as class-based resources. | ||
|
|
||
| - Follow class-based resources guidelines: [./instructions/dsc-community-style-guidelines-class-resource.instructions.md](./instructions/dsc-community-style-guidelines-class-resource.instructions.md) |
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
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,225 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Sets default objects of a database in a SQL Server Database Engine instance. | ||
|
|
||
| .DESCRIPTION | ||
| This command sets default objects of a database in a SQL Server Database Engine instance. | ||
| It can set the default filegroup, default FILESTREAM filegroup, and default Full-Text catalog | ||
| using SMO methods. | ||
|
|
||
| .PARAMETER ServerObject | ||
| Specifies current server connection object. | ||
|
|
||
| .PARAMETER DatabaseObject | ||
| Specifies a database object to modify. | ||
|
|
||
| .PARAMETER Name | ||
| Specifies the name of the database to be modified. | ||
|
|
||
| .PARAMETER DefaultFileGroup | ||
| Sets the default filegroup for the database. The filegroup must exist in the database. | ||
|
|
||
| .PARAMETER DefaultFileStreamFileGroup | ||
| Sets the default FILESTREAM filegroup for the database. The filegroup must exist in the database. | ||
|
|
||
| .PARAMETER DefaultFullTextCatalog | ||
| Sets the default Full-Text catalog for the database. The catalog must exist in the database. | ||
|
|
||
| .PARAMETER Force | ||
| Specifies that the database defaults should be modified without any confirmation. | ||
|
|
||
| .PARAMETER Refresh | ||
| Specifies that the **ServerObject**'s databases should be refreshed before | ||
| modifying 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, or pass in **DatabaseObject**. | ||
|
|
||
| .PARAMETER PassThru | ||
| Specifies that the database object should be returned after modification. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| $databaseObject = $serverObject | Get-SqlDscDatabase -Name 'MyDatabase' | ||
| $databaseObject | Set-SqlDscDatabaseDefault -DefaultFileGroup 'MyFileGroup' | ||
|
|
||
| Sets the default filegroup of the database named **MyDatabase** to **MyFileGroup**. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| $serverObject | Set-SqlDscDatabaseDefault -Name 'MyDatabase' -DefaultFullTextCatalog 'MyCatalog' -Force | ||
|
|
||
| Sets the default Full-Text catalog of the database named **MyDatabase** to **MyCatalog** without prompting for confirmation. | ||
|
|
||
| .EXAMPLE | ||
| $serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance' | ||
| $databaseObject = $serverObject | Get-SqlDscDatabase -Name 'MyDatabase' | ||
| $databaseObject | Set-SqlDscDatabaseDefault -DefaultFileGroup 'DataFileGroup' -DefaultFileStreamFileGroup 'FileStreamFileGroup' -DefaultFullTextCatalog 'FTCatalog' | ||
|
|
||
| Sets multiple default objects for the database named **MyDatabase**. | ||
|
|
||
| .INPUTS | ||
| Microsoft.SqlServer.Management.Smo.Server | ||
| Server object accepted from the pipeline (ServerObject parameter set). | ||
|
|
||
| Microsoft.SqlServer.Management.Smo.Database | ||
| Database object accepted from the pipeline (DatabaseObject parameter set). | ||
|
|
||
| .OUTPUTS | ||
| None. But when **PassThru** is specified the output is `[Microsoft.SqlServer.Management.Smo.Database]`. | ||
| #> | ||
| function Set-SqlDscDatabaseDefault | ||
| { | ||
| [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.')] | ||
| [OutputType()] | ||
| [OutputType([Microsoft.SqlServer.Management.Smo.Database])] | ||
| [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] | ||
| param | ||
| ( | ||
| [Parameter(ParameterSetName = 'ServerObject', Mandatory = $true, ValueFromPipeline = $true)] | ||
| [Microsoft.SqlServer.Management.Smo.Server] | ||
| $ServerObject, | ||
|
|
||
| [Parameter(ParameterSetName = 'DatabaseObject', Mandatory = $true, ValueFromPipeline = $true)] | ||
| [Microsoft.SqlServer.Management.Smo.Database] | ||
| $DatabaseObject, | ||
|
|
||
| [Parameter(ParameterSetName = 'ServerObject', Mandatory = $true)] | ||
| [ValidateNotNullOrEmpty()] | ||
| [System.String] | ||
| $Name, | ||
|
|
||
| [Parameter()] | ||
| [ValidateNotNullOrEmpty()] | ||
| [System.String] | ||
| $DefaultFileGroup, | ||
|
|
||
| [Parameter()] | ||
| [ValidateNotNullOrEmpty()] | ||
| [System.String] | ||
| $DefaultFileStreamFileGroup, | ||
|
|
||
| [Parameter()] | ||
| [ValidateNotNullOrEmpty()] | ||
| [System.String] | ||
| $DefaultFullTextCatalog, | ||
|
|
||
| [Parameter()] | ||
| [System.Management.Automation.SwitchParameter] | ||
| $Force, | ||
|
|
||
| [Parameter(ParameterSetName = 'ServerObject')] | ||
| [System.Management.Automation.SwitchParameter] | ||
| $Refresh, | ||
|
|
||
| [Parameter()] | ||
| [System.Management.Automation.SwitchParameter] | ||
| $PassThru | ||
| ) | ||
|
|
||
| process | ||
| { | ||
| if ($PSCmdlet.ParameterSetName -eq 'ServerObject') | ||
| { | ||
| if ($Refresh.IsPresent) | ||
| { | ||
| # Refresh the server object's databases collection | ||
| $ServerObject.Databases.Refresh() | ||
| } | ||
|
|
||
| Write-Verbose -Message ($script:localizedData.DatabaseDefault_Set -f $Name, $ServerObject.InstanceName) | ||
|
|
||
| # Get the database object | ||
| $DatabaseObject = $ServerObject.Databases[$Name] | ||
|
|
||
| if (-not $DatabaseObject) | ||
| { | ||
| $errorMessage = $script:localizedData.Database_NotFound -f $Name | ||
| New-InvalidOperationException -Message $errorMessage | ||
| } | ||
| } | ||
| else | ||
| { | ||
| $Name = $DatabaseObject.Name | ||
| $ServerObject = $DatabaseObject.Parent | ||
| Write-Verbose -Message ($script:localizedData.DatabaseDefault_Set -f $Name, $ServerObject.InstanceName) | ||
| } | ||
|
|
||
| if ($Force.IsPresent -and -not $Confirm) | ||
| { | ||
| $ConfirmPreference = 'None' | ||
| } | ||
|
|
||
| try | ||
| { | ||
| $wasUpdate = $false | ||
|
|
||
| if ($PSBoundParameters.ContainsKey('DefaultFileGroup')) | ||
| { | ||
| $descriptionMessage = $script:localizedData.DatabaseDefault_SetFileGroup_ShouldProcessVerboseDescription -f $Name, $DefaultFileGroup, $ServerObject.InstanceName | ||
| $confirmationMessage = $script:localizedData.DatabaseDefault_SetFileGroup_ShouldProcessVerboseWarning -f $Name, $DefaultFileGroup | ||
| $captionMessage = $script:localizedData.DatabaseDefault_SetFileGroup_ShouldProcessCaption | ||
|
|
||
| if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage)) | ||
| { | ||
| Write-Verbose -Message ($script:localizedData.DatabaseDefault_UpdatingDefaultFileGroup -f $DefaultFileGroup) | ||
| $DatabaseObject.SetDefaultFileGroup($DefaultFileGroup) | ||
| $wasUpdate = $true | ||
| } | ||
| } | ||
|
|
||
| if ($PSBoundParameters.ContainsKey('DefaultFileStreamFileGroup')) | ||
| { | ||
| $descriptionMessage = $script:localizedData.DatabaseDefault_SetFileStreamFileGroup_ShouldProcessVerboseDescription -f $Name, $DefaultFileStreamFileGroup, $ServerObject.InstanceName | ||
| $confirmationMessage = $script:localizedData.DatabaseDefault_SetFileStreamFileGroup_ShouldProcessVerboseWarning -f $Name, $DefaultFileStreamFileGroup | ||
| $captionMessage = $script:localizedData.DatabaseDefault_SetFileStreamFileGroup_ShouldProcessCaption | ||
|
|
||
| if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage)) | ||
| { | ||
| Write-Verbose -Message ($script:localizedData.DatabaseDefault_UpdatingDefaultFileStreamFileGroup -f $DefaultFileStreamFileGroup) | ||
| $DatabaseObject.SetDefaultFileStreamFileGroup($DefaultFileStreamFileGroup) | ||
| $wasUpdate = $true | ||
| } | ||
| } | ||
|
|
||
| if ($PSBoundParameters.ContainsKey('DefaultFullTextCatalog')) | ||
| { | ||
| $descriptionMessage = $script:localizedData.DatabaseDefault_SetFullTextCatalog_ShouldProcessVerboseDescription -f $Name, $DefaultFullTextCatalog, $ServerObject.InstanceName | ||
| $confirmationMessage = $script:localizedData.DatabaseDefault_SetFullTextCatalog_ShouldProcessVerboseWarning -f $Name, $DefaultFullTextCatalog | ||
| $captionMessage = $script:localizedData.DatabaseDefault_SetFullTextCatalog_ShouldProcessCaption | ||
|
|
||
| if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage)) | ||
| { | ||
| Write-Verbose -Message ($script:localizedData.DatabaseDefault_UpdatingDefaultFullTextCatalog -f $DefaultFullTextCatalog) | ||
| $DatabaseObject.SetDefaultFullTextCatalog($DefaultFullTextCatalog) | ||
| $wasUpdate = $true | ||
| } | ||
| } | ||
|
|
||
| if ($wasUpdate) | ||
| { | ||
| Write-Verbose -Message ($script:localizedData.DatabaseDefault_Updated -f $Name) | ||
| } | ||
|
|
||
| if ($PassThru.IsPresent) | ||
| { | ||
| return $DatabaseObject | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| $errorMessage = $script:localizedData.DatabaseDefault_SetFailed -f $Name, $ServerObject.InstanceName | ||
|
|
||
| $exception = [System.InvalidOperationException]::new($errorMessage, $_.Exception) | ||
|
|
||
| $PSCmdlet.ThrowTerminatingError( | ||
| [System.Management.Automation.ErrorRecord]::new( | ||
| $exception, | ||
| 'SSDDD0001', # cSpell: disable-line | ||
| [System.Management.Automation.ErrorCategory]::InvalidOperation, | ||
| $Name | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
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.