Skip to content

Commit

Permalink
Add locking and deletion support (#16)
Browse files Browse the repository at this point in the history
Adds locking and deletion support to tables
  • Loading branch information
tresf authored Jul 31, 2018
1 parent 04aa9b2 commit e943b91
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
69 changes: 56 additions & 13 deletions GetMsSqlDump.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
Instructs the dump file to commit all lines at once. May speed up processing time. Ignored if -format is not provided.
.PARAMETER condense
Condense multiple INSERT INTO statements into single statements. Significant performance boost; debugging becomes difficult.
.PARAMETER lock
Adds table lock instructions to the dump file
.PARAMETER delete
Use with caution. Adds a "DELETE FROM <table>;" to the beginning of the dump file.
.PARAMETER debug
Prints debug information for troubleshooting and debugging purposes
.PARAMETER version
Expand All @@ -52,10 +56,9 @@
.OUTPUTS
stdout unless -file is provided.
.NOTES
Version: 0.4.1
Version: 0.4.2
Author: Bitemo, Erik Gergely, Tres Finocchiaro
Creation Date: 2018
Purpose/Change: Updated for compatiblity with SQL Server 2016
License: Microsoft Reciprocal License (MS-RL)
.EXAMPLE
.\GetMsSqlDump.ps1 -server SQL01 -db WideWorldImporters -table Sales.Customers -file ~\Sales.Customer.sql -overwrite -noidentity
Expand All @@ -79,6 +82,8 @@ Param(
[switch]$pointfromtext = $false,
[switch]$noautocommit = $false,
[switch]$condense = $false,
[switch]$lock = $false,
[switch]$delete = $false,
[switch]$debug = $false,
[switch]$version = $false,
[switch]$help = $false
Expand Down Expand Up @@ -325,11 +330,37 @@ foreach ($obj in $tables) {
WriteLine "-- Table $obj / scripted at $(Get-Date) on server $server, database $db" $file
WriteLine "" $file

# Handle deferred commits, can improve performance
if ($noautocommit) {
if ($format -eq "mysql") { WriteLine "SET autocommit=0;" $file }
elseif ($format -eq "mssql") { WriteLine "SET IMPLICIT_TRANSACTIONS ON;" $file }
else { Write-Warning "Flag `$noautocommit was specified without `$format. Ignoring." }
if (!$format -and $noautocommit) {
Write-Warning "Flag '`$noautocommit $noautocommit' was provided without specifying `$format. Ignoring."
}
if (!$format -and $lock) {
Write-Warning "Flag '`$lock $lock' was provided without specifying `$format. Ignoring."
}

# Handle platform-specific statements
$insertfooter = ""
if ($format -eq "mysql") {
# Handle deferred commits, improves performance
if ($noautocommit) {
WriteLine "SET autocommit=0;" $file
}
# Handle database locks for integrity
if ($lock) {
WriteLine "LOCK TABLES $obj WRITE;" $file
}
} elseif ($format -eq "mssql") {
if ($noautocommit) {
WriteLine "SET IMPLICIT_TRANSACTIONS ON;" $file
}
# Handle database locks for integrity
if ($lock) {
$insertfooter = " WITH (TABLOCKX)"
}
}

# Handle delete flag
if ($delete) {
WriteLine "DELETE from $obj;" $file
}

# First part of the insert statements
Expand Down Expand Up @@ -357,7 +388,7 @@ foreach ($obj in $tables) {
Debug " $($column.ColumnName): $($column.DataType)"
}
$insertheader = $insertheader -replace ", $", ") VALUES("
$terminator = ";"
$terminator = "$insertfooter;"

$linebuffer = New-Object System.Text.StringBuilder
$linecount = 0
Expand All @@ -384,7 +415,7 @@ foreach ($obj in $tables) {

# Each condensed block must end with a semicolon ";"
if ($linecount % $condensemax -eq 0 -or $linecount -eq $rows) {
$terminator = ";"
$terminator = "$insertfooter;"
} else {
$terminator = ","
}
Expand Down Expand Up @@ -417,10 +448,22 @@ foreach ($obj in $tables) {
$linebuffer.Clear() | Out-Null
$linecount = 0

# Handle deferred commits, can improve performance
if ($noautocommit) {
if ($format -eq "mysql") { WriteLine "COMMIT;" $file }
elseif ($format -eq "mssql") { WriteLine "COMMIT TRANSACTION;" $file }
# Handle platform-specific statements
if ($format -eq "mysql") {
# Handle database locks
if ($lock) {
WriteLine "UNLOCK TABLES;" $file
}
# Handle deferred commits
if ($noautocommit) {
WriteLine "COMMIT;" $file
}
} elseif ($format -eq "mssql") {
# Handle deferred commits
if ($noautocommit) {
WriteLine "COMMIT TRANSACTION;" $file
}
# Locks are automatically released in MSSQL
}
}
# Drop the dataset
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.\GetMsSqlDump.ps1 [-server servername] [-db dbname]
-table tablename [-query "customquery"] [-username username -password password]
[-file filename] [-dateformat dateformat] [-format "mysql|mssql" -noautocommit]
[-condence] [-append] [-noidentity] [-debug] [-help] [-?]
[-condense] [-lock] [-delete] [-append|-overwrite] [-noidentity] [-debug] [-help] [-?]
```

## Description
Expand Down Expand Up @@ -48,6 +48,8 @@ All switched **must** be prefixed with a single hyphen. e.g. `-append -overwrit
| `pointfromtext` | Use PointFromText attempts to convert `SqlGeography` `POINT(x y)` values using `PointFromText('POINT(x y)')` WKT (well-known-text) conversion |
| `noautocommit` | Instructs the dump file to commit all lines at once. May speed up processing time. Ignored if `-format` is not provided. |
| `condense` | Condense multiple INSERT INTO statements into single statements. Significant performance boost; debugging becomes difficult. |
| `lock` | Add table lock instructions to dump file. Recommended to combine with `-noautocommit`. | N/A |
| `delete` | **Use with caution.** Adds a `DELETE FROM <table>;` to the beginning of the dump file. | N/A |
| `debug` | Prints way more characters to your screen than you'd like to. If something didn't work in the way you expected, or you want to submit a bug, run your statement with the debug switch. |
| `version` | Prints the version information and exits. |
| `help` or `?` | Prints this short help. Ignores all other parameters. |
Expand Down

0 comments on commit e943b91

Please sign in to comment.