Skip to content
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

Add Dbatools Koans #140

Merged
merged 135 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
135 commits
Select commit Hold shift + click to select a range
ffcd62c
Basic scaffolding.
shaneis Dec 6, 2018
4b29f41
Added initial dbadatabase koan
shaneis Feb 17, 2019
9a5778e
Merge pull request #1 from vexx32/dbatools
shaneis Feb 18, 2019
c3ba20f
Fixed typo in the description block
shaneis Feb 26, 2019
8098297
Added extra description to first Get-DbaDatabase.ps1` Koan
shaneis Feb 26, 2019
de3e85a
Created Mocks folder in dbatools
shaneis Feb 26, 2019
e1e328c
changed mocks to call the xml database object
shaneis Feb 26, 2019
f98d5b2
Proper grammer is important
shaneis Feb 26, 2019
03d1424
Added test for passed in testdb name
shaneis Feb 26, 2019
4d07749
Move verbose messages
shaneis Feb 26, 2019
f0b5342
Changed details to not have localhost
shaneis Mar 2, 2019
f4cad79
Added Koan for using Get-DbaDatabase for all databases.
shaneis Mar 3, 2019
74fe873
Made testdb information consistent and fixed check on number of datab…
shaneis Mar 3, 2019
9534a2b
Added xml file for system databases.
shaneis Mar 3, 2019
f44aabc
Added koan for parameter: -ExcludeUser.
shaneis Mar 3, 2019
480742c
Added koan for the parameter: -ExcludeSystem.
shaneis Mar 3, 2019
7120462
Added example of using parameter: -ExcludeDatbase.
shaneis Mar 3, 2019
7b0ee91
Added koan of using parameter: -RecoveryModel.
shaneis Mar 3, 2019
92501f5
Basic scaffolding.
shaneis Dec 6, 2018
4a67dd2
Added initial dbadatabase koan
shaneis Feb 17, 2019
71feefc
Fixed typo in the description block
shaneis Feb 26, 2019
689924d
Added extra description to first Get-DbaDatabase.ps1` Koan
shaneis Feb 26, 2019
06ce0b2
Created Mocks folder in dbatools
shaneis Feb 26, 2019
db284b9
changed mocks to call the xml database object
shaneis Feb 26, 2019
e59af3a
Proper grammer is important
shaneis Feb 26, 2019
0d54074
Added test for passed in testdb name
shaneis Feb 26, 2019
b49588b
Move verbose messages
shaneis Feb 26, 2019
9cafaf6
Changed details to not have localhost
shaneis Mar 2, 2019
0bb505a
Fixed merge conficts.
shaneis Mar 5, 2019
48d9546
Removed evidence of my merge conflict resolution.
shaneis Mar 5, 2019
e298116
Pascal cased!
shaneis Mar 6, 2019
6fb5e46
If it is not necessary, why speak it?
shaneis Mar 6, 2019
af1e872
Do not repeat if it does not teach.
shaneis Mar 6, 2019
e0d4e1b
Bye bye extra by.
shaneis Mar 6, 2019
20be87a
Merging the commits made the sentences not make sense.
shaneis Mar 6, 2019
a0e7d18
Let's simplify the pipes.
shaneis Mar 6, 2019
59a2aed
Splatting through the KOAN.
shaneis Mar 7, 2019
e76749b
Added file.
shaneis Mar 10, 2019
1365dea
Added start description.
shaneis Mar 10, 2019
593ec62
First look at querying databases.
shaneis Mar 10, 2019
42645f6
Changed query to return meaningful data.
shaneis Mar 14, 2019
dc4b561
Added mock file for querying multiple servers.
shaneis Mar 18, 2019
96a11d9
Added koan for querying multiple servers.
shaneis Mar 18, 2019
ada3681
Added warning on not messing with Mocks.
shaneis Apr 2, 2019
1c897bd
Added warning on not messing with Mocks to AboutDbaDatabase.ps1
shaneis Apr 2, 2019
a6901dc
Removed unneeded parantheses surrounding variables.
shaneis Apr 2, 2019
58b9fe6
Removed backticks.
shaneis Apr 2, 2019
dd18ba0
Added explanation for @@SERVERNAME.
shaneis Apr 2, 2019
349ba75
Added newline to EOF for Unix.
shaneis Apr 2, 2019
838e266
Added next description for using -File parameter of Invoke-DbaQuery.
shaneis Apr 2, 2019
bc3a7cc
Invoke-DbaQuery with a file.
shaneis Apr 15, 2019
e81e399
Boiler plate added for using -SqlParameters parameter.
shaneis Apr 23, 2019
8544581
Changing 4 ___ 2 2 __.
shaneis Apr 27, 2019
7b43374
Changed query to be ready for Lil Bobby.
shaneis May 1, 2019
98c926a
Mocks! Mocks need to be updated too!
shaneis May 1, 2019
d4ab406
Wrapped string blanks in quotes.
shaneis May 1, 2019
e8a9731
Specified switch parameter.
shaneis May 15, 2019
5797d86
Cleared up grammer.
shaneis May 17, 2019
474dd52
Added comments for Lil Bobby Tables.
shaneis May 21, 2019
97936b6
Querying the Student table.
shaneis May 23, 2019
7607299
added mock and fixed typo bug on parameterfilters.
shaneis May 23, 2019
b5f592f
Mock file for StudentTable.
shaneis May 23, 2019
f850d98
[Bug fix]: Using $SqlParamResult01 instead of 02.
shaneis May 27, 2019
af39ccd
Added StudentTableParams.xml file for mocks.
shaneis May 27, 2019
5b18b84
Added example 2 of safe way of adding Bobby Tables.
shaneis Jun 14, 2019
b51ad43
Mock file for example 2 Bobby Tables.
shaneis Jun 14, 2019
0b5721c
Added mock for running example 2 bobby tables.
shaneis Jun 14, 2019
9e2cbf8
You know, some of these need a database parameter... :|
shaneis Jun 14, 2019
bd20eaa
*sigh* Can't even spell Bobby Tables name...
shaneis Jun 14, 2019
547e406
Added mock for SQL Injection.
shaneis Jul 1, 2019
bc2acb1
Splat for unsafe insert
shaneis Jul 22, 2019
d3d3096
Bobby tables test added
shaneis Jul 22, 2019
e5519ae
added needed -EnableException switch
shaneis Jul 22, 2019
2e5e334
Added comments about Bobby
shaneis Jul 22, 2019
10f6827
Started Backup-DbaDatabase
shaneis Jul 25, 2019
fc67fd9
Bumped koan position up, we'll do new-dbadatabase first
shaneis Jul 31, 2019
b8be891
Set up New-DbaDatabase koan
shaneis Jul 31, 2019
908bf87
Added Mock for basic New-DbaDatabase command
shaneis Aug 6, 2019
a468b2c
First example : default database
shaneis Aug 12, 2019
dfb33e1
Added Koan for New-DbaDatabase with Name parameter
shaneis Sep 3, 2019
2ff7612
Added mock for New-DbaDatabase with Name parameter
shaneis Sep 3, 2019
018d9bd
Koan for new database on multiple servers
shaneis Sep 5, 2019
fff2618
Created mock for new database on multiple servers
shaneis Sep 5, 2019
6c4237e
Added comment on placing data and log files
shaneis Oct 3, 2019
9aef750
Added mock for New-DbaDatabases and data / file paths
shaneis Oct 3, 2019
fe26517
Added koan for New-DbaDatabase and log / data file path
shaneis Oct 3, 2019
7cff6e2
Added comment on -Recoverymodel parameter
Oct 11, 2019
5b325e1
Added Mock for New-DbaDatabase with Full recovery model
Oct 16, 2019
86221fc
Added koan for New-DbaDatabase and -RecoveryModel
Oct 16, 2019
a83ca23
Added opening comments to AboutBackupDatabases.ps1
Nov 4, 2019
40bea25
Added default mock for Backup-DbaDatabase
Nov 11, 2019
4e19c1d
Added initial koan to backup all databases
Nov 11, 2019
afe16bc
Added comment for specific database backup Koan
Nov 12, 2019
4dfb6be
Added mock for specific database backup Koan. BUG FIX: fixed TotalSiz…
Nov 12, 2019
1a34b3e
Added koan for creating a backup of specific databases.
Nov 27, 2019
e1f7f2f
Added koan for types of backups.
shaneis Dec 3, 2019
195a417
Added koan for specific types of backups.
Dec 13, 2019
4295f15
Fixed merge conflict on pull
Jan 20, 2020
fe2ce80
Added comment for the -Path parameter
Jan 20, 2020
1bd07bd
Added mock about diff backups
Feb 6, 2020
ac8f5a4
Added backing up a database to a specific path.
shaneis Mar 15, 2020
885533f
Added mock for backing up a database to a specific path.
shaneis Mar 15, 2020
bf61b5a
Added using keywords for backup file names.
shaneis Mar 16, 2020
0d713a9
Bug fix: Fixed the typo on the variable name.
shaneis Mar 16, 2020
bc93bca
Added mock for using keywords.
shaneis Mar 16, 2020
58e13a5
Removed the '#Requires' line. Added a 'Module = 'dbatools') to the 'K…
Mar 18, 2020
cb93d88
New lines ftw!
Mar 18, 2020
07cf8ef
Added #region Mocks / #endregion around mocks.
Mar 18, 2020
1280427
Wrapped Mocks in a BeforeAll region.
Mar 18, 2020
63be11f
Added back the #Requires line.
shaneis Mar 21, 2020
981a5b7
Renamed files to 'Koans.ps1'.
shaneis Mar 21, 2020
ad03a95
Wrapped It block around backing up all SQL Server databases.
shaneis Mar 22, 2020
fa5fc49
Wrapped it block around specific database backups and types of backups.
shaneis Mar 22, 2020
3d3f004
Wrapped It block around connecting to SQL Server Instance.
Apr 7, 2020
79a7b56
Added It block around connecting to specific database name.
Apr 7, 2020
7b4fda4
Added It block around getting system databases.
Apr 7, 2020
a5e3ff3
Added It block around excluding system databases.
Apr 7, 2020
aac18be
Added It block around gathering databases based on their recovery model.
Apr 7, 2020
8270a24
Added It block around querying a default SQL Server instance.
Apr 7, 2020
ae5c37a
Added It block around querying multiple SQL Server instances.
Apr 7, 2020
fbadecb
Merge branch 'dbatools' of https://github.com/shaneis/PSKoans into db…
shaneis Apr 20, 2020
4018976
Wrapped It block around storing backups in specific locations.
shaneis Apr 20, 2020
c6d6f09
ACTUALLY wrapped It block around storing backups in specific locations.
shaneis Apr 20, 2020
a193e41
Wrapped It block around substituting keywords in file names.
shaneis Apr 20, 2020
68c1a73
Removed unnecessary brakets from Mocks.
shaneis Apr 20, 2020
c2c020b
Added a $ to the ExcludeUser param.
Apr 21, 2020
798681b
Wrapped It block around creating a new database.
Apr 21, 2020
2f5b3ae
Wrapped It block around creating new databases with specific names.
Apr 21, 2020
42d56bc
Wrapped It block around creating a database on multiple specified ins…
Apr 21, 2020
15299a7
Wrapped It block around creating a database with a specified recovery…
Apr 21, 2020
632bccd
Wrapped It block around creating a database with specified data and l…
Apr 21, 2020
2767e30
Extended the underscores from 2 to 4 on New-DbaDatabase -SqlInstance …
Apr 21, 2020
048a286
Wrapped It block around querying a database from a file.
Apr 21, 2020
57d4ebe
Wrapped It block around querying a database and passing values to par…
Apr 21, 2020
54d28fa
Wrapped It block around poor lil' Bobby Tables.
Apr 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 180 additions & 0 deletions PSKoans/Koans/Modules/dbatools/AboutBackupDatabases.Koans.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
#Requires -Modules dbatools
using module PSKoans
[Koan(Position = 1004, Module = 'dbatools')]
param()
<#
Backup-DbaDatabase

Backing up a database is crucially important to all who use databases.

If there is a need to recover data from an earlier time, then backups get used to recover the data.

There are many filters and options for backing up a database. dbatools helps you to take a backup
with the Backup-DbaDatabase command.
#>
Describe "Backup-DbaDatabase" {

#region Mocks
<#
Let's setup the environment for you. Unless you want the Koans to nearly always fail, I would
suggest not messing with this bit.
#>
BeforeAll {
Mock -CommandName Backup-DbaDatabase -MockWith {
$StartDate = [Datetime]::new(2019, 01, 01, 12, 00, 00)
1..10 | ForEach-Object -Process {
$RestoreStart = $StartDate
$RestoreEnd = $StartDate.AddSeconds(10)

[PSCustomObject]@{
ComputerName = 'localhost'
SqlInstance = $ENV:COMPUTERNAME
Database = ('Database_{0:d2}' -f $_)
Type = 'Full'
TotalSize = ('{0}' -f ([Math]::PI * $_))
DeviceType = 'Disk'
Start = $RestoreStart
Duration = $RestoreEnd - $RestoreStart
End = $RestoreEnd
}
$StartDate = $RestoreEnd
}
}
Mock -CommandName Backup-DbaDatabase -MockWith {
$StartDate = [Datetime]::new(2019, 01, 01, 12, 00, 00)
1..2 | ForEach-Object -Process {
$RestoreStart = $StartDate
$RestoreEnd = $StartDate.AddSeconds(15)

[PSCustomObject]@{
ComputerName = 'localhost'
SqlInstance = $ENV:COMPUTERNAME
Database = ('Database_{0:d2}' -f $_)
Type = 'Full'
TotalSize = ('{0}' -f ([Math]::PI * $_))
DeviceType = 'Disk'
Start = $RestoreStart
Duration = $RestoreEnd - $RestoreStart
End = $RestoreEnd
}

$StartDate = $RestoreEnd
}
} -ParameterFilter {
$_.Database -contains ('Database_01', 'Database_02')
}
Mock -CommandName Backup-DbaDatabase -MockWith {
$StartDate = [Datetime]::new(2019, 01, 01, 12, 00, 00)
$RestoreEnd = $StartDate.AddSeconds(5)
[PSCustomObject]@{
ComputerName = 'localhost'
SqlInstance = $ENV:COMPUTERNAME
Database = 'Database_01'
Type = 'Differential'
TotalSize = ('{0}' -f ([Math]::PI))
DeviceType = 'Disk'
Start = $StartDate
Duration = $RestoreEnd - $StartDate
End = $RestoreEnd
}
} -ParameterFilter {
$_.Type -eq 'Differential'
}
Mock -CommandName Backup-DbaDatabase -MockWith {
$StartDate = [Datetime]::new(2019, 01, 01, 12, 00, 00)
$RestoreEnd = $StartDate.AddSeconds(5)
[PSCustomObject]@{
ComputerName = 'localhost'
SqlInstance = $ENV:COMPUTERNAME
Database = 'Database_01'
Type = 'Differential'
TotalSize = ('{0}' -f ([Math]::PI))
DeviceType = 'Disk'
Start = $StartDate
Duration = $RestoreEnd - $StartDate
End = $RestoreEnd
Path = 'E:\Backups\Database_01_201901011200.bak'
}
} -ParameterFilter {
$_.Path -like 'E:\Backups\*'
}
Mock -CommandName Backup-DbaDatabase -MockWith {
$StartDate = [Datetime]::new(2019, 01, 01, 12, 00, 00)
$RestoreEnd = $StartDate.AddSeconds(5)
[PSCustomObject]@{
ComputerName = 'localhost'
SqlInstance = $ENV:COMPUTERNAME
Database = 'Database_01'
Type = 'Differential'
TotalSize = ('{0}' -f ([Math]::PI))
DeviceType = 'Disk'
Start = $StartDate
Duration = $RestoreEnd - $StartDate
End = $RestoreEnd
Path = 'E:\Backups\Database01-Full.bak'
}
} -ParameterFilter {
$_.FilePath -eq 'dbname-backuptype.bak'
}
}
#endregion

It 'takes a database backup of all SQL Server databases' {
<#
By default, Backup-DbaDatabase will backup every database on the SQL Instance.
These backups will get saved to the default backup directory.
Complete the below command to backup all the databases on the localhost instance.
#>
$AllBackups = Backup-DbaDatabase -SqlInstance '____'
$AllBackups.ComputerName | Should -Be 'localhost'
}

It 'takes a database backup of specific SQL Server databases' {
<#
By using the -Database parameter, you can backup a single or many databases.
Complete the below command to backup the databases Database_01 and Database_02
#>
$SpecificBackups = Backup-DbaDatabase -SqlInstance localhost -Database '____','____'
$SpecificBackups.Database | Should -Contain 'Database_01', 'Database_02'
}

It 'takes different types of SQL Server backups' {
<#
There are different types of database backups that can be taken. The 3 main ones
are a Full backup (Full/Database), a Differential backup (Diff/Differential), and
a Transaction Log backup (Log).
Backup-DbaDatabase can take each of these types of backups.
Complete the below command to take a differential backup of the Database_01 database.
#>
$DifferentialBackup = Backup-DbaDatabase -SqlInstance localhost -Database Database_01 -Type '____'
$DifferentialBackup.Type | Should -Be 'Differential'
}

It 'takes backups and stores them at a specific location' {
<#
Managing where you store your backups is something that is unique to each person.
Backup-DbaDatabase allows you to specify where you want the backups to be stored.
It allows this specification by using the -Path parameter.
Complete the below command to store the backup file in the E:\Backups\ folder.
#>
$SpecificPathBackup = Backup-DbaDatabase -SqlInstance localhost -Database Database_01 -Path '____'
$SpecificPathBackup.Path | Should -BeLike 'E:\Backups\'
}

It 'takes backups and substitutes keywords such as database name and database backup type' {
<#
There are different conventions for specifying where the backup file will go to
and what the backup file will be called. There are different keywords that
dbatools provides that can be used with the -FilePath and -ReplaceInName.
instancename - will be replaced with the instance name
servername - will be replaced with the server name
dbname - will be replaced with the database name
timestamp - will be replaced with the timestamp
backuptype - will be replaced with Full, Log or Differential as appropriate.
Complete the below command to backup the database "Database01" as "Database01-Full.bak"
#>
$KeywordBackup = Backup-DbaDatabase -SqlInstance localhost -Database Database01 -FilePath '____-____.bak' -ReplaceInName
$BackupFileName = Split-Path $KeywordBackup.Path -Leaf
$BackupFileName | Should -Be 'Database01-Full.bak'
}
}
101 changes: 101 additions & 0 deletions PSKoans/Koans/Modules/dbatools/AboutDbaDatabase.Koans.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#Requires -Modules dbatools
using module PSKoans
[Koan(Position = 1001, Module = 'dbatools')]
param()
<#
Get-DbaDatabase

The Get-DbaDatabase command gets SQL database information for each database
that is present on the target instance(s) of SQL Server by default. If the
name of the database is provided, the command will return only the specific
database.
#>
Describe 'Get-DbaDatabase' {

#region Mocks
<#
Let's setup the environment for you. Unless you want your Koans to
nearly always fail I'd suggest not messing with this bit.
#>
BeforeAll {
Mock -CommandName Get-DbaDatabase -MockWith {
Import-Clixml -Path .\PSKoans\Koans\dbatools\Mocks\Database_All.xml
} -ParameterFilter { $_.SqlInstance -eq 'localhost' }

Mock -CommandName Get-DbaDatabase -MockWith {
Import-Clixml -Path .\PSKoans\Koans\dbatools\Mocks\Database_TestDb.xml
} -ParameterFilter { $_.SqlInstance -eq 'localhost' -and $_.Database -eq 'testdb' }

Mock -CommandName Get-DbaDatabase -MockWith {
Import-Clixml -Path .\PSKoans\Koans\dbatools\Mocks\Database_System.xml
} -ParameterFilter { $_.SqlInstance -eq 'localhost' -and $_.ExcludeUser }

Mock -CommandName Get-DbaDatabase -MockWith {
Import-Clixml -Path .\PSKoans\Koans\dbatools\Mocks\Database_TestDb.xml
} -ParameterFilter { $_.SqlInstance -eq 'localhost' -and $_.ExludeSystem }
}
#endregion

It 'Gathers databases by SQL Server instance...' {
<#
Get-DbaDatabase requires one thing; A SQL Server instance name.
You can pass in "localhost" for the default name for a SQL Server
instance.
The simplest usage of Get-DbaDatabase is to run it and passing in the
name of the SQL Server instance. This will get information about all
the databases on the instance.
#>
$AllDatabases = Get-DbaDatabase -SqlInstance ____
$AllDatabases.Count | Should -Be 5
}

It 'Gathers database by SQL Server instance and specific name...' {
<#
By passing in the SQL Server instance and the name of a specific
database, using the -Database parameter, we can get information on
that single database instead.
#>
$MasterDatabase = Get-DbaDatabase -SqlInstance localhost -Database ____
$MasterDatabase.Name | Should -Be 'testdb'
}

It 'Gathers system databases only if specified...' {
<#
You may want to get only the system databases on an instance.
While you can pass in the specific names of the system databases, it
would be easier if there was a parameter you could add that would
return the system databases.

A switch parameter like -ExcludeUser.
#>
$UserDbParams = @{
SqlInstance = 'localhost'
ExcludeUser = $____
}
$UserDbsExcluded = Get-DbaDatabase @UserDbParams
$UserDbsExcluded.Name | Should -BeIn 'tempdb', 'master', 'model', 'msdb'
}

It 'Excludes system databases if specified...' {
<#
The same can be done to exclude system databases by providing the
-ExcludeSystem parameter switch.
#>
$SystemDbParams = @{
SqlInstance = 'localhost'
ExludeSystem = ____
}
$SystemDbsExluded = Get-DbaDatabase @SystemDbParams
$SystemDbsExluded.Name | Should -Be 'testdb'
}

It 'Gathers databases based on their recovery model...' {
<#
Some common questions that people who work with databases
may have getting databases that are in the 'Full', 'Simple', or
'BulkLogged' recovery models.
#>
$FullRecoveryDbs = Get-DbaDatabase -SqlInstance localhost -RecoveryModel ____
$FullRecoveryDbs.RecoveryModel | Should -Be 'Full'
}
}
Loading