Skip to content

Commit f49e80a

Browse files
authored
New-SqlDscDatabaseSnapshot: Command for SMO-based snapshot creation (#2343)
1 parent 0688805 commit f49e80a

34 files changed

+4846
-8
lines changed

.github/instructions/dsc-community-style-guidelines-pester.instructions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ applyTo: "**/*.[Tt]ests.ps1"
2828
- Mock variables prefix: 'mock'
2929

3030
## Structure & Scope
31-
- Public commands: Never use `InModuleScope` (unless retrieving localized strings)
31+
- Public commands: Never use `InModuleScope` (unless retrieving localized strings or creating an object using an internal class)
3232
- Private functions/class resources: Always use `InModuleScope`
3333
- Each class method = separate `Context` block
3434
- Each scenario = separate `Context` block
@@ -46,6 +46,7 @@ applyTo: "**/*.[Tt]ests.ps1"
4646
- Set `$PSDefaultParameterValues` for `Mock:ModuleName`, `Should:ModuleName`, `InModuleScope:ModuleName`
4747
- Omit `-ModuleName` parameter on Pester commands
4848
- Never use `Mock` inside `InModuleScope`-block
49+
- Never use `param()` inside `-MockWith` scriptblocks, parameters are auto-bound
4950

5051
## File Organization
5152
- Class resources: `tests/Unit/Classes/{Name}.Tests.ps1`

.github/instructions/dsc-community-style-guidelines.instructions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ applyTo: "**"
1919
## File Organization
2020
- Public commands: `source/Public/{CommandName}.ps1`
2121
- Private functions: `source/Private/{FunctionName}.ps1`
22+
- Classes: `source/Classes/{DependencyGroupNumber}.{ClassName}.ps1`
23+
- Enums: `source/Enum/{DependencyGroupNumber}.{EnumName}.ps1`
2224
- Unit tests: `tests/Unit/{Classes|Public|Private}/{Name}.Tests.ps1`
2325
- Integration tests: `tests/Integration/Commands/{CommandName}.Integration.Tests.ps1`
2426

.vscode/settings.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,26 @@
173173
"path": "bash",
174174
"args": []
175175
}
176+
},
177+
"chat.tools.terminal.terminalProfile.osx": {
178+
"path": "pwsh",
179+
"args": [],
180+
"env": {
181+
"COPILOT": "1"
182+
}
183+
},
184+
"chat.tools.terminal.terminalProfile.linux": {
185+
"path": "pwsh",
186+
"args": [],
187+
"env": {
188+
"COPILOT": "1"
189+
}
190+
},
191+
"chat.tools.terminal.terminalProfile.windows": {
192+
"path": "pwsh.exe",
193+
"args": [],
194+
"env": {
195+
"COPILOT": "1"
196+
}
176197
}
177198
}

CHANGELOG.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,39 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525

2626
### Added
2727

28+
- Added public command `New-SqlDscDatabaseSnapshot` to create database snapshots
29+
in a SQL Server Database Engine instance using SMO. This command provides an
30+
automated and DSC-friendly approach to snapshot management by leveraging
31+
`New-SqlDscDatabase` for the actual creation. The command now supports `FileGroup`
32+
and `DataFile` parameters to allow control over snapshot file placement and
33+
structure ([issue #2341](https://github.com/dsccommunity/SqlServerDsc/issues/2341)).
34+
- Added public command `New-SqlDscFileGroup` to create FileGroup objects for SQL
35+
Server databases. This command simplifies creating FileGroup objects that can be
36+
used with `New-SqlDscDatabase` and other database-related commands. The `Database`
37+
parameter is optional, allowing FileGroup objects to be created standalone and
38+
added to a Database later using `Add-SqlDscFileGroup`.
39+
- Added public command `New-SqlDscDataFile` to create DataFile objects for SQL
40+
Server FileGroups. This command simplifies creating DataFile objects with
41+
specified physical file paths, supporting both regular database files (.mdf, .ndf)
42+
and sparse files for database snapshots (.ss). The `FileGroup` parameter is
43+
mandatory, requiring DataFile objects to be created with an associated FileGroup.
44+
- Added public command `Add-SqlDscFileGroup` to add one or more FileGroup objects
45+
to a Database. This command provides a clean way to associate FileGroup objects
46+
with a Database after they have been created.
47+
- Added public command `ConvertTo-SqlDscDataFile` to convert `DatabaseFileSpec`
48+
objects to SMO DataFile objects.
49+
- Added public command `ConvertTo-SqlDscFileGroup` to convert `DatabaseFileGroupSpec`
50+
objects to SMO FileGroup objects.
51+
- Added class `DatabaseFileSpec` to define data file specifications without requiring
52+
a database or SMO context.
53+
- Added class `DatabaseFileGroupSpec` to define file group specifications with
54+
associated data files without requiring a database or SMO context.
55+
- `New-SqlDscDatabase`
56+
- Added `FileGroup` and `DataFile` parameters to allow specifying custom file
57+
locations and structure. These parameters apply to both regular databases and
58+
database snapshots, enabling control over file placement for snapshots (sparse
59+
files) and custom filegroup/datafile configuration for regular databases
60+
([issue #2341](https://github.com/dsccommunity/SqlServerDsc/issues/2341)).
2861
- Added public command `Set-SqlDscDatabaseOwner` to change the owner of a SQL Server
2962
database [issue #2177](https://github.com/dsccommunity/SqlServerDsc/issues/2177).
3063
This command uses the SMO `SetOwner()` method and supports both `ServerObject`

azure-pipelines.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ stages:
293293
# Group 2
294294
'tests/Integration/Commands/PostInstallationConfiguration.Integration.Tests.ps1'
295295
# Group 3
296+
'tests/Integration/Commands/New-SqlDscFileGroup.Integration.Tests.ps1'
297+
'tests/Integration/Commands/New-SqlDscDataFile.Integration.Tests.ps1'
298+
'tests/Integration/Commands/Add-SqlDscFileGroup.Integration.Tests.ps1'
299+
'tests/Integration/Commands/ConvertTo-SqlDscFileGroup.Integration.Tests.ps1'
300+
'tests/Integration/Commands/ConvertTo-SqlDscDataFile.Integration.Tests.ps1'
296301
'tests/Integration/Commands/Get-SqlDscSetupLog.Integration.Tests.ps1'
297302
'tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1'
298303
'tests/Integration/Commands/Disconnect-SqlDscDatabaseEngine.Integration.Tests.ps1'
@@ -335,6 +340,7 @@ stages:
335340
'tests/Integration/Commands/Get-SqlDscDatabase.Integration.Tests.ps1'
336341
'tests/Integration/Commands/ConvertFrom-SqlDscDatabasePermission.Integration.Tests.ps1'
337342
'tests/Integration/Commands/New-SqlDscDatabase.Integration.Tests.ps1'
343+
'tests/Integration/Commands/New-SqlDscDatabaseSnapshot.Integration.Tests.ps1'
338344
'tests/Integration/Commands/Get-SqlDscCompatibilityLevel.Integration.Tests.ps1'
339345
'tests/Integration/Commands/Set-SqlDscDatabaseProperty.Integration.Tests.ps1'
340346
'tests/Integration/Commands/Set-SqlDscDatabaseOwner.Integration.Tests.ps1'
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<#
2+
.SYNOPSIS
3+
Defines a data file specification for a database file group.
4+
5+
.DESCRIPTION
6+
This class represents a data file specification that can be used when
7+
creating a new database. It contains the properties needed to define
8+
a data file without requiring an existing database or file group SMO object.
9+
10+
.PARAMETER Name
11+
The logical name of the data file.
12+
13+
.PARAMETER FileName
14+
The physical file path for the data file. This must be a valid path
15+
on the SQL Server instance.
16+
17+
.PARAMETER Size
18+
The initial size of the data file in kilobytes. If not specified,
19+
SQL Server will use its default initial size.
20+
21+
.PARAMETER MaxSize
22+
The maximum size to which the data file can grow in kilobytes.
23+
If not specified, the file can grow without limit (or up to disk space).
24+
25+
.PARAMETER Growth
26+
The amount by which the data file grows when it needs more space.
27+
The value is in kilobytes if GrowthType is KB, or a percentage if
28+
GrowthType is Percent. If not specified, SQL Server will use its
29+
default growth setting.
30+
31+
.PARAMETER GrowthType
32+
Specifies whether the Growth value is in kilobytes (KB) or percent (Percent).
33+
If not specified, defaults to KB.
34+
35+
.PARAMETER IsPrimaryFile
36+
Specifies that this file is the primary file in the PRIMARY file group.
37+
Only one file in the PRIMARY file group should be marked as the primary file.
38+
This property is typically used for the first file in the PRIMARY file group.
39+
40+
.NOTES
41+
This class is used to specify data file configurations when creating a new
42+
database via New-SqlDscDatabase. Unlike SMO DataFile objects, these
43+
specification objects can be created without an existing database context.
44+
45+
.EXAMPLE
46+
$fileSpec = [DatabaseFileSpec]::new()
47+
$fileSpec.Name = 'MyDatabase_Data'
48+
$fileSpec.FileName = 'C:\SQLData\MyDatabase.mdf'
49+
$fileSpec.Size = 102400 # 100 MB in KB
50+
$fileSpec.Growth = 10240 # 10 MB in KB
51+
$fileSpec.GrowthType = 'KB'
52+
53+
Creates a new data file specification with a specific size and growth settings.
54+
55+
.EXAMPLE
56+
[DatabaseFileSpec] @{
57+
Name = 'MyDatabase_Data'
58+
FileName = 'C:\SQLData\MyDatabase.mdf'
59+
IsPrimaryFile = $true
60+
}
61+
62+
Creates a new primary data file specification using hashtable syntax.
63+
#>
64+
class DatabaseFileSpec
65+
{
66+
[System.String]
67+
$Name
68+
69+
[System.String]
70+
$FileName
71+
72+
[System.Nullable[System.Double]]
73+
$Size
74+
75+
[System.Nullable[System.Double]]
76+
$MaxSize
77+
78+
[System.Nullable[System.Double]]
79+
$Growth
80+
81+
[ValidateSet('KB', 'MB', 'Percent')]
82+
[System.String]
83+
$GrowthType
84+
85+
[System.Boolean]
86+
$IsPrimaryFile = $false
87+
88+
DatabaseFileSpec()
89+
{
90+
}
91+
92+
DatabaseFileSpec([System.String] $name, [System.String] $fileName)
93+
{
94+
$this.Name = $name
95+
$this.FileName = $fileName
96+
}
97+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<#
2+
.SYNOPSIS
3+
Defines a file group specification for a database.
4+
5+
.DESCRIPTION
6+
This class represents a file group specification that can be used when
7+
creating a new database. It contains the properties needed to define
8+
a file group and its associated data files without requiring an existing
9+
database SMO object.
10+
11+
.PARAMETER Name
12+
The name of the file group. For the primary file group, this should be 'PRIMARY'.
13+
14+
.PARAMETER Files
15+
An array of DatabaseFileSpec objects that define the data files belonging
16+
to this file group. At least one file must be specified for each file group.
17+
18+
.PARAMETER ReadOnly
19+
Specifies whether the file group is read-only. If not specified, defaults
20+
to $false (read-write).
21+
22+
.PARAMETER IsDefault
23+
Specifies whether this file group should be the default file group for
24+
new objects. If not specified, defaults to $false. Typically, only the
25+
PRIMARY file group or one custom file group should be marked as default.
26+
27+
.NOTES
28+
This class is used to specify file group configurations when creating a new
29+
database via New-SqlDscDatabase. Unlike SMO FileGroup objects, these
30+
specification objects can be created without an existing database context.
31+
32+
When creating a database, you typically need at least one file group named
33+
'PRIMARY' which contains the primary data file. Additional file groups can
34+
be added for organizing data files.
35+
36+
.EXAMPLE
37+
$primaryFile = [DatabaseFileSpec] @{
38+
Name = 'MyDatabase_Primary'
39+
FileName = 'C:\SQLData\MyDatabase.mdf'
40+
IsPrimaryFile = $true
41+
}
42+
43+
$primaryFileGroup = [DatabaseFileGroupSpec]::new()
44+
$primaryFileGroup.Name = 'PRIMARY'
45+
$primaryFileGroup.Files = @($primaryFile)
46+
47+
Creates a PRIMARY file group specification with one primary data file.
48+
49+
.EXAMPLE
50+
$dataFile1 = [DatabaseFileSpec] @{
51+
Name = 'MyDatabase_Data1'
52+
FileName = 'D:\SQLData\MyDatabase_Data1.ndf'
53+
Size = 204800 # 200 MB
54+
}
55+
56+
$dataFile2 = [DatabaseFileSpec] @{
57+
Name = 'MyDatabase_Data2'
58+
FileName = 'D:\SQLData\MyDatabase_Data2.ndf'
59+
Size = 204800 # 200 MB
60+
}
61+
62+
$secondaryFileGroup = [DatabaseFileGroupSpec] @{
63+
Name = 'SECONDARY'
64+
Files = @($dataFile1, $dataFile2)
65+
}
66+
67+
Creates a SECONDARY file group specification with two data files.
68+
69+
.EXAMPLE
70+
[DatabaseFileGroupSpec] @{
71+
Name = 'PRIMARY'
72+
Files = @(
73+
[DatabaseFileSpec] @{
74+
Name = 'MyDB_Primary'
75+
FileName = 'C:\SQLData\MyDB.mdf'
76+
}
77+
)
78+
}
79+
80+
Creates a PRIMARY file group using hashtable syntax with an embedded file spec.
81+
#>
82+
class DatabaseFileGroupSpec
83+
{
84+
[System.String]
85+
$Name
86+
87+
[DatabaseFileSpec[]]
88+
$Files
89+
90+
[System.Boolean]
91+
$ReadOnly = $false
92+
93+
[System.Boolean]
94+
$IsDefault = $false
95+
96+
DatabaseFileGroupSpec()
97+
{
98+
}
99+
100+
DatabaseFileGroupSpec([System.String] $name)
101+
{
102+
$this.Name = $name
103+
}
104+
105+
DatabaseFileGroupSpec([System.String] $name, [DatabaseFileSpec[]] $files)
106+
{
107+
$this.Name = $name
108+
$this.Files = $files
109+
}
110+
}

0 commit comments

Comments
 (0)