Skip to content

Commit

Permalink
Fix: Add comment section
Browse files Browse the repository at this point in the history
  • Loading branch information
Splaxi committed May 14, 2024
1 parent e784004 commit 0f4d748
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 35 deletions.
129 changes: 127 additions & 2 deletions d365fo.tools/functions/repair-d365bacpacmodel.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,115 @@
function Repair-D365BacpacModel {
<#
.SYNOPSIS
Repair a bacpac model file
.DESCRIPTION
As the backend of the Azure SQL infrastructure keeps evovling, the bacpac file can contain invalid instructions while we are trying to import into a local SQL Server installation on a Tier1 environment
.PARAMETER Path
Path to the bacpac model file that you want to work against
.PARAMETER OutputPath
Path to where the repaired model file should be placed
.PARAMETER PathRepairSimple
Path to the json file, that contains all the instructions to be executed in the "Simple" section
Simple means, that we can remove complex elements, based on some basic logic. E.g.
{
"Search": "*<Element Type=\"SqlPermissionStatement\"*ms_db_configreader*",
"End": "*</Element>*"
}
"*<Element Type=\"SqlPermissionStatement\"*ms_db_configreader*" can identify below, and together with "*</Element>*" - we know when to stop.
<Element Type="SqlPermissionStatement" Name="[Grant.Delete.Object].[ms_db_configreader].[dbo].[dbo].[AutotuneBase]">
<Property Name="Permission" Value="4" />
<Relationship Name="Grantee">
<Entry>
<References Name="[ms_db_configreader]" />
</Entry>
</Relationship>
<Relationship Name="Grantor">
<Entry>
<References ExternalSource="BuiltIns" Name="[dbo]" />
</Entry>
</Relationship>
<Relationship Name="SecuredObject">
<Entry>
<References Name="[dbo].[AutotuneBase]" />
</Entry>
</Relationship>
</Element>
.PARAMETER PathRepairQualifier
Path to the json file, that contains all the instructions to be executed in the "Qualifier" section
Qualifier means, that we can remove complex elements, based on some basic logic. E.g.
{
"Search": "*<Element Type=\"SqlRoleMembership\">*",
"Qualifier": "*<References Name=*ms_db_configwriter*",
"End": "*</Element>*"
}
"*<Element Type=\"SqlRoleMembership\">*" can identify below, "*<References Name=*ms_db_configwriter*" qualifies that we are locating the correct one and together with "*</Element>*" - we know when to stop.
<Element Type="SqlRoleMembership">
<Relationship Name="Member">
<Entry>
<References Name="[ms_db_configwriter]" />
</Entry>
</Relationship>
<Relationship Name="Role">
<Entry>
<References ExternalSource="BuiltIns" Name="[db_ddladmin]" />
</Entry>
</Relationship>
</Element>
.PARAMETER PathRepairReplace
Path to the json file, that contains all the instructions to be executed in the "Replace" section
Replace means, that we can replace/remove strings, based on some basic logic. E.g.
{
"Search": "<Property Name=\"AutoDrop\" Value=\"True\" />",
"Replace": ""
}
"<Property Name=\"AutoDrop\" Value=\"True\" />" can identify below, and "" is the value we want to replace with it.
<Property Name="AutoDrop" Value="True" />
.PARAMETER KeepFiles
Instruct the cmdlet to keep the files from the repair process
The files are very large, so only use this as a way to analyze why your model file didn't end up in the desired state
Use it while you evolve/develop your instructions, but remove it from ANY full automation scripts
.EXAMPLE
An example
.NOTES
Author: Mötz Jensen (@Splaxi)
Json files has to be an array directly in the root of the file. All " (double quotes) has to be escaped with \" - otherwise it will not work as intended.
This cmdlet is inspired by the work of "Brad Bateman" (github: @batetech)
His github profile can be found here:
https://github.com/batetech
Florian Hopfner did a gist implementation, and did all the inital work in terms of finding the fastest way to work against the model file
The original gist is: https://gist.github.com/FH-Inway/f485c720b43b72bffaca5fb6c094707e
His github profile can be found here:
https://github.com/FH-Inway
#>
function Repair-D365BacpacModel {
[CmdletBinding()]
param (
[string] $Path,
Expand All @@ -9,7 +120,9 @@

[string] $PathRepairQualifier = "$script:ModuleRoot\internal\misc\RepairBacpac.Qualifier.json",

[string] $PathRepairReplace = "$script:ModuleRoot\internal\misc\RepairBacpac.Replace.json"
[string] $PathRepairReplace = "$script:ModuleRoot\internal\misc\RepairBacpac.Replace.json",

[switch] $KeepFiles
)

# Load all the simple delete instructions
Expand Down Expand Up @@ -46,6 +159,10 @@
# Clone input file to the local temporary file
Copy-Item -Path $forOutput -Destination $localInput -Force

if (-not $KeepFiles) {
Get-ChildItem -Path "$($directoryObj.FullName)\*.simple.*.xml" | Remove-Item -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
}

for ($i = 0; $i -lt $arrQualifier.Count; $i++) {
$forInput = Join-Path -Path $directoryObj.FullName -ChildPath "$i.qualifier.input.xml"
$forOutput = Join-Path -Path $directoryObj.FullName -ChildPath "$i.qualifier.output.xml"
Expand All @@ -68,6 +185,10 @@
# Clone input file to the local temporary file
Copy-Item -Path $forOutput -Destination $localInput -Force

if (-not $KeepFiles) {
Get-ChildItem -Path "$($directoryObj.FullName)\*.qualifier.*.xml" | Remove-Item -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
}

for ($i = 0; $i -lt $arrReplace.Count; $i++) {
$forInput = Join-Path -Path $directoryObj.FullName -ChildPath "$i.replace.input.xml"
$forOutput = Join-Path -Path $directoryObj.FullName -ChildPath "$i.replace.output.xml"
Expand All @@ -79,4 +200,8 @@
}

Copy-Item -Path $forOutput -Destination $OutputPath -Force

if (-not $KeepFiles) {
Get-ChildItem -Path "$($directoryObj.FullName)\*.replace.*.xml" | Remove-Item -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
}
}
61 changes: 49 additions & 12 deletions d365fo.tools/internal/functions/repair-bacpacmodelqualifier.ps1
Original file line number Diff line number Diff line change
@@ -1,34 +1,71 @@
<#
."C:\GIT\GITHUB\d365fo.tools.Workspace\d365fo.tools\d365fo.tools\internal\functions\repair-bacpacmodelqualifier.ps1"
.SYNOPSIS
Short description
Repair a bacpac model file - using qualification logic
.DESCRIPTION
Long description
Will use a search pattern, qualification and end pattern, to remove an element from the model file
.PARAMETER Path
Parameter description
Path to the bacpac model file that you want to work against
.PARAMETER OutputPath
Parameter description
Path to where the repaired model file should be placed
.PARAMETER Search
Parameter description
Search pattern that is used to start the removable of the element
Supports wildcard - as it utilizes the -Like operation that is available directly in powershell
E.g. "*<Element Type=\"SqlRoleMembership\">*"
.PARAMETER Qualifier
Parameter description
Qualifier pattern that is used to qualify the element, based on a nested line value
Supports wildcard - as it utilizes the -Like operation that is available directly in powershell
E.g. "*<References Name=*ms_db_configwriter*"
.PARAMETER End
Parameter description
End pattern that is used to conclude the removable of the element
Supports wildcard - as it utilizes the -Like operation that is available directly in powershell
E.g. "*</Element>*"
.EXAMPLE
An example
PS C:\> Repair-BacpacModelQualifier -Path c:\temp\model.xml -OutputPath c:\temp\repaired_model.xml -Search "*<Element Type=\"SqlRoleMembership\">*" -Qualifier "*<References Name=*ms_db_configwriter*" -End "*</Element>*"
This will remove the below section from the model file
<Element Type="SqlRoleMembership">
<Relationship Name="Member">
<Entry>
<References Name="[ms_db_configwriter]" />
</Entry>
</Relationship>
<Relationship Name="Role">
<Entry>
<References ExternalSource="BuiltIns" Name="[db_ddladmin]" />
</Entry>
</Relationship>
</Element>
.NOTES
General notes
Author: Mötz Jensen (@Splaxi)
Json files has to be an array directly in the root of the file. All " (double quotes) has to be escaped with \" - otherwise it will not work as intended.
This cmdlet is inspired by the work of "Brad Bateman" (github: @batetech)
His github profile can be found here:
https://github.com/batetech
Florian Hopfner did a gist implementation, and did all the inital work in terms of finding the fastest way to work against the model file
The original gist is: https://gist.github.com/FH-Inway/f485c720b43b72bffaca5fb6c094707e
Repair-BacpacModelQualifier -Path "C:\Temp\INOX\Bacpac\Base.xml" -OutputPath "C:\Temp\INOX\Bacpac\Working.xml" -Search '*<Element Type="SqlRoleMembership">*' -Qualifier '*<References Name=*ms_db_configreader*' -End '*</Element>*'
Repair-BacpacModelQualifier -Path "C:\Temp\INOX\Bacpac\Base.xml" -OutputPath "C:\Temp\INOX\Bacpac\Working.xml" -Search '*<Element Type="SqlRoleMembership">*' -Qualifier '*<References Name=*ms_db_configwriter*' -End '*</Element>*'
His github profile can be found here:
https://github.com/FH-Inway
#>
function Repair-BacpacModelQualifier {
[CmdletBinding()]
Expand Down
39 changes: 29 additions & 10 deletions d365fo.tools/internal/functions/repair-bacpacmodelreplace.ps1
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
<#
."C:\GIT\GITHUB\d365fo.tools.Workspace\d365fo.tools\d365fo.tools\internal\functions\repair-bacpacmodelreplace.ps1"
.SYNOPSIS
Short description
Repair a bacpac model file - using replace logic
.DESCRIPTION
Long description
Will use a search value, to replace an text value from the model file
.PARAMETER Path
Parameter description
Path to the bacpac model file that you want to work against
.PARAMETER OutputPath
Parameter description
Path to where the repaired model file should be placed
.PARAMETER Search
Parameter description
Search pattern that is used to replace the value
Works directly on the value entered, not wildcard or regex is supported at all
E.g. "<Property Name=\"AutoDrop\" Value=\"True\" />"
.PARAMETER Replace
Parameter description
Replace value that you want to substitute your search value with
.EXAMPLE
An example
PS C:\> Repair-BacpacModelReplace -Path c:\temp\model.xml -OutputPath c:\temp\repaired_model.xml -Search "<Property Name=\"AutoDrop\" Value=\"True\" />" -Replace ""
This will replace the below section from the model file
<Property Name="AutoDrop" Value="True" />
.NOTES
General notes
Author: Mötz Jensen (@Splaxi)
Json files has to be an array directly in the root of the file. All " (double quotes) has to be escaped with \" - otherwise it will not work as intended.
This cmdlet is inspired by the work of "Brad Bateman" (github: @batetech)
His github profile can be found here:
https://github.com/batetech
Florian Hopfner did a gist implementation, and did all the inital work in terms of finding the fastest way to work against the model file
The original gist is: https://gist.github.com/FH-Inway/f485c720b43b72bffaca5fb6c094707e
Repair-BacpacModelReplace -Path "C:\Temp\INOX\Bacpac\Base.xml" -OutputPath "C:\Temp\INOX\Bacpac\Working.xml" -Search '<Property Name="IsArithAbortOn" Value="True" />' -Replace ''
His github profile can be found here:
https://github.com/FH-Inway
#>
function Repair-BacpacModelReplace {
[CmdletBinding()]
Expand Down
60 changes: 49 additions & 11 deletions d365fo.tools/internal/functions/repair-bacpacmodelsimpleremove.ps1
Original file line number Diff line number Diff line change
@@ -1,32 +1,70 @@
<#
."C:\GIT\GITHUB\d365fo.tools.Workspace\d365fo.tools\d365fo.tools\internal\functions\repair-bacpacmodelsimpleremove.ps1"
.SYNOPSIS
Short description
Repair a bacpac model file - using simple logic
.DESCRIPTION
Long description
Will use a search pattern, and end pattern, to remove an element from the model file
.PARAMETER Path
Parameter description
Path to the bacpac model file that you want to work against
.PARAMETER OutputPath
Parameter description
Path to where the repaired model file should be placed
.PARAMETER Search
Parameter description
Search pattern that is used to start the removable of the element
Supports wildcard - as it utilizes the -Like operation that is available directly in powershell
E.g. "*<Element Type=\"SqlPermissionStatement\"*ms_db_configreader*"
.PARAMETER End
Parameter description
End pattern that is used to conclude the removable of the element
Supports wildcard - as it utilizes the -Like operation that is available directly in powershell
E.g. "*</Element>*"
.EXAMPLE
An example
PS C:\> Repair-BacpacModelSimpleRemove -Path c:\temp\model.xml -OutputPath c:\temp\repaired_model.xml -Search "*<Element Type=\"SqlPermissionStatement\"*ms_db_configreader*" -End "*</Element>*"
This will remove the below section from the model file
<Element Type="SqlPermissionStatement" Name="[Grant.Delete.Object].[ms_db_configreader].[dbo].[dbo].[AutotuneBase]">
<Property Name="Permission" Value="4" />
<Relationship Name="Grantee">
<Entry>
<References Name="[ms_db_configreader]" />
</Entry>
</Relationship>
<Relationship Name="Grantor">
<Entry>
<References ExternalSource="BuiltIns" Name="[dbo]" />
</Entry>
</Relationship>
<Relationship Name="SecuredObject">
<Entry>
<References Name="[dbo].[AutotuneBase]" />
</Entry>
</Relationship>
</Element>
.NOTES
General notes
Author: Mötz Jensen (@Splaxi)
Json files has to be an array directly in the root of the file. All " (double quotes) has to be escaped with \" - otherwise it will not work as intended.
This cmdlet is inspired by the work of "Brad Bateman" (github: @batetech)
His github profile can be found here:
https://github.com/batetech
Florian Hopfner did a gist implementation, and did all the inital work in terms of finding the fastest way to work against the model file
Repair-BacpacModelSimpleRemove -Path "C:\Temp\INOX\Bacpac\Base.xml" -OutputPath "C:\Temp\INOX\Bacpac\Working.xml" -Search '*<Element Type="SqlPermissionStatement"*ms_db_configreader*' -End '*</Element>*'
Repair-BacpacModelSimpleRemove -Path "C:\Temp\INOX\Bacpac\Base.xml" -OutputPath "C:\Temp\INOX\Bacpac\Working.xml" -Search '*<Element Type="SqlPermissionStatement"*ms_db_configwriter*' -End '*</Element>*'
The original gist is: https://gist.github.com/FH-Inway/f485c720b43b72bffaca5fb6c094707e
His github profile can be found here:
https://github.com/FH-Inway
#>
function Repair-BacpacModelSimpleRemove {
[CmdletBinding()]
Expand Down

0 comments on commit 0f4d748

Please sign in to comment.