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

SqlDatabase: Get-TargetResource returns a collation even when the database does not exist #1395

Closed
johlju opened this issue Jul 15, 2019 · 0 comments · Fixed by #1391
Closed
Labels
bug The issue is a bug.

Comments

@johlju
Copy link
Member

johlju commented Jul 15, 2019

Details of the scenario you tried and the problem that is occurring

Get-TargetResource returns a collation even when the database does not exist. This is becuase in Get-TargetResource the collation is always first set to the collation of the instance.

https://github.com/PowerShell/SqlServerDsc/blob/2d37a930d957d2bbe148d118a5065a2f6f33d29e/DSCResources/MSFT_SqlDatabase/MSFT_SqlDatabase.psm1#L68-L79

Verbose logs showing the problem

The integration tests shows this problem.

    Context When using configuration MSFT_SqlDatabase_RemoveDatabase2_Config
The 'Microsoft.PowerShell.Management' module was not imported because the 'Microsoft.PowerShell.Management' snap-in was already imported.
VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredStateConfiguration'.
VERBOSE: An LCM method call arrived from computer APPVYR-WIN with user sid S-1-5-21-1930758936-441951309-2831684283-1000.
VERBOSE: [APPVYR-WIN]: LCM:  [ Start  Set      ]
VERBOSE: [APPVYR-WIN]: LCM:  [ Start  Resource ]  [[SqlDatabase]Integration_Test]
VERBOSE: [APPVYR-WIN]: LCM:  [ Start  Test     ]  [[SqlDatabase]Integration_Test]
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Determines the state of the database 'Database2' on the instance 'DSCSQLTEST'.
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Get the state of the database 'Database2' on the instance 'DSCSQLTEST'.
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Information: PowerShell module SqlServer not found, trying to use older SQLPS module. (SQLCOMMON0024)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Importing PowerShell module 'SQLPS' with version '1.0' from path 'C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\SQLPS\SQLPS.psd1'. (SQLCOMMON0025)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Connecting using the credential 'APPVYR-WIN\SqlAdmin' and the login type 'Integrated'. (SQLCOMMON0054)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Connected to SQL instance 'APPVYR-WIN\DSCSQLTEST'. (SQLCOMMON0018)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] There is a database 'Database2' present, and has the collation 'SQL_Latin1_General_Pref_CP850_CI_AS'.
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Expected the database 'Database2' to absent, but it was present.
VERBOSE: [APPVYR-WIN]: LCM:  [ End    Test     ]  [[SqlDatabase]Integration_Test]  in 2.5650 seconds.
VERBOSE: [APPVYR-WIN]: LCM:  [ Start  Set      ]  [[SqlDatabase]Integration_Test]
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Found PowerShell module Sqlps already imported in the session. (SQLCOMMON0026)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Connecting using the credential 'APPVYR-WIN\SqlAdmin' and the login type 'Integrated'. (SQLCOMMON0054)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Connected to SQL instance 'APPVYR-WIN\DSCSQLTEST'. (SQLCOMMON0018)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Removing the database 'Database2'.
VERBOSE: [APPVYR-WIN]: LCM:  [ End    Set      ]  [[SqlDatabase]Integration_Test]  in 0.3090 seconds.
VERBOSE: [APPVYR-WIN]: LCM:  [ End    Resource ]  [[SqlDatabase]Integration_Test]
VERBOSE: [APPVYR-WIN]: LCM:  [ End    Set      ]
VERBOSE: [APPVYR-WIN]: LCM:  [ End    Set      ]    in  3.0410 seconds.
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 3.081 seconds
      [+] Should compile and apply the MOF without throwing 3.54s
VERBOSE: An LCM method call arrived from computer APPVYR-WIN with user sid S-1-5-21-1930758936-441951309-2831684283-1000.
VERBOSE: [APPVYR-WIN]: LCM:  [ Start  Get      ]      
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Get the state of the database 'Database2' on the instance 'DSCSQLTEST'.
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Information: PowerShell module SqlServer not found, trying to use older SQLPS module. (SQLCOMMON0024)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Importing PowerShell module 'SQLPS' with version '1.0' from path 'C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\SQLPS\SQLPS.psd1'. (SQLCOMMON0025)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Connecting using the credential 'APPVYR-WIN\SqlAdmin' and the login type 'Integrated'. (SQLCOMMON0054)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Connected to SQL instance 'APPVYR-WIN\DSCSQLTEST'. (SQLCOMMON0018)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] It does not exist a database named 'Database2'.
VERBOSE: [APPVYR-WIN]: LCM:  [ End    Get      ]  [[SqlDatabase]Integration_Test]  in 2.8280 seconds.
VERBOSE: [APPVYR-WIN]: LCM:  [ End    Get      ]    in  2.9830 seconds.
      [+] Should be able to call Get-DscConfiguration without throwing 3.03s
      [-] Should have set the resource and all the parameters should match 286ms
        Expected $null or empty, but got Finnish_Swedish_CI_AS.
        185:                 $resourceCurrentState.Collation | Should -BeNullOrEmpty
        at <ScriptBlock>, C:\projects\sqlserverdsc\Tests\Integration\MSFT_SqlDatabase.Integration.Tests.ps1: line 185
VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = TestConfiguration,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredStateConfiguration'.
VERBOSE: An LCM method call arrived from computer APPVYR-WIN with user sid S-1-5-21-1930758936-441951309-2831684283-1000.
VERBOSE: [APPVYR-WIN]: LCM:  [ Start  Test     ]
VERBOSE: [APPVYR-WIN]: LCM:  [ Start  Resource ]  [[SqlDatabase]Integration_Test]
VERBOSE: [APPVYR-WIN]: LCM:  [ Start  Test     ]  [[SqlDatabase]Integration_Test]
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Determines the state of the database 'Database2' on the instance 'DSCSQLTEST'.
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Get the state of the database 'Database2' on the instance 'DSCSQLTEST'.
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Information: PowerShell module SqlServer not found, trying to use older SQLPS module. (SQLCOMMON0024)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Importing PowerShell module 'SQLPS' with version '1.0' from path 'C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\SQLPS\SQLPS.psd1'. (SQLCOMMON0025)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Connecting using the credential 'APPVYR-WIN\SqlAdmin' and the login type 'Integrated'. (SQLCOMMON0054)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] Connected to SQL instance 'APPVYR-WIN\DSCSQLTEST'. (SQLCOMMON0018)
VERBOSE: [APPVYR-WIN]:                            [[SqlDatabase]Integration_Test] It does not exist a database named 'Database2'.
VERBOSE: [APPVYR-WIN]: LCM:  [ End    Test     ]  [[SqlDatabase]Integration_Test] True in 2.8080 seconds.
VERBOSE: [APPVYR-WIN]: LCM:  [ End    Resource ]  [[SqlDatabase]Integration_Test]
VERBOSE: [APPVYR-WIN]: LCM:  [ End    Test     ]     Completed processing test operation. The operation returned True.
VERBOSE: [APPVYR-WIN]: LCM:  [ End    Test     ]    in  2.9620 seconds.
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 3 seconds
      [+] Should return $true when Test-DscConfiguration is run 3.01s

Suggested solution to the issue

Remove the line

https://github.com/PowerShell/SqlServerDsc/blob/2d37a930d957d2bbe148d118a5065a2f6f33d29e/DSCResources/MSFT_SqlDatabase/MSFT_SqlDatabase.psm1#L71

The DSC configuration that is used to reproduce the issue (as detailed as possible)

Configuration MSFT_SqlDatabase_RemoveDatabaseUser4_Config
{
    Import-DscResource -ModuleName 'SqlServerDsc'

    node $AllNodes.NodeName
    {
        SqlDatabaseUser 'Integration_Test'
        {
            Ensure       = 'Absent'
            ServerName   = $Node.ServerName
            InstanceName = $Node.InstanceName
            DatabaseName = $Node.DatabaseName
            Name         = $Node.User4_Name

            PsDscRunAsCredential = New-Object `
                -TypeName System.Management.Automation.PSCredential `
                -ArgumentList @($Node.Username, (ConvertTo-SecureString -String $Node.Password -AsPlainText -Force))
        }
    }
}

SQL Server edition and version the target node is running

n/a

SQL Server PowerShell modules present on the target node

n/a

The operating system the target node is running

n/a

Version and build of PowerShell the target node is running

n/a

Version of the DSC module that was used ('dev' if using current dev branch)

dev

@johlju johlju added bug The issue is a bug. in progress The issue is being actively worked on by someone. labels Jul 15, 2019
johlju added a commit to pshamus/SqlServerDsc that referenced this issue Jul 15, 2019
johlju pushed a commit that referenced this issue Jul 17, 2019
- Changes to SqlServerDsc
  - New DSC resource SqlDatabaseUser (issue #846).
    - Adds ability to create database users with more fine-grained control,
      e.g. re-mapping of orphaned logins or a different login. Supports
      creating a user with or without login name, and database users mapped
      to a certificate or asymmetric key.
  - Minor style fixes in unit tests.
- Changes to SqlDatabase
  - Get-TargetResource now correctly return `$null` for the collation property
    when the database does not exist (issue #1395).
  - No longer enforces the collation property if the Collation parameter
    is not part of the configuration (issue #1396).
  - Updated resource description in README.md
  - Fix examples to use `PsDscRunAsCredential` (issue #760).
  - Added integration tests (issue #739).
  - Updated unit tests to the latest template (issue #1068).
@johlju johlju removed the in progress The issue is being actively worked on by someone. label Jul 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant