Skip to content

Commit e7a7ac6

Browse files
authored
Merge pull request #98 from sqlcollaborative/feature/deprecation
Renaming Install-DBOSqlScript to Install-DBOScript
2 parents 329229d + c901a83 commit e7a7ac6

File tree

11 files changed

+198
-89
lines changed

11 files changed

+198
-89
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Example code:
6767

6868
```powershell
6969
# Ad-hoc deployment of the scripts from a folder myscripts
70-
Install-DBOSqlScript -ScriptPath C:\temp\myscripts -SqlInstance server1 -Database MyDB
70+
Install-DBOScript -ScriptPath C:\temp\myscripts -SqlInstance server1 -Database MyDB
7171
7272
# Execute a list of files as an Ad-hoc query
7373
Get-ChildItem C:\temp\myscripts | Invoke-DBOQuery -SqlInstance server1 -Database MyDB

dbops.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
'Get-DBOPackageArtifact',
8181
'Publish-DBOPackageArtifact',
8282
'Copy-DBOPackageArtifact',
83-
'Install-DBOSqlScript',
83+
'Install-DBOScript',
8484
'Register-DBOPackage',
8585
'Send-DBOMailMessage',
8686
'Reset-DBODefaultSetting',

dbops.psm1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,16 @@ if ($typeData) {
199199
Install-DBOSqlScript -ScriptPath $Path -ConnectionString $connectionString
200200
} -ErrorAction Ignore
201201
}
202+
}
203+
204+
# Aliases
205+
206+
$aliases = @(
207+
@{
208+
"AliasName" = "Install-DBOSqlScript"
209+
"Definition" = "Install-DBOScript"
210+
}
211+
)
212+
$aliases | ForEach-Object {
213+
if (-not (Test-Path Alias:$($_.AliasName))) { Set-Alias -Scope Global -Name $($_.AliasName) -Value $($_.Definition) }
202214
}

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Example code:
6262

6363
```powershell
6464
# Ad-hoc deployment of the scripts from a folder myscripts
65-
Install-DBOSqlScript -ScriptPath C:\temp\myscripts -SqlInstance server1 -Database MyDB
65+
Install-DBOScript -ScriptPath C:\temp\myscripts -SqlInstance server1 -Database MyDB
6666
6767
# Execute a list of files as an Ad-hoc query
6868
Get-ChildItem C:\temp\myscripts | Invoke-DBOQuery -SqlInstance server1 -Database MyDB

functions/Install-DBOSqlScript.ps1 renamed to functions/Install-DBOScript.ps1

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function Install-DBOSqlScript {
1+
function Install-DBOScript {
22
<#
33
.SYNOPSIS
44
Deploys genering SQL scripts to a target database
@@ -120,23 +120,23 @@
120120
121121
.EXAMPLE
122122
# Deploys all SQL scripts from the folder .\SqlCode to the target database
123-
Install-DBOSqlScript .\SqlCode\*.sql -SqlInstance 'myserver\instance1' -Database 'MyDb'
123+
Install-DBOScript .\SqlCode\*.sql -SqlInstance 'myserver\instance1' -Database 'MyDb'
124124
125125
.EXAMPLE
126126
# Deploys script file using specific connection parameters
127-
Get-Item .\SqlCode\Script1.sql | Install-DBOSqlScript -SqlInstance 'Srv1' -Database 'MyDb' -ExecutionTimeout 3600
127+
Get-Item .\SqlCode\Script1.sql | Install-DBOScript -SqlInstance 'Srv1' -Database 'MyDb' -ExecutionTimeout 3600
128128
129129
.EXAMPLE
130130
# Deploys all the scripts from the .\SqlCode folder using custom logging parameters and schema tracking table
131-
Get-ChildItem .\SqlCode | Install-DBOSqlScript -SqlInstance 'Srv1' -Database 'MyDb' -SchemaVersionTable dbo.SchemaHistory -OutputFile .\out.log -Append
131+
Get-ChildItem .\SqlCode | Install-DBOScript -SqlInstance 'Srv1' -Database 'MyDb' -SchemaVersionTable dbo.SchemaHistory -OutputFile .\out.log -Append
132132
133133
.EXAMPLE
134134
# Deploys two scripts from the current folder using custom configuration file
135-
Install-DBOSqlScript -Path .\Script1.sql,.\Script2.sql -SqlInstance 'Srv1' -Database 'MyDb' -ConfigurationFile .\localconfig.json
135+
Install-DBOScript -Path .\Script1.sql,.\Script2.sql -SqlInstance 'Srv1' -Database 'MyDb' -ConfigurationFile .\localconfig.json
136136
137137
.EXAMPLE
138138
# Deploys two scripts from the current folder using variables instead of specifying values directly
139-
'.\Script1.sql','.\Script2.sql' | Install-DBOSqlScript -SqlInstance '#{server}' -Database '#{db}' -Variables @{server = 'Srv1'; db = 'MyDb'}
139+
'.\Script1.sql','.\Script2.sql' | Install-DBOScript -SqlInstance '#{server}' -Database '#{db}' -Variables @{server = 'Srv1'; db = 'MyDb'}
140140
#>
141141
# ShouldProcess is handled in the underlying command
142142
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "")]
@@ -210,7 +210,7 @@
210210
$config = New-DBOConfig -Configuration $Configuration
211211

212212
#Merge custom parameters into a configuration
213-
$newConfig = @{}
213+
$newConfig = @{ }
214214
foreach ($key in ($PSBoundParameters.Keys)) {
215215
if ($key -in [DBOpsConfig]::EnumProperties()) {
216216
$newConfig.$key = $PSBoundParameters[$key]
@@ -231,5 +231,8 @@
231231
}
232232
Write-PSFMessage -Level Verbose -Message "Preparing to start the deployment of $($Path.Count) file(s)"
233233
Invoke-DBODeployment @params
234+
235+
# Test name deprecation
236+
Test-AliasDeprecation -DeprecatedOn "1.0.0" -EnableException:$false -Alias Install-DBOSqlScript
234237
}
235238
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
function Test-AliasDeprecation {
2+
<#
3+
.SYNOPSIS
4+
Tests whether a function or one of its parameters was called by a bad name.
5+
Taken from dbatools.io
6+
.DESCRIPTION
7+
Tests whether a function or one of its parameters was called by a bad name.
8+
This allows giving deprecation warnings - once per session - whenever a user uses something we are planning on removing.
9+
For example, when renaming a function, we give a grace period by adding an Alias for that function with its old name.
10+
However, we do not want to carry along this alias forever, so we give warning ahead of time using this function.
11+
When reaching the specified version, we then can safely remove the alias.
12+
Furthermore, this function is used for testing, whether such a removal was properly done.
13+
.PARAMETER DeprecatedOn
14+
The version this parameter or alias will be removed in.
15+
Generally, deprecated parameters and aliases should only be removed on major releases.
16+
.PARAMETER FunctionName
17+
Automatically filled with the calling function.
18+
The name of the function that contains either a deprecated alias or parameter.
19+
.PARAMETER Call
20+
The InvocationInfo of the calling function.
21+
Automatically filled.
22+
.PARAMETER Parameter
23+
The parameter that has become deprecated.
24+
On renamed parameters, keep a parameter-alias. This function will notice, when the alias is used.
25+
.PARAMETER Alias
26+
The alias of the command that will be deprecated.
27+
.PARAMETER CustomMessage
28+
This function will generate a default message. However, this may not always be appropriate.
29+
Use CustomMessage to tailor a response to the necessity of the moment.
30+
.PARAMETER EnableException
31+
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
32+
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
33+
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
34+
.EXAMPLE
35+
PS C:\> Alias-DbaDeprecation -DeprecatedOn "1.0.0.0" -Parameter 'Details'
36+
Will - once per session - complain if the parameter 'Details' is used.
37+
Will cause tests to fail, if it's still in the code after release 1.0.0.0.
38+
.EXAMPLE
39+
PS C:\> Alias-DbaDeprecationn -DeprecatedOn "1.0.0.0" -Alias Copy-SqlDatabase
40+
Will - once per session - complain if the alias 'Copy-SqlDatabase' is used.
41+
Will cause tests to fail, if it's still in the code after release 1.0.0.0.
42+
#>
43+
[CmdletBinding()]
44+
param (
45+
[Parameter(Mandatory)]
46+
[Version]
47+
$DeprecatedOn,
48+
49+
[string]
50+
$FunctionName = (Get-PSCallStack)[0].Command,
51+
52+
[object]
53+
$Call = (Get-PSCallStack)[0].InvocationInfo,
54+
55+
[Parameter(ParameterSetName = "Param", Mandatory)]
56+
[string]
57+
$Parameter,
58+
59+
[Parameter(ParameterSetName = "Alias", Mandatory)]
60+
[string]
61+
$Alias,
62+
63+
[string]
64+
$CustomMessage,
65+
66+
[bool]
67+
$EnableException = $EnableException
68+
)
69+
70+
switch ($PSCmdlet.ParameterSetName) {
71+
"Param" {
72+
$ast = [System.Management.Automation.Language.Parser]::ParseInput($Call.Line, [ref]$null, [ref]$null)
73+
$objects = $ast.FindAll( { $args[0] -is [System.Management.Automation.Language.CommandAst] }, $true)
74+
$sub = $objects | Where-Object Parent -Like "$($Call.InvocationName)*" | Select-Object -First 1
75+
76+
if ($sub.CommandElements | Where-Object ParameterName -eq $Parameter) {
77+
if ($CustomMessage) { $Message = $CustomMessage }
78+
else { $Message = "Using the parameter $Parameter is deprecated. This parameter will be removed in version $DeprecatedOn or before, check in the documentation what parameter to use instead" }
79+
80+
Write-PSFMessage -Message $Message -Level Warning -FunctionName $FunctionName -Once "Deprecated.Alias.$Alias"
81+
}
82+
}
83+
84+
"Alias" {
85+
if ($Alias -eq $Call.InvocationName) {
86+
if ($CustomMessage) { $Message = $CustomMessage }
87+
else { $Message = "Using the alias $Alias is deprecated. This alias will be removed in version $DeprecatedOn or before, use $FunctionName instead." }
88+
89+
Write-PSFMessage -Message $Message -Level Warning -FunctionName $FunctionName -Once "Deprecated.Alias.$Alias"
90+
}
91+
}
92+
}
93+
}

internal/json/dbops.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"functions\\Get-DBOPackageArtifact.ps1",
3737
"functions\\Publish-DBOPackageArtifact.ps1",
3838
"functions\\Copy-DBOPackageArtifact.ps1",
39-
"functions\\Install-DBOSqlScript.ps1",
39+
"functions\\Install-DBOScript.ps1",
4040
"functions\\Register-DBOPackage.ps1",
4141
"functions\\Send-DBOMailMessage.ps1",
4242
"functions\\Reset-DBODefaultSetting.ps1",
@@ -67,6 +67,7 @@
6767
"internal\\functions\\Get-DatabaseConnection.ps1",
6868
"internal\\functions\\Invoke-DBODeployment.ps1",
6969
"internal\\functions\\Install-NugetPackage.ps1",
70+
"internal\\functions\\Test-AliasDeprecation.ps1",
7071
"internal\\classes\\DBOpsHelper.class.ps1",
7172
"internal\\classes\\DBOps.class.ps1",
7273
"internal\\classes\\DBOpsDeploymentStatus.class.ps1",

0 commit comments

Comments
 (0)