Skip to content

Commit 570d072

Browse files
committed
Slim mode
1 parent 3c343a5 commit 570d072

File tree

4 files changed

+93
-12
lines changed

4 files changed

+93
-12
lines changed

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/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) {

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)