Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ allcommands.ps1
.aider/.aider.input.history
.aider/aider.chat.history.md
.aider/aider.input.history
.aider/aider.llm.history
.aider/aider.llm.history
7 changes: 5 additions & 2 deletions private/functions/Convert-DbaMaskingValue.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ function Convert-DbaMaskingValue {
} else {
switch ($DataType.ToLower()) {
{ $_ -in 'bit', 'bool' } {
if ($item -match "([0-1])") {

if ($item -match "^[01]$") {
$newValue = "$item"
} elseif ($item -eq "true") {
$newValue = "1"
} elseif ($item -eq "false") {
$newValue = "0"
} else {
$errorMessage = "Value '$($item)' is not valid BIT or BOOL"
}
Expand Down
9 changes: 4 additions & 5 deletions public/Invoke-DbaDbDataMasking.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ function Invoke-DbaDbDataMasking {
Format = $columnobject.Format
Locale = $Locale
}
} elseif ($columnobject.SubType.ToLowerInvariant() -eq 'shuffle') {
} elseif ($columnobject.SubType.ToLowerInvariant() -in 'shuffle', 'string2', 'string') {
if ($columnobject.ColumnType -in 'bigint', 'char', 'int', 'nchar', 'nvarchar', 'smallint', 'tinyint', 'varchar') {
$newValueParams = @{
RandomizerType = "Random"
Expand Down Expand Up @@ -957,11 +957,10 @@ function Invoke-DbaDbDataMasking {

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

if ($convertedValue.ErrorMessage) {
$maskingErrorFlag = $true
Expand Down
24 changes: 18 additions & 6 deletions tests/Invoke-DbaDbDataMasking.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,24 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
$sql = "CREATE TABLE [dbo].[people](
[fname] [varchar](50) NULL,
[lname] [varchar](50) NULL,
[dob] [datetime] NULL
[dob] [datetime] NULL,
[percenttest] [decimal](15,3) NULL,
[bittest] bit NULL
) ON [PRIMARY]
GO
INSERT INTO people (fname, lname, dob) VALUES ('Joe','Schmoe','2/2/2000')
INSERT INTO people (fname, lname, dob) VALUES ('Jane','Schmee','2/2/1950')
INSERT INTO people (fname, lname, dob, percenttest,bittest) VALUES ('Joe','Schmoe','2/2/2000',29.53,1)
INSERT INTO people (fname, lname, dob, percenttest,bittest) VALUES ('Jane','Schmee','2/2/1950',65.38,0)
GO
CREATE TABLE [dbo].[people2](
[fname] [varchar](50) NULL,
[lname] [varchar](50) NULL,
[dob] [datetime] NULL
[dob] [datetime] NULL,
[percenttest] [decimal](15,3) NULL,
[bittest] bit NULL
) ON [PRIMARY]
GO
INSERT INTO people2 (fname, lname, dob) VALUES ('Layla','Schmoe','2/2/2000')
INSERT INTO people2 (fname, lname, dob) VALUES ('Eric','Schmee','2/2/1950')"
INSERT INTO people2 (fname, lname, dob, percenttest,bittest) VALUES ('Layla','Schmoe','2/2/2000',29.53,1)
INSERT INTO people2 (fname, lname, dob, percenttest,bittest) VALUES ('Eric','Schmee','2/2/1950',65.38,0)"
New-DbaDatabase -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Name $db
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query $sql
}
Expand All @@ -66,6 +70,10 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
It "starts with the right data" {
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where fname = 'Joe'" | Should -Not -Be $null
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where lname = 'Schmee'" | Should -Not -Be $null
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where percenttest = 29.53" | Should -Not -Be $null
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where percenttest = 65.38" | Should -Not -Be $null
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where bittest = 1 AND lname = 'Schmoe'" | Should -Not -Be $null
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where bittest = 0 AND lname = 'Schmee'" | Should -Not -Be $null
}
It "returns the proper output" {
$file = New-DbaDbMaskingConfig -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Path C:\temp
Expand All @@ -83,6 +91,10 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people" | Should -Not -Be $null
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where fname = 'Joe'" | Should -Be $null
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where lname = 'Schmee'" | Should -Be $null
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where percenttest = 29.53" | Should -Be $null
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where percenttest = 65.38" | Should -Be $null
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where bittest = 1 AND lname = 'Schmoe'" | Should -Be $null
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -SqlCredential $TestConfig.SqlCredential -Database $db -Query "select * from people where bittest = 0 AND lname = 'Schmee'" | Should -Be $null
}
}
}