Skip to content

Commit 4e8aebf

Browse files
authored
Invoke-DbaDbDataMasking - fix up decimal & bit support (#9664)
1 parent 93c5fef commit 4e8aebf

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ allcommands.ps1
5151
.aider/.aider.input.history
5252
.aider/aider.chat.history.md
5353
.aider/aider.input.history
54-
.aider/aider.llm.history
54+
.aider/aider.llm.history

private/functions/Convert-DbaMaskingValue.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,12 @@ function Convert-DbaMaskingValue {
111111
} else {
112112
switch ($DataType.ToLower()) {
113113
{ $_ -in 'bit', 'bool' } {
114-
if ($item -match "([0-1])") {
115-
114+
if ($item -match "^[01]$") {
116115
$newValue = "$item"
116+
} elseif ($item -eq "true") {
117+
$newValue = "1"
118+
} elseif ($item -eq "false") {
119+
$newValue = "0"
117120
} else {
118121
$errorMessage = "Value '$($item)' is not valid BIT or BOOL"
119122
}

public/Invoke-DbaDbDataMasking.ps1

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ function Invoke-DbaDbDataMasking {
917917
Format = $columnobject.Format
918918
Locale = $Locale
919919
}
920-
} elseif ($columnobject.SubType.ToLowerInvariant() -eq 'shuffle') {
920+
} elseif ($columnobject.SubType.ToLowerInvariant() -in 'shuffle', 'string2', 'string') {
921921
if ($columnobject.ColumnType -in 'bigint', 'char', 'int', 'nchar', 'nvarchar', 'smallint', 'tinyint', 'varchar') {
922922
$newValueParams = @{
923923
RandomizerType = "Random"
@@ -957,11 +957,10 @@ function Invoke-DbaDbDataMasking {
957957

958958
# Convert the values so they can used in T-SQL
959959
try {
960-
if ($row.($columnobject.Name) -eq '') {
961-
$convertedValue = Convert-DbaMaskingValue -Value ' ' -DataType $columnobject.ColumnType -Nullable:$columnobject.Nullable -EnableException
962-
} else {
963-
$convertedValue = Convert-DbaMaskingValue -Value $newValue -DataType $columnobject.ColumnType -Nullable:$columnobject.Nullable -EnableException
960+
if ($row.($columnobject.Name) -eq '' -and $columnobject.ColumnType -in 'decimal') {
961+
$newvalue = "0.00"
964962
}
963+
$convertedValue = Convert-DbaMaskingValue -Value $newValue -DataType $columnobject.ColumnType -Nullable:$columnobject.Nullable -EnableException
965964

966965
if ($convertedValue.ErrorMessage) {
967966
$maskingErrorFlag = $true

tests/Invoke-DbaDbDataMasking.Tests.ps1

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,24 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
3939
$sql = "CREATE TABLE [dbo].[people](
4040
[fname] [varchar](50) NULL,
4141
[lname] [varchar](50) NULL,
42-
[dob] [datetime] NULL
42+
[dob] [datetime] NULL,
43+
[percenttest] [decimal](15,3) NULL,
44+
[bittest] bit NULL
4345
) ON [PRIMARY]
4446
GO
45-
INSERT INTO people (fname, lname, dob) VALUES ('Joe','Schmoe','2/2/2000')
46-
INSERT INTO people (fname, lname, dob) VALUES ('Jane','Schmee','2/2/1950')
47+
INSERT INTO people (fname, lname, dob, percenttest,bittest) VALUES ('Joe','Schmoe','2/2/2000',29.53,1)
48+
INSERT INTO people (fname, lname, dob, percenttest,bittest) VALUES ('Jane','Schmee','2/2/1950',65.38,0)
4749
GO
4850
CREATE TABLE [dbo].[people2](
4951
[fname] [varchar](50) NULL,
5052
[lname] [varchar](50) NULL,
51-
[dob] [datetime] NULL
53+
[dob] [datetime] NULL,
54+
[percenttest] [decimal](15,3) NULL,
55+
[bittest] bit NULL
5256
) ON [PRIMARY]
5357
GO
54-
INSERT INTO people2 (fname, lname, dob) VALUES ('Layla','Schmoe','2/2/2000')
55-
INSERT INTO people2 (fname, lname, dob) VALUES ('Eric','Schmee','2/2/1950')"
58+
INSERT INTO people2 (fname, lname, dob, percenttest,bittest) VALUES ('Layla','Schmoe','2/2/2000',29.53,1)
59+
INSERT INTO people2 (fname, lname, dob, percenttest,bittest) VALUES ('Eric','Schmee','2/2/1950',65.38,0)"
5660
New-DbaDatabase -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Name $db
5761
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query $sql
5862
}
@@ -66,6 +70,10 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
6670
It "starts with the right data" {
6771
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where fname = 'Joe'" | Should -Not -Be $null
6872
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where lname = 'Schmee'" | Should -Not -Be $null
73+
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where percenttest = 29.53" | Should -Not -Be $null
74+
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where percenttest = 65.38" | Should -Not -Be $null
75+
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where bittest = 1 AND lname = 'Schmoe'" | Should -Not -Be $null
76+
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where bittest = 0 AND lname = 'Schmee'" | Should -Not -Be $null
6977
}
7078
It "returns the proper output" {
7179
$file = New-DbaDbMaskingConfig -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Path C:\temp
@@ -83,6 +91,10 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
8391
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people" | Should -Not -Be $null
8492
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where fname = 'Joe'" | Should -Be $null
8593
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where lname = 'Schmee'" | Should -Be $null
94+
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where percenttest = 29.53" | Should -Be $null
95+
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where percenttest = 65.38" | Should -Be $null
96+
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where bittest = 1 AND lname = 'Schmoe'" | Should -Be $null
97+
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where bittest = 0 AND lname = 'Schmee'" | Should -Be $null
8698
}
8799
}
88100
}

0 commit comments

Comments
 (0)