Skip to content

Commit 904ea06

Browse files
authored
Merge pull request #83 from sqlcollaborative/development
0.5.7
2 parents 25637d0 + eaae32e commit 904ea06

File tree

9 files changed

+169
-49
lines changed

9 files changed

+169
-49
lines changed

bin/deploy.ps1

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ Param (
3131
[string]$Type = 'SQLServer'
3232
)
3333

34-
#Import module
35-
If (-not (Get-Module dbops)) {
36-
Import-Module "$PSScriptRoot\Modules\dbops\dbops.psd1"
34+
#Import modules
35+
foreach ($module in @('PSFramework', 'dbops')) {
36+
if (-not (Get-Module $module)) {
37+
Import-Module "$PSScriptRoot\Modules\$module"
38+
}
3739
}
38-
. "$PSScriptRoot\Modules\dbops\internal\classes\DBOps.enums.ps1"
3940

4041
$config = Get-DBOConfig -Path "$PSScriptRoot\dbops.config.json" -Configuration $Configuration
4142

@@ -62,7 +63,8 @@ foreach ($key in ($PSBoundParameters.Keys)) {
6263

6364
if ($PSCmdlet.ShouldProcess($params.PackageFile, "Initiating the deployment of the package")) {
6465
Invoke-DBODeployment @params
65-
} else {
66+
}
67+
else {
6668
Invoke-DBODeployment @params -WhatIf
6769
}
6870

dbops.psm1

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ Register-PSFConfigValidation -Name "transaction" -ScriptBlock {
2929
try {
3030
if (([string]$Value) -in @('SingleTransaction', 'TransactionPerScript', 'NoTransaction')) {
3131
$Result.Value = [string]$Value
32-
} else {
32+
}
33+
else {
3334
$Result.Message = "Allowed values: SingleTransaction, TransactionPerScript, NoTransaction"
3435
$Result.Success = $False
3536
}
36-
} catch {
37+
}
38+
catch {
3739
$Result.Message = "Failed to convert value to string"
3840
$Result.Success = $False
3941
}
@@ -53,7 +55,8 @@ Register-PSFConfigValidation -Name "securestring" -ScriptBlock {
5355
}
5456
if ($Value -is [securestring]) {
5557
$Result.Value = $Value
56-
} else {
58+
}
59+
else {
5760
$Result.Message = 'Only [securestring] is accepted'
5861
$Result.Success = $False
5962
}
@@ -73,11 +76,13 @@ Register-PSFConfigValidation -Name "hashtable" -ScriptBlock {
7376
try {
7477
if (([hashtable]$Value) -is [hashtable]) {
7578
$Result.Value = [hashtable]$Value
76-
} else {
79+
}
80+
else {
7781
$Result.Message = "Only hashtables are allowed"
7882
$Result.Success = $False
7983
}
80-
} catch {
84+
}
85+
catch {
8186
$Result.Message = "Failed to convert value to hashtable. Only hashtables are allowed."
8287
$Result.Success = $False
8388
}
@@ -98,11 +103,13 @@ Register-PSFConfigValidation -Name "connectionType" -ScriptBlock {
98103
try {
99104
if (([string]$Value) -is [string] -and [string]$Value -in $allowedTypes) {
100105
$Result.Value = [string]$Value
101-
} else {
106+
}
107+
else {
102108
$Result.Message = $failMessage
103109
$Result.Success = $False
104110
}
105-
} catch {
111+
}
112+
catch {
106113
$Result.Message = "Failed to convert value to string. $failMessage"
107114
$Result.Success = $False
108115
}
@@ -138,6 +145,7 @@ Set-PSFConfig -FullName dbops.mail.Subject -Value "DBOps deployment status" -Ini
138145
Set-PSFConfig -FullName dbops.security.encryptionkey -Value "~/.dbops.key" -Initialize -Description "Path to a custom encryption key used to encrypt/decrypt passwords. The key should be a binary file with a length of 128, 192 or 256 bits. Key will be generated automatically if not exists."
139146
Set-PSFConfig -FullName dbops.security.usecustomencryptionkey -Value ($PSVersionTable.Platform -eq 'Unix') -Validation bool -Initialize -Description "Determines whether to use a custom encryption key for storing passwords. Enabled by default only on Unix platforms."
140147
Set-PSFConfig -FullName dbops.rdbms.type -Value 'SqlServer' -Validation connectionType -Initialize -Description "Assumes a certain RDBMS as a default one for each command. SQLServer by default"
148+
Set-PSFConfig -FullName dbops.package.slim -Value $false -Validation bool -Initialize -Description "Decides whether to make the packages 'slim' and omit module files when creating the package. Default: false."
141149

142150
# extensions for SMO
143151
$typeData = Get-TypeData -TypeName 'Microsoft.SqlServer.Management.Smo.Database'

functions/Install-DBOPackage.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,19 @@
114114
115115
.EXAMPLE
116116
# Installs package using specific connection parameters
117-
.\MyPackage.zip | Install-DBOPackage -SqlInstance 'myserver\instance1' -Database 'MyDb' -ExecutionTimeout 3600
117+
'.\MyPackage.zip' | Install-DBOPackage -SqlInstance 'myserver\instance1' -Database 'MyDb' -ExecutionTimeout 3600
118118
119119
.EXAMPLE
120120
# Installs package using custom logging parameters and schema tracking table
121-
.\MyPackage.zip | Install-DBOPackage -SchemaVersionTable dbo.SchemaHistory -OutputFile .\out.log -Append
121+
'.\MyPackage.zip' | Install-DBOPackage -SchemaVersionTable dbo.SchemaHistory -OutputFile .\out.log -Append
122122
123123
.EXAMPLE
124124
# Installs package using custom configuration file
125-
.\MyPackage.zip | Install-DBOPackage -ConfigurationFile .\localconfig.json
125+
'.\MyPackage.zip' | Install-DBOPackage -ConfigurationFile .\localconfig.json
126126
127127
.EXAMPLE
128128
# Installs package using variables instead of specifying values directly
129-
.\MyPackage.zip | Install-DBOPackage -SqlInstance '#{server}' -Database '#{db}' -Variables @{server = 'myserver\instance1'; db = 'MyDb'}
129+
'.\MyPackage.zip' | Install-DBOPackage -SqlInstance '#{server}' -Database '#{db}' -Variables @{server = 'myserver\instance1'; db = 'MyDb'}
130130
#>
131131
# ShouldProcess is handled in the underlying command
132132
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "")]

functions/New-DBOPackage.ps1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
Runs a regex verification against provided file names using the provided Match string.
5757
Example: .*\.sql
5858
59+
.PARAMETER Slim
60+
Do not include accompanying modules into the package file.
61+
5962
.PARAMETER Confirm
6063
Prompts to confirm certain actions
6164
@@ -100,7 +103,8 @@
100103
[switch]$Absolute,
101104
[switch]$Relative,
102105
[switch]$NoRecurse,
103-
[string[]]$Match
106+
[string[]]$Match,
107+
[switch]$Slim = (Get-DBODefaultSetting -Name package.slim -Value)
104108
)
105109

106110
begin {
@@ -122,6 +126,7 @@
122126

123127
#Create a package object
124128
$package = [DBOpsPackage]::new()
129+
$package.Slim = $Slim
125130

126131
#Get configuration object according to current config options
127132
if (Test-PSFParameterBinding -ParameterName ConfigurationFile -BoundParameters $PSBoundParameters) {

functions/Register-DBOPackage.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,19 @@ function Register-DBOPackage {
104104
105105
.EXAMPLE
106106
# Register package scripts in a target database using specific connection parameters
107-
.\MyPackage.zip | Register-DBOPackage -SqlInstance 'myserver\instance1' -Database 'MyDb' -ExecutionTimeout 3600
107+
'.\MyPackage.zip' | Register-DBOPackage -SqlInstance 'myserver\instance1' -Database 'MyDb' -ExecutionTimeout 3600
108108
109109
.EXAMPLE
110110
# Register package scripts in a target database using custom logging parameters and schema tracking table
111-
.\MyPackage.zip | Register-DBOPackage -SchemaVersionTable dbo.SchemaHistory -OutputFile .\out.log -Append
111+
'.\MyPackage.zip' | Register-DBOPackage -SchemaVersionTable dbo.SchemaHistory -OutputFile .\out.log -Append
112112
113113
.EXAMPLE
114114
# Register package scripts in a target database using custom configuration file
115-
.\MyPackage.zip | Register-DBOPackage -ConfigurationFile .\localconfig.json
115+
'.\MyPackage.zip' | Register-DBOPackage -ConfigurationFile .\localconfig.json
116116
117117
.EXAMPLE
118118
# Register package scripts in a target database using variables instead of specifying values directly
119-
.\MyPackage.zip | Register-DBOPackage -SqlInstance '#{server}' -Database '#{db}' -Variables @{server = 'myserver\instance1'; db = 'MyDb'}
119+
'.\MyPackage.zip' | Register-DBOPackage -SqlInstance '#{server}' -Database '#{db}' -Variables @{server = 'myserver\instance1'; db = 'MyDb'}
120120
#>
121121
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "")]
122122
[CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'Default')]

functions/Reset-DBODefaultSetting.ps1

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@ function Reset-DBODefaultSetting {
22
<#
33
.SYNOPSIS
44
Resets configuration entries back to default values.
5-
5+
66
.DESCRIPTION
77
This function creates or changes configuration values.
88
These can be used to provide dynamic configuration information outside the PowerShell variable system.
9-
9+
1010
.PARAMETER Name
1111
Name of the configuration entry.
1212
1313
.PARAMETER All
1414
Specify if you want to reset all configuration values to their defaults.
1515
16+
.PARAMETER Temporary
17+
The setting is not persisted outside the current session.
18+
By default, settings will be remembered across all powershell sessions.
19+
20+
.PARAMETER Scope
21+
Choose if the setting should be stored in current user's registry or will be shared between all users.
22+
Allowed values: CurrentUser, AllUsers.
23+
AllUsers will require administrative access to the computer (elevated session).
24+
25+
Default: CurrentUser.
26+
1627
.PARAMETER Confirm
1728
Prompts to confirm certain actions
1829
@@ -21,42 +32,51 @@ function Reset-DBODefaultSetting {
2132
2233
.EXAMPLE
2334
Reset-DBODefaultSetting -Name ConnectionTimeout
24-
35+
2536
Reset connection timeout setting back to default value.
26-
37+
2738
.EXAMPLE
2839
Reset-DBODefaultSetting -All
29-
40+
3041
Reset all settings.
3142
#>
3243
[CmdletBinding(DefaultParameterSetName = "Named", SupportsShouldProcess)]
3344
param (
3445
[parameter(Mandatory, ParameterSetName = 'Named')]
3546
[string[]]$Name,
3647
[parameter(Mandatory, ParameterSetName = 'All')]
37-
[switch]$All
48+
[switch]$All,
49+
[switch]$Temporary,
50+
[ValidateSet('CurrentUser', 'AllUsers')]
51+
[string]$Scope = 'CurrentUser'
3852
)
3953

4054
process {
4155
if ($Name) {
56+
$settings = @()
4257
foreach ($n in $Name) {
43-
if ($PSCmdlet.ShouldProcess($Name, "Resetting the setting back to its default value")) {
44-
$config = Get-PSFConfig -FullName dbops.$n
45-
if ($config) { $config.ResetValue() }
46-
else {
47-
Stop-PSFFunction -Message "Unable to find setting $n" -EnableException $true
48-
}
49-
}
58+
$config = Get-PSFConfig -FullName dbops.$n
59+
if ($config) { $settings += $config }
60+
else { Stop-PSFFunction -Message "Unable to find setting $n" -EnableException $true }
5061
}
5162
}
52-
elseif ($All) {
53-
foreach ($config in Get-PSFConfig -Module dbops ) {
54-
if ($PSCmdlet.ShouldProcess($config, "Resetting the setting back to its default value")) {
55-
if ($config.Initialized) {
56-
$config.ResetValue()
57-
}
58-
else {
59-
Write-PSFMessage -Level Warning -Message "Setting $($config.fullName -replace '^dbops\.','')) was not initialized and has no default value as such"
63+
elseif ($All) { $settings = Get-PSFConfig -Module dbops }
64+
$newScope = switch ($Scope) {
65+
'CurrentUser' { 'UserDefault' }
66+
'AllUsers' { 'SystemDefault' }
67+
}
68+
foreach ($config in $settings) {
69+
$sName = $config.fullName -replace '^dbops\.', ''
70+
if ($PSCmdlet.ShouldProcess($config, "Resetting the setting $sName back to its default value")) {
71+
if ($config.Initialized) {
72+
$config.ResetValue()
73+
}
74+
else {
75+
Write-PSFMessage -Level Warning -Message "Setting $sName was not initialized and has no default value as such"
76+
}
77+
if (!$Temporary) {
78+
if ($PSCmdlet.ShouldProcess($Name, "Unregistering $sName in the $newScope scope")) {
79+
$config | Unregister-PSFConfig -Scope $newScope
6080
}
6181
}
6282
}

internal/classes/DBOps.class.ps1

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ class DBOpsPackageBase : DBOps {
165165
[DBOpsConfig]$Configuration
166166
[string]$Version
167167
[System.Version]$ModuleVersion
168+
[bool]$Slim
168169

169170
#Regular file properties
170171
[string]$PSPath
@@ -205,6 +206,7 @@ class DBOpsPackageBase : DBOps {
205206
$this.Configuration = [DBOpsConfig]::new()
206207
$this.Configuration.Parent = $this
207208
$this.PackagePath = ""
209+
$this.Slim = $false
208210
}
209211
[void] Init ([object]$jsonObject) {
210212
$this.Init()
@@ -451,8 +453,20 @@ class DBOpsPackageBase : DBOps {
451453
}
452454

453455
hidden [void] SaveModuleToFile([ZipArchive]$zipArchive) {
454-
foreach ($file in (Get-DBOModuleFileList)) {
455-
[DBOpsHelper]::WriteZipFile($zipArchive, (Join-PSFPath -Normalize "Modules\dbops" $file.Path), [DBOpsHelper]::GetBinaryFile($file.FullName))
456+
if (-not $this.Slim) {
457+
foreach ($file in (Get-DBOModuleFileList)) {
458+
[DBOpsHelper]::WriteZipFile($zipArchive, (Join-PSFPath -Normalize "Modules\dbops" $file.Path), [DBOpsHelper]::GetBinaryFile($file.FullName))
459+
}
460+
# import other modules
461+
$modules = Get-Module dbops | Select-Object -ExpandProperty RequiredModules
462+
foreach ($module in $modules) {
463+
Push-Location $module.ModuleBase
464+
foreach ($file in (Get-ChildItem -Recurse -File)) {
465+
$relativePath = (Resolve-Path -Relative -LiteralPath $file.FullName) -replace '^\.', ''
466+
[DBOpsHelper]::WriteZipFile($zipArchive, (Join-PSFPath -Normalize "Modules\$($module.Name)" $relativePath), [DBOpsHelper]::GetBinaryFile($file.FullName))
467+
}
468+
Pop-Location
469+
}
456470
}
457471
}
458472
#Returns root folder
@@ -970,7 +984,7 @@ class DBOpsFile : DBOps {
970984
[string] GetPackagePath() {
971985
$pPath = $this.PackagePath
972986
# removing odd symbols
973-
$pPath = $pPath -replace ':',''
987+
$pPath = $pPath -replace ':', ''
974988
if ($this.Parent) {
975989
if ($parentPath = $this.Parent.GetPackagePath()) {
976990
$pPath = Join-Path $this.Parent.GetPackagePath() $pPath
@@ -981,7 +995,7 @@ class DBOpsFile : DBOps {
981995
[string] GetDeploymentPath () {
982996
$dPath = $this.PackagePath
983997
# removing odd symbols
984-
$dPath = $dPath -replace ':',''
998+
$dPath = $dPath -replace ':', ''
985999
if ($this.Parent) {
9861000
if ($parentPath = $this.Parent.GetDeploymentPath()) {
9871001
$dPath = Join-Path $this.Parent.GetDeploymentPath() $dPath

tests/New-DBOPackage.Tests.ps1

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ Describe "New-DBOPackage tests" -Tag $commandName, UnitTests {
5757
Join-PSFPath -Normalize Modules\dbops $file.Path | Should BeIn $testResults.Path
5858
}
5959
}
60+
It "should contain external modules" {
61+
$testResults = Get-ArchiveItem $packageName
62+
foreach ($module in Get-Module dbops | Select-Object -ExpandProperty RequiredModules) {
63+
$mName = $module.Name
64+
Join-PSFPath -Normalize Modules "$mName\$mName.psd1" | Should BeIn $testResults.Path
65+
}
66+
}
6067
It "should contain config files" {
6168
$testResults = Get-ArchiveItem $packageName
6269
'dbops.config.json' | Should BeIn $testResults.Path
@@ -91,6 +98,53 @@ Describe "New-DBOPackage tests" -Tag $commandName, UnitTests {
9198
Test-Path $packageName | Should Be $true
9299
}
93100
}
101+
Context "testing slim package contents" {
102+
AfterAll {
103+
if ((Test-Path $workFolder\*) -and $workFolder -like '*.Tests.dbops') { Remove-Item $workFolder\* }
104+
}
105+
It "should create a package file" {
106+
$testResults = New-DBOPackage -ScriptPath "$here\etc\query1.sql" -Name $packageName -Slim -Force
107+
$testResults | Should Not Be $null
108+
$testResults.Name | Should Be (Split-Path $packageName -Leaf)
109+
$testResults.FullName | Should Be (Get-Item $packageName).FullName
110+
$testResults.ModuleVersion | Should Be $null
111+
Test-Path $packageName | Should Be $true
112+
}
113+
It "should contain query files" {
114+
$testResults = Get-ArchiveItem $packageName
115+
'query1.sql' | Should BeIn $testResults.Name
116+
}
117+
It "should not contain module files" {
118+
$testResults = Get-ArchiveItem $packageName
119+
foreach ($file in Get-DBOModuleFileList) {
120+
Join-PSFPath -Normalize Modules\dbops $file.Path | Should -Not -BeIn $testResults.Path
121+
}
122+
}
123+
It "should not contain external modules" {
124+
$testResults = Get-ArchiveItem $packageName
125+
foreach ($module in Get-Module dbops | Select-Object -ExpandProperty RequiredModules) {
126+
$mName = $module.Name
127+
Join-PSFPath -Normalize Modules "$mName\$mName.psd1" | Should -Not -BeIn $testResults.Path
128+
}
129+
}
130+
It "should contain config files" {
131+
$testResults = Get-ArchiveItem $packageName
132+
'dbops.config.json' | Should BeIn $testResults.Path
133+
'dbops.package.json' | Should BeIn $testResults.Path
134+
}
135+
It "should contain deploy files" {
136+
$testResults = Get-ArchiveItem $packageName
137+
'Deploy.ps1' | Should BeIn $testResults.Path
138+
}
139+
It "should create a zip package based on name without extension" {
140+
$testResults = New-DBOPackage -ScriptPath "$here\etc\query1.sql" -Name ($packageName -replace '\.zip$', '') -Force
141+
$testResults | Should Not Be $null
142+
$testResults.Name | Should Be (Split-Path $packageName -Leaf)
143+
$testResults.FullName | Should Be (Get-Item $packageName).FullName
144+
$testResults.ModuleVersion | Should Be (Get-Module dbops).Version
145+
Test-Path $packageName | Should Be $true
146+
}
147+
}
94148
Context "testing configurations" {
95149
BeforeEach {
96150
}

0 commit comments

Comments
 (0)