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

New-DbaDbUser: Refactor and added contained user creation #8827

Merged
merged 11 commits into from
Apr 1, 2023
Merged

New-DbaDbUser: Refactor and added contained user creation #8827

merged 11 commits into from
Apr 1, 2023

Conversation

sqlarticles
Copy link
Contributor

@sqlarticles sqlarticles commented Mar 28, 2023

Type of Change

  • Bug fix (non-breaking change, fixes # )
  • New feature (non-breaking change, adds functionality, fixes New-DbaDbUser - Contained user #8815
  • Breaking change (affects multiple commands or functionality, fixes # )
  • Ran manual Pester test and has passed (.\tests\manual.pester.ps1)
  • Adding code coverage to existing functionality
  • Pester test is included
  • If new file reference added for test, has is been added to github.com/dataplat/appveyor-lab ?
  • Unit test is included
  • Documentation
  • Build system

Purpose

  • Refactored the code a bit to use dbatools public functions.
  • Added parameter sets based on the type of user
  • Added functionality to create contained users
  • Added functionality to return an warning when the given database doesn't exists
  • Removed deprecated SqlLogin user type
  • Updated integration test

Approach

Included a new parameter (Password) to the function and also used parameter sets. Depends on which parameter the user choose then action will be taken. In this case if they use Password parameter then it will create a SQL user with password in the database. SQL User with password is supported only in contained database and from SQL 2012 onwards. This check is not done , if the database is not enabled partial containment then at the time of execution a error will be thrown.

Commands to test

New-DbaDbUser -SqlInstance sqlserver2016 -Database DB1 -Username user1 -Password (ConvertTo-SecureString -String "DBATools" -AsPlainText)

@sqlarticles sqlarticles marked this pull request as ready for review March 28, 2023 14:45
@sqlarticles sqlarticles marked this pull request as draft March 28, 2023 15:00
@wsmelton
Copy link
Member

Please use the template provided for the pull request when it deals with modifying code. It helps in review of the PR.

public/New-DbaDbUser.ps1 Outdated Show resolved Hide resolved
}

#Set appropriate user type
#Removed SQLLogin user type. This is deprecated and the alternate is to map to SqlUser. Reference https://learn.microsoft.com/en-us/dotnet/api/microsoft.sqlserver.management.smo.usertype?view=sql-smo-160
Copy link
Member

Choose a reason for hiding this comment

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

Your link does not show that is deprecated. This will still work on lower versions of SQL Server?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is mentioned in the fields section. https://learn.microsoft.com/en-us/dotnet/api/microsoft.sqlserver.management.smo.usertype?view=sql-smo-160#fields

SqlLogin 0 Specifies a SQLLogin user. This is deprecated, use SQLUser instead.

I might be wrong. I thought the smo library is part of dbatools or dbatools.library module as a dependency which should be the recent version. If this is the case then I will prefer to keep the changes. However, if the module uses smo library installed locally then it makes sense to revert it back. I was not clear, your inputs will be helpful.

if ($ExcludeDatabase) {
$databases = $databases | Where-Object Name -NotIn $ExcludeDatabase
}
$databases = Get-DbaDatabase @getDbParam
Copy link
Member

Choose a reason for hiding this comment

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

We tend to not use this in our commands internally because of performance impact. I would prefer this to be reverted to the original pattern as that is what is followed in most all of our commands.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I didnt get it. You meant the hash table as parameters or using the Get-DbaDatabase function?

Copy link
Member

@wsmelton wsmelton Mar 31, 2023

Choose a reason for hiding this comment

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

My request is reverting the change back to the original pattern. Get-DbaDatabase incurs performance on many systems and use of that in any command can cause degradation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha. Before making this change I raised in the Slack channel and I was told there isn't any standard and suggested to use functions if there is one already exists. That's when I refactored to use the functions instead.

I'm happy to revert the changes to use SMO connection instead of the function. Since this PR is merged, do you guys want me to revert it and raise a new PR?

Also, this is my understanding. These are operational functions and not a real time application so it should be fine even if it add few ms to the execution. I know this module is used in wide scope so I dont want to mess up with the expectation. Like I said before, let me know if I need to revert it and create a new PR?

@wsmelton
Copy link
Member

wsmelton commented Mar 28, 2023

The test failures are related to our tests use New-DbaDbUser so those need to be addressed.

@potatoqualitee
Copy link
Member

hey cool! thank you for the PR. I plan to return to dbatools this weekend and will take a closer look as well.

@sqlarticles
Copy link
Contributor Author

The test failures are related to our tests use New-DbaDbUser so those need to be addressed.

Fixed the failing tests and all the tests are successful now.

@sqlarticles sqlarticles reopened this Mar 30, 2023
@sqlarticles
Copy link
Contributor Author

Please use the template provided for the pull request when it deals with modifying code. It helps in review of the PR.

Thanks for this, updated the description using the template. My first PR with dbatools so please bear with me till I get used to it.

@sqlarticles sqlarticles marked this pull request as ready for review March 30, 2023 12:03
public/New-DbaDbUser.ps1 Outdated Show resolved Hide resolved
public/New-DbaDbUser.ps1 Outdated Show resolved Hide resolved
public/New-DbaDbUser.ps1 Outdated Show resolved Hide resolved
$connParam = @{ }
if ($SqlCredential) { $connParam.SqlCredential = $SqlCredential }

# is this required?
Copy link
Member

Choose a reason for hiding this comment

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

Please remove comment, it is required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

shame! I kept that comment. Apologies.

potatoqualitee and others added 3 commits April 1, 2023 10:08
Co-authored-by: Shawn Melton <11204251+wsmelton@users.noreply.github.com>
Co-authored-by: Shawn Melton <11204251+wsmelton@users.noreply.github.com>
Co-authored-by: Shawn Melton <11204251+wsmelton@users.noreply.github.com>
@potatoqualitee
Copy link
Member

Thank you for the proposed changes, @wsmelton 🙇🏼 I have merged them.

@potatoqualitee
Copy link
Member

sweet, the merges ultimately resulted in a pass.

@potatoqualitee potatoqualitee merged commit 728fca1 into dataplat:development Apr 1, 2023
@sqlarticles sqlarticles deleted the ContainedUser#8815 branch April 1, 2023 22:46
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.

New-DbaDbUser - Contained user
3 participants