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

SqlServerDsc: fix sort in SqlDscPreferredModule to get the latest version #1976

Merged

Conversation

gotit96
Copy link
Contributor

@gotit96 gotit96 commented Oct 24, 2023

Pull Request (PR) description

  • SqlServerDsc
    • Get-SMOModuleCalculatedVersion
      • Return SQLPS version as 12.0 instead of 120
    • Get-SqlDscPreferredModule
      • Fix sort to get the latest version

This Pull Request (PR) fixes the following issues

With multiple version of SqlServer installed:

Version
-------
22.1.1
22.0.59
21.1.18256
21.1.18246

Get-SqlDscPreferredModule was selecting:

Version
-------
21.1.18256

This proposed change update the sort to get the latest version;

Version
-------
22.1.1

Task list

  • Added an entry to the change log under the Unreleased section of the
    file CHANGELOG.md. Entry should say what was changed and how that
    affects users (if applicable), and reference the issue being resolved
    (if applicable).
  • Resource documentation updated in the resource's README.md.
  • Resource parameter descriptions updated in schema.mof.
  • Comment-based help updated, including parameter descriptions.
  • Localization strings updated.
  • Examples updated.
  • Unit tests updated. See DSC Community Testing Guidelines.
  • Integration tests updated (where possible). See DSC Community Testing Guidelines.
  • Code changes adheres to DSC Community Style Guidelines.

This change is Reviewable

@codecov
Copy link

codecov bot commented Oct 24, 2023

Codecov Report

Merging #1976 (845b87c) into main (350c705) will not change coverage.
Report is 2 commits behind head on main.
The diff coverage is 100%.

Impacted file tree graph

@@         Coverage Diff          @@
##           main   #1976   +/-   ##
====================================
  Coverage    92%     92%           
====================================
  Files        93      93           
  Lines      7840    7840           
====================================
  Hits       7214    7214           
  Misses      626     626           
Flag Coverage Δ
unit 92% <100%> (ø)
Files Coverage Δ
source/Private/Get-SMOModuleCalculatedVersion.ps1 100% <100%> (ø)
source/Public/Get-SqlDscPreferredModule.ps1 100% <100%> (ø)

@gotit96 gotit96 marked this pull request as ready for review October 25, 2023 08:56
@gotit96 gotit96 requested review from johlju and a team as code owners October 25, 2023 08:56
@johlju
Copy link
Member

johlju commented Oct 27, 2023

I will try to get to this as soon as possible.

Copy link
Member

@johlju johlju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 4 of 4 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @gotit96)


source/Public/Get-SqlDscPreferredModule.ps1 line 121 at r2 (raw file):

                # Get the latest version if available
                $availableModule = $preferredModules |
                    Sort-Object -Property { ($_.CalculatedVersion -replace '-.+$') -as [version] }, { $_ } -Descending |

Change to the full type name.

Suggestion:

[System.Version]

Copy link
Member

@johlju johlju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @gotit96)


tests/Unit/Public/Get-SqlDscPreferredModule.Tests.ps1 line 392 at r2 (raw file):

            It 'Should return the latest first preferred module' {
                Get-SqlDscPreferredModule -Name @('SqlServer', 'SQLPS') | Should -Be $sqlServerModule2
            }

This change does not seem to return the correct preview versions if several are available. If adding this test it will fail with the proposed, but it will pass when not using the proposed change.

Suggestion:

            BeforeAll {
                $sqlServerModule1 = New-MockObject -Type 'PSModuleInfo' -Properties @{
                    Name = 'SqlServer'
                    Version = [Version]::new(21, 1, 18068)
                }
                $sqlServerModule2 = New-MockObject -Type 'PSModuleInfo' -Properties @{
                    Name = 'SqlServer'
                    Version = [Version]::new(22, 1, 1)
                    PrivateData = @{
                        PSData = @{
                            PreRelease = 'preview1'
                        }
                    }
                }
                $sqlServerModule3 = New-MockObject -Type 'PSModuleInfo' -Properties @{
                    Name = 'SqlServer'
                    Version = [Version]::new(22, 1, 1)
                    PrivateData = @{
                        PSData = @{
                            PreRelease = 'preview2'
                        }
                    }
                }
                $sqlpsModule1 = New-MockObject -Type 'PSModuleInfo' -Properties @{
                    Name = 'SQLPS'
                    Path = 'C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1'
                }
                $sqlpsModule2 = New-MockObject -Type 'PSModuleInfo' -Properties @{
                    Name = 'SQLPS'
                    Path = 'C:\Program Files (x86)\Microsoft SQL Server\160\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1'
                }

                Mock -CommandName Get-Module -MockWith {
                    return @(
                        $sqlServerModule1,
                        $sqlServerModule2,
                        $sqlServerModule3,
                        $sqlpsModule1,
                        $sqlpsModule2
                    )
                }
            }

            It 'Should return the latest first preferred module' {
                Get-SqlDscPreferredModule -Name @('SqlServer', 'SQLPS') | Should -Be $sqlServerModule3
            }

@johlju johlju added the waiting for code fix A review left open comments, and the pull request is waiting for changes to be pushed by the author. label Oct 29, 2023
@gotit96
Copy link
Contributor Author

gotit96 commented Oct 30, 2023

Reviewed 4 of 4 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @gotit96)

source/Public/Get-SqlDscPreferredModule.ps1 line 121 at r2 (raw file):

                # Get the latest version if available
                $availableModule = $preferredModules |
                    Sort-Object -Property { ($_.CalculatedVersion -replace '-.+$') -as [version] }, { $_ } -Descending |

Change to the full type name.

Suggestion:

[System.Version]

fixed

@gotit96 gotit96 closed this Oct 30, 2023
@gotit96 gotit96 reopened this Oct 30, 2023
@gotit96
Copy link
Contributor Author

gotit96 commented Oct 30, 2023

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @gotit96)

tests/Unit/Public/Get-SqlDscPreferredModule.Tests.ps1 line 392 at r2 (raw file):

            It 'Should return the latest first preferred module' {
                Get-SqlDscPreferredModule -Name @('SqlServer', 'SQLPS') | Should -Be $sqlServerModule2
            }

This change does not seem to return the correct preview versions if several are available. If adding this test it will fail with the proposed, but it will pass when not using the proposed change.

Suggestion:

            BeforeAll {
                $sqlServerModule1 = New-MockObject -Type 'PSModuleInfo' -Properties @{
                    Name = 'SqlServer'
                    Version = [Version]::new(21, 1, 18068)
                }
                $sqlServerModule2 = New-MockObject -Type 'PSModuleInfo' -Properties @{
                    Name = 'SqlServer'
                    Version = [Version]::new(22, 1, 1)
                    PrivateData = @{
                        PSData = @{
                            PreRelease = 'preview1'
                        }
                    }
                }
                $sqlServerModule3 = New-MockObject -Type 'PSModuleInfo' -Properties @{
                    Name = 'SqlServer'
                    Version = [Version]::new(22, 1, 1)
                    PrivateData = @{
                        PSData = @{
                            PreRelease = 'preview2'
                        }
                    }
                }
                $sqlpsModule1 = New-MockObject -Type 'PSModuleInfo' -Properties @{
                    Name = 'SQLPS'
                    Path = 'C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1'
                }
                $sqlpsModule2 = New-MockObject -Type 'PSModuleInfo' -Properties @{
                    Name = 'SQLPS'
                    Path = 'C:\Program Files (x86)\Microsoft SQL Server\160\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1'
                }

                Mock -CommandName Get-Module -MockWith {
                    return @(
                        $sqlServerModule1,
                        $sqlServerModule2,
                        $sqlServerModule3,
                        $sqlpsModule1,
                        $sqlpsModule2
                    )
                }
            }

            It 'Should return the latest first preferred module' {
                Get-SqlDscPreferredModule -Name @('SqlServer', 'SQLPS') | Should -Be $sqlServerModule3
            }

Test added, and sort of preview version fixed.

digging deeper into the test, it seems that powershell does not allow the installation of multiple prerelease/releases of the same version:

> Install-Module -Name dbatools -RequiredVersion 2.0.0-preview2 -AllowPrerelease
> Get-Module -Name dbatools -ListAvailable | Select-Object -Property Name,Version,{$_.PrivateData.PSData.Prerelease}

Name     Version $_.PrivateData.PSData.Prerelease
----     ------- --------------------------------
dbatools 2.0.0   preview2
dbatools 1.1.146

> Install-Module -Name dbatools -RequiredVersion 2.0.0-preview5 -AllowPrerelease
> Get-Module -Name dbatools -ListAvailable | Select-Object -Property Name,Version,{$_.PrivateData.PSData.Prerelease}

Name     Version $_.PrivateData.PSData.Prerelease
----     ------- --------------------------------
dbatools 2.0.0   preview5
dbatools 1.1.146

> Install-Module -Name dbatools -RequiredVersion 2.0.0
WARNING : Version '2.0.0' of module 'dbatools' is already installed at '...\dbatools\2.0.0'. To reinstall this version '2.0.0', run Install-Module or
Updated-Module cmdlet with the -Force parameter.

> Install-Module -Name dbatools -RequiredVersion 2.0.0 -Force
> Get-Module -Name dbatools -ListAvailable | Select-Object -Property Name,Version,{$_.PrivateData.PSData.Prerelease}

Name     Version $_.PrivateData.PSData.Prerelease
----     ------- --------------------------------
dbatools 2.0.0
dbatools 1.1.146

@johlju johlju added needs review The pull request needs a code review. and removed waiting for code fix A review left open comments, and the pull request is waiting for changes to be pushed by the author. labels Nov 1, 2023
@johlju
Copy link
Member

johlju commented Nov 1, 2023

Yes, but you can have modules installed in different folders that are part of PSModulePath.

PS> Get-Module -Name dbatools -ListAvailable | Select-Object -Property Name,Version,{$_.PrivateData.PSData.Prerelease}

Name     Version $_.PrivateData.PSData.Prerelease
----     ------- --------------------------------
dbatools 2.0.0   preview2
dbatools 2.0.0   preview5

Copy link
Member

@johlju johlju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 2 of 2 files at r3, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @gotit96)

@johlju johlju merged commit 85f8523 into dsccommunity:main Nov 2, 2023
36 checks passed
@johlju johlju removed the needs review The pull request needs a code review. label Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants