Skip to content

Commit e943b91

Browse files
authored
Add locking and deletion support (#16)
Adds locking and deletion support to tables
1 parent 04aa9b2 commit e943b91

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

GetMsSqlDump.ps1

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
Instructs the dump file to commit all lines at once. May speed up processing time. Ignored if -format is not provided.
4242
.PARAMETER condense
4343
Condense multiple INSERT INTO statements into single statements. Significant performance boost; debugging becomes difficult.
44+
.PARAMETER lock
45+
Adds table lock instructions to the dump file
46+
.PARAMETER delete
47+
Use with caution. Adds a "DELETE FROM <table>;" to the beginning of the dump file.
4448
.PARAMETER debug
4549
Prints debug information for troubleshooting and debugging purposes
4650
.PARAMETER version
@@ -52,10 +56,9 @@
5256
.OUTPUTS
5357
stdout unless -file is provided.
5458
.NOTES
55-
Version: 0.4.1
59+
Version: 0.4.2
5660
Author: Bitemo, Erik Gergely, Tres Finocchiaro
5761
Creation Date: 2018
58-
Purpose/Change: Updated for compatiblity with SQL Server 2016
5962
License: Microsoft Reciprocal License (MS-RL)
6063
.EXAMPLE
6164
.\GetMsSqlDump.ps1 -server SQL01 -db WideWorldImporters -table Sales.Customers -file ~\Sales.Customer.sql -overwrite -noidentity
@@ -79,6 +82,8 @@ Param(
7982
[switch]$pointfromtext = $false,
8083
[switch]$noautocommit = $false,
8184
[switch]$condense = $false,
85+
[switch]$lock = $false,
86+
[switch]$delete = $false,
8287
[switch]$debug = $false,
8388
[switch]$version = $false,
8489
[switch]$help = $false
@@ -325,11 +330,37 @@ foreach ($obj in $tables) {
325330
WriteLine "-- Table $obj / scripted at $(Get-Date) on server $server, database $db" $file
326331
WriteLine "" $file
327332

328-
# Handle deferred commits, can improve performance
329-
if ($noautocommit) {
330-
if ($format -eq "mysql") { WriteLine "SET autocommit=0;" $file }
331-
elseif ($format -eq "mssql") { WriteLine "SET IMPLICIT_TRANSACTIONS ON;" $file }
332-
else { Write-Warning "Flag `$noautocommit was specified without `$format. Ignoring." }
333+
if (!$format -and $noautocommit) {
334+
Write-Warning "Flag '`$noautocommit $noautocommit' was provided without specifying `$format. Ignoring."
335+
}
336+
if (!$format -and $lock) {
337+
Write-Warning "Flag '`$lock $lock' was provided without specifying `$format. Ignoring."
338+
}
339+
340+
# Handle platform-specific statements
341+
$insertfooter = ""
342+
if ($format -eq "mysql") {
343+
# Handle deferred commits, improves performance
344+
if ($noautocommit) {
345+
WriteLine "SET autocommit=0;" $file
346+
}
347+
# Handle database locks for integrity
348+
if ($lock) {
349+
WriteLine "LOCK TABLES $obj WRITE;" $file
350+
}
351+
} elseif ($format -eq "mssql") {
352+
if ($noautocommit) {
353+
WriteLine "SET IMPLICIT_TRANSACTIONS ON;" $file
354+
}
355+
# Handle database locks for integrity
356+
if ($lock) {
357+
$insertfooter = " WITH (TABLOCKX)"
358+
}
359+
}
360+
361+
# Handle delete flag
362+
if ($delete) {
363+
WriteLine "DELETE from $obj;" $file
333364
}
334365

335366
# First part of the insert statements
@@ -357,7 +388,7 @@ foreach ($obj in $tables) {
357388
Debug " $($column.ColumnName): $($column.DataType)"
358389
}
359390
$insertheader = $insertheader -replace ", $", ") VALUES("
360-
$terminator = ";"
391+
$terminator = "$insertfooter;"
361392

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

385416
# Each condensed block must end with a semicolon ";"
386417
if ($linecount % $condensemax -eq 0 -or $linecount -eq $rows) {
387-
$terminator = ";"
418+
$terminator = "$insertfooter;"
388419
} else {
389420
$terminator = ","
390421
}
@@ -417,10 +448,22 @@ foreach ($obj in $tables) {
417448
$linebuffer.Clear() | Out-Null
418449
$linecount = 0
419450

420-
# Handle deferred commits, can improve performance
421-
if ($noautocommit) {
422-
if ($format -eq "mysql") { WriteLine "COMMIT;" $file }
423-
elseif ($format -eq "mssql") { WriteLine "COMMIT TRANSACTION;" $file }
451+
# Handle platform-specific statements
452+
if ($format -eq "mysql") {
453+
# Handle database locks
454+
if ($lock) {
455+
WriteLine "UNLOCK TABLES;" $file
456+
}
457+
# Handle deferred commits
458+
if ($noautocommit) {
459+
WriteLine "COMMIT;" $file
460+
}
461+
} elseif ($format -eq "mssql") {
462+
# Handle deferred commits
463+
if ($noautocommit) {
464+
WriteLine "COMMIT TRANSACTION;" $file
465+
}
466+
# Locks are automatically released in MSSQL
424467
}
425468
}
426469
# Drop the dataset

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
.\GetMsSqlDump.ps1 [-server servername] [-db dbname]
1111
-table tablename [-query "customquery"] [-username username -password password]
1212
[-file filename] [-dateformat dateformat] [-format "mysql|mssql" -noautocommit]
13-
[-condence] [-append] [-noidentity] [-debug] [-help] [-?]
13+
[-condense] [-lock] [-delete] [-append|-overwrite] [-noidentity] [-debug] [-help] [-?]
1414
```
1515

1616
## Description
@@ -48,6 +48,8 @@ All switched **must** be prefixed with a single hyphen. e.g. `-append -overwrit
4848
| `pointfromtext` | Use PointFromText attempts to convert `SqlGeography` `POINT(x y)` values using `PointFromText('POINT(x y)')` WKT (well-known-text) conversion |
4949
| `noautocommit` | Instructs the dump file to commit all lines at once. May speed up processing time. Ignored if `-format` is not provided. |
5050
| `condense` | Condense multiple INSERT INTO statements into single statements. Significant performance boost; debugging becomes difficult. |
51+
| `lock` | Add table lock instructions to dump file. Recommended to combine with `-noautocommit`. | N/A |
52+
| `delete` | **Use with caution.** Adds a `DELETE FROM <table>;` to the beginning of the dump file. | N/A |
5153
| `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. |
5254
| `version` | Prints the version information and exits. |
5355
| `help` or `?` | Prints this short help. Ignores all other parameters. |

0 commit comments

Comments
 (0)