/
AboutNewDatabases.Koans.ps1
204 lines (190 loc) · 8.27 KB
/
AboutNewDatabases.Koans.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#Requires -Modules dbatools
using module PSKoans
[Koan(Position = 1003, Module = 'dbatools')]
param()
<#
New-DbaDatabase
Getting details and querying a database are useful when there is a database to query.
What if we need to create a new database?
dbatools has us covered with New-DbaDatabase
#>
Describe 'New-DbaDatabase' {
#region Mocks
<#
Let's set up the environment for you.
Unless you want the Koans to nearly always fail, I would suggest not messing with this bit.
#>
BeforeAll {
Mock -CommandName New-DbaDatabase -MockWith {
[PSCustomObject]@{
ComputerName = 'localhost'
InstanceName = 'MSSQLSERVER'
SqlInstance = 'localhost'
Name = 'random-123456789'
Status = 'Normal'
IsAccessible = $true
RecoveryModel = 'Simple'
LogReuseWaitStatus = 'Nothing'
SizeMB = 16
Compatibility = 'Version140'
Collation = 'SQL_Latin1_General_CP1_CI_AS'
Owner = $ENV:USERNAME
LastFullBackup = Get-Date '0001-01-01'
LastDiffBackup = Get-Date '0001-01-01'
LastLogBackup = Get-Date '0001-01-01'
}
} -ParameterFilter {$_.SqlInstance -eq 'localhost'}
Mock -CommandName New-DbaDatabase -MockWith {
[PSCustomObject]@{
ComputerName = 'localhost'
InstanceName = 'MSSQLSERVER'
SqlInstance = 'localhost'
Name = 'RandomIsGood'
Status = 'Normal'
IsAccessible = $true
RecoveryModel = 'Simple'
LogReuseWaitStatus = 'Nothing'
SizeMB = 16
Compatibility = 'Version140'
Collation = 'SQL_Latin1_General_CP1_CI_AS'
Owner = $ENV:USERNAME
LastFullBackup = Get-Date '0001-01-01'
LastDiffBackup = Get-Date '0001-01-01'
LastLogBackup = Get-Date '0001-01-01'
},
[PSCustomObject]@{
ComputerName = 'localhost'
InstanceName = 'MSSQLSERVER'
SqlInstance = 'localhost'
Name = 'SpecificIsBetter'
Status = 'Normal'
IsAccessible = $true
RecoveryModel = 'Simple'
LogReuseWaitStatus = 'Nothing'
SizeMB = 16
Compatibility = 'Version140'
Collation = 'SQL_Latin1_General_CP1_CI_AS'
Owner = $ENV:USERNAME
LastFullBackup = Get-Date '0001-01-01'
LastDiffBackup = Get-Date '0001-01-01'
LastLogBackup = Get-Date '0001-01-01'
}
} -ParameterFilter {
$_.SqlInstance -eq 'localhost' -and
@('RandomIsGood', 'SpecificIsBetter') -in $_.Name
}
Mock -CommandName New-DbaDatabase -MockWith {
'Val', 'Dev', 'Prod' | ForEach-Object -Process {
[PSCustomObject]@{
ComputerName = $_
InstanceName = 'MSSQLSERVER'
SqlInstance = $_
Name = 'DBScripts'
Status = 'Normal'
IsAccessible = $true
RecoveryModel = 'Simple'
LogReuseWaitStatus = 'Nothing'
SizeMB = 16
Compatibility = 'Version140'
Collation = 'SQL_Latin1_General_CP1_CI_AS'
Owner = $ENV:USERNAME
LastFullBackup = Get-Date '0001-01-01'
LastDiffBackup = Get-Date '0001-01-01'
LastLogBackup = Get-Date '0001-01-01'
}
}
} -ParameterFilter {
$_.Name -eq 'DBScripts' -and
@('Val', 'Dev', 'Prod') -in $_.SqlInstance
}
Mock -CommandName New-DbaDatabase -MockWith {
[PSCustomObject]@{
FileGroups = @{
Files = @{
FileName = 'E:\DATA\TestDataLogFile_PRIMARY.mdf'
}
}
LogFiles = @{
FileName = 'E:\LOG\TestDataLogFile_Log.ldf'
}
}
} -ParameterFilter {
$_.DataFilePath -eq 'E:\DATA' -and
$_.LogFilePath -eq 'E:\LOG'
}
Mock -CommandName New-DbaDatabase -MockWith {
[PSCustomObject]@{
ComputerName = 'localhost'
InstanceName = 'MSSQLSERVER'
SqlInstance = 'localhost'
Name = 'DBScripts'
Status = 'Normal'
IsAccessible = $true
RecoveryModel = 'FULL'
LogReuseWaitStatus = 'Nothing'
SizeMB = 16
Compatibility = 'Version140'
Collation = 'SQL_Latin1_General_CP1_CI_AS'
Owner = $ENV:USERNAME
LastFullBackup = Get-Date '0001-01-01'
LastDiffBackup = Get-Date '0001-01-01'
LastLogBackup = Get-Date '0001-01-01'
}
} -ParameterFilter {
$_.SqlInstance -eq 'localhost' -and
$_.RecoveryModel -eq 'Full'
}
}
#endregion
It 'creates a new database on the specified instance' {
<#
New-DbaDatabase, while having a few different parameters, requires only that you have an instance
of SQL Server that you can connect to and it can create a database on.
Complete the below to create a new database on localhost.
#>
$NewDatabase = New-DbaDatabase -SqlInstance '____'
$NewDatabase.ComputerName | Should -Be 'localhost'
}
It 'creates databases with specific names' {
<#
While only specifying the server name is convenient, it is rare that we want a randomly named
database. Most of the time we want to specify the database name(s) to be created.
Luckily, New-DbaDatabase allows us to do that!
#>
$NewSpecifiedDatabase = New-DbaDatabase -SqlInstance localhost -Name '____', '____'
$NewSpecifiedDatabase.Name | Should -Be 'RandomIsGood', 'SpecificIsBetter'
}
It 'creates a database on multiple specified instances' {
<#
If you're interacting with databases regularly then you more than likely have a list of
scripts that you run often enough to save. You can save these scripts as files or you can
save them to a database on each server.
If only there was a way to create a database on your Dev, Val, and Prod servers that you
can save these scripts to...
#>
$NewScriptsDatabase = New-DbaDatabase -SqlInstance '____', '____', '____' -Name DBScripts
$NewScriptsDatabase.SqlInstance | Should -Be 'Dev', 'Val', 'Prod'
}
It 'creates a new database with a specified recovery model' {
<#
When new databases get created they get created with a recovery model. They take this
recovery model from the system database model.
New-DbaDatabase allows you to specify a recovery model that you want the new database to
have. This recovery model does not need to be what the model database is set to.
Finish the below block of code to create a new database with the FULL recovery model.
#>
$NewRecoveryModelDatabase = New-DbaDatabase -SqlInstance localhost -RecoveryModel '____'
$NewRecoveryModelDatabase.RecoveryModel | Should -Be 'Full'
}
It 'creates a database with a specified path for data and log files' {
<#
There is also an option to specify where to place the database data and log files. By using the parameters
-DataFilePath and -LogFilePath, you can specify where to place these files.
#>
$NewFilePathDatabase = New-DbaDatabase -SqlInstance localhost -Name FilePathTese -DataFilePath '____' -LogFilePath '___'
$DataPath = $NewFilePathDatabase.FileGroups.Files
$LogPath = $NewFilePathDatabase.LogFiles
(Split-Path -Path $DataPath).FileName | Should -Be 'E:\DATA'
(Split-Path -Path $LogPath).FileName | Should -BE 'E:\LOG'
}
}