Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke-DbaDbDataGenerator / Get-DbaRandomizedValue - Fix precision for datatime datatype #9016

Merged
merged 2 commits into from
Jun 23, 2023
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
27 changes: 17 additions & 10 deletions public/Get-DbaRandomizedValue.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function Get-DbaRandomizedValue {

if (Test-FunctionInterrupt) { return }

if ($DataType) {
if ($DataType -and -not $RandomizerSubType) {

switch ($DataType.ToLowerInvariant()) {
'bigint' {
Expand Down Expand Up @@ -346,6 +346,14 @@ function Get-DbaRandomizedValue {
$script:faker.Database.$RandomizerSubType()
}
'date' {
if ($DataType -eq 'date') {
$formatString = "yyyy-MM-dd"
} elseif ($DataType -eq 'datetime') {
$formatString = "yyyy-MM-dd HH:mm:ss.fff"
} elseif ($DataType -eq 'datetime2') {
$formatString = "yyyy-MM-dd HH:mm:ss.fffffff"
}

if ($randSubType -eq 'between') {

if (-not $Min) {
Expand All @@ -359,7 +367,7 @@ function Get-DbaRandomizedValue {
if ($Min -gt $Max) {
Stop-Function -Message "The minimum value for the date cannot be later than maximum value" -Continue -Target $Min
} else {
($script:faker.Date.Between($Min, $Max)).ToString("yyyy-MM-dd HH:mm:ss.fffffff", [System.Globalization.CultureInfo]::InvariantCulture)
($script:faker.Date.Between($Min, $Max)).ToString($formatString, [System.Globalization.CultureInfo]::InvariantCulture)
}
} elseif ($randSubType -eq 'past') {
if ($Max) {
Expand All @@ -369,9 +377,9 @@ function Get-DbaRandomizedValue {
$yearsToGoBack = 1
}

$script:faker.Date.Past($yearsToGoBack, $Max).ToString("yyyy-MM-dd HH:mm:ss.fffffff", [System.Globalization.CultureInfo]::InvariantCulture)
$script:faker.Date.Past($yearsToGoBack, $Max).ToString($formatString, [System.Globalization.CultureInfo]::InvariantCulture)
} else {
$script:faker.Date.Past().ToString("yyyy-MM-dd HH:mm:ss.fffffff", [System.Globalization.CultureInfo]::InvariantCulture)
$script:faker.Date.Past().ToString($formatString, [System.Globalization.CultureInfo]::InvariantCulture)
}
} elseif ($randSubType -eq 'future') {
if ($Min) {
Expand All @@ -381,13 +389,12 @@ function Get-DbaRandomizedValue {
$yearsToGoForward = 1
}

$script:faker.Date.Future($yearsToGoForward, $Min).ToString("yyyy-MM-dd HH:mm:ss.fffffff", [System.Globalization.CultureInfo]::InvariantCulture)
$script:faker.Date.Future($yearsToGoForward, $Min).ToString($formatString, [System.Globalization.CultureInfo]::InvariantCulture)
} else {
$script:faker.Date.Future().ToString("yyyy-MM-dd HH:mm:ss.fffffff", [System.Globalization.CultureInfo]::InvariantCulture)
$script:faker.Date.Future().ToString($formatString, [System.Globalization.CultureInfo]::InvariantCulture)
}

} elseif ($randSubType -eq 'recent') {
$script:faker.Date.Recent().ToString("yyyy-MM-dd HH:mm:ss.fffffff", [System.Globalization.CultureInfo]::InvariantCulture)
$script:faker.Date.Recent().ToString($formatString, [System.Globalization.CultureInfo]::InvariantCulture)
} elseif ($randSubType -eq 'random') {
if ($Min -or $Max) {
if (-not $Min) {
Expand All @@ -398,9 +405,9 @@ function Get-DbaRandomizedValue {
$Max = (Get-Date).AddYears(1)
}

($script:faker.Date.Between($Min, $Max)).ToString("yyyy-MM-dd HH:mm:ss.fffffff", [System.Globalization.CultureInfo]::InvariantCulture)
($script:faker.Date.Between($Min, $Max)).ToString($formatString, [System.Globalization.CultureInfo]::InvariantCulture)
} else {
($script:faker.Date.Past()).ToString("yyyy-MM-dd HH:mm:ss.fffffff", [System.Globalization.CultureInfo]::InvariantCulture)
($script:faker.Date.Past()).ToString($formatString, [System.Globalization.CultureInfo]::InvariantCulture)
}
} else {
$script:faker.Date.$RandomizerSubType()
Expand Down
19 changes: 17 additions & 2 deletions public/Invoke-DbaDbDataGenerator.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,24 @@ function Invoke-DbaDbDataGenerator {
$columnobject.MaxValue = $MaxValue
}
if ($columnobject.ColumnType -in $supportedDataTypes -and $columnobject.MaskingType -eq 'Random' -and $columnobject.SubType -in 'Bool', 'Number', 'Float', 'Byte', 'String') {
$columnValue = Get-DbaRandomizedValue -DataType $columnobject.ColumnType -CharacterString $charstring -Locale $Locale -Min $columnobject.MinValue -Max $columnobject.MaxValue
$randomParams = @{
DataType = $columnobject.ColumnType
CharacterString = $charstring
Locale = $Locale
Min = $columnobject.MinValue
Max = $columnobject.MaxValue
}
$columnValue = Get-DbaRandomizedValue @randomParams
} else {
$columnValue = Get-DbaRandomizedValue -RandomizerType $columnobject.MaskingType -RandomizerSubtype $columnobject.SubType -CharacterString $charstring -Locale $Locale -Min $columnobject.MinValue -Max $columnobject.MaxValue
$randomParams = @{
RandomizerType = $columnobject.MaskingType
RandomizerSubtype = $columnobject.SubType
CharacterString = $charstring
Locale = $Locale
Min = $columnobject.MinValue
Max = $columnobject.MaxValue
}
$columnValue = Get-DbaRandomizedValue @randomParams
}

} catch {
Expand Down