-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[24.1] Add username and email deduplication scripts #18492
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import os | ||
import sys | ||
|
||
from sqlalchemy import create_engine | ||
|
||
sys.path.insert( | ||
1, os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir, os.pardir, "lib")) | ||
) | ||
|
||
from galaxy.model.orm.scripts import get_config | ||
from galaxy.model.scripts.user_deduplicator import UserDeduplicator | ||
|
||
DESCRIPTION = "Deduplicate user emails in galaxy_user table" | ||
|
||
|
||
def main(): | ||
config = get_config(sys.argv, use_argparse=False, cwd=os.getcwd()) | ||
engine = create_engine(config["db_url"]) | ||
ud = UserDeduplicator(engine=engine) | ||
ud.deduplicate_emails() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import os | ||
import sys | ||
|
||
from sqlalchemy import create_engine | ||
|
||
sys.path.insert( | ||
1, os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir, os.pardir, "lib")) | ||
) | ||
|
||
from galaxy.model.orm.scripts import get_config | ||
from galaxy.model.scripts.user_deduplicator import UserDeduplicator | ||
|
||
DESCRIPTION = "Deduplicate usernames in galaxy_user table" | ||
|
||
|
||
def main(): | ||
config = get_config(sys.argv, use_argparse=False, cwd=os.getcwd()) | ||
engine = create_engine(config["db_url"]) | ||
ud = UserDeduplicator(engine=engine) | ||
ud.deduplicate_usernames() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from sqlalchemy import ( | ||
func, | ||
select, | ||
update, | ||
) | ||
from sqlalchemy.engine import ( | ||
Connection, | ||
Result, | ||
) | ||
|
||
from galaxy.managers.users import generate_next_available_username_with_connection | ||
from galaxy.model import User | ||
|
||
|
||
class UserDeduplicator: | ||
|
||
def __init__(self, engine): | ||
self.engine = engine | ||
|
||
def deduplicate_emails(self) -> None: | ||
with self.engine.begin() as connection: | ||
prev_email = None | ||
for id, email, _ in self._get_duplicate_email_data(): | ||
if email == prev_email: | ||
new_email = self._generate_replacement_for_duplicate_email(connection, email) | ||
stmt = update(User).where(User.id == id).values(email=new_email) | ||
connection.execute(stmt) | ||
else: | ||
prev_email = email | ||
|
||
def deduplicate_usernames(self) -> None: | ||
with self.engine.begin() as connection: | ||
prev_username = None | ||
for id, username, _ in self._get_duplicate_username_data(): | ||
if username == prev_username: | ||
new_username = generate_next_available_username_with_connection( | ||
connection, username, model_class=User | ||
) | ||
stmt = update(User).where(User.id == id).values(username=new_username) | ||
connection.execute(stmt) | ||
else: | ||
prev_username = username | ||
|
||
def _get_duplicate_username_data(self) -> Result: | ||
counts = select(User.username, func.count()).group_by(User.username).having(func.count() > 1) | ||
sq = select(User.username).select_from(counts.cte()) | ||
stmt = ( | ||
select(User.id, User.username, User.create_time) | ||
.where(User.username.in_(sq)) | ||
.order_by(User.username, User.create_time.desc()) | ||
) | ||
|
||
with self.engine.connect() as conn: | ||
duplicates = conn.execute(stmt) | ||
return duplicates | ||
|
||
def _get_duplicate_email_data(self) -> Result: | ||
counts = select(User.email, func.count()).group_by(User.email).having(func.count() > 1) | ||
sq = select(User.email).select_from(counts.cte()) | ||
stmt = ( | ||
select(User.id, User.email, User.create_time) | ||
.where(User.email.in_(sq)) | ||
.order_by(User.email, User.create_time.desc()) | ||
) | ||
|
||
with self.engine.connect() as conn: | ||
duplicates = conn.execute(stmt) | ||
return duplicates | ||
|
||
def _generate_replacement_for_duplicate_email(self, connection: Connection, email: str) -> str: | ||
""" | ||
Generate a replacement for a duplicate email value. The new value consists of the original email | ||
and a suffix. This value cannot be used as an email, but since the original email is part of | ||
the new value, it will be possible to retrieve the user record based on this value, if needed. | ||
This is used to remove duplicate email values from the database, for the rare case the database | ||
contains such values. | ||
""" | ||
i = 1 | ||
while connection.execute(select(User).where(User.email == f"{email}-{i}")).first(): | ||
i += 1 | ||
return f"{email}-{i}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't do this, users won't control the new email address.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as deduplication goes, there is not much else we can do: there should be no duplicate emails in the database, and if there are, we can either delete the duplicate records or edit the duplicate values. Another option is to expect admins to do this manually (deleting rows, contacting users, whatever), and add this to the admin release notes. This script modifies the emails on the older duplicate accounts, leaving the newest intact, which of course is not ideal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At a minimum prefix/suffix the email address to something not guessable ? Yes, if there are duplicates it's better to check which account actually has data, it's not unlikely only one of them will have anything at all, since we probably pick one on login, or we've always been denying / failing at the login stage, at which point toggling the delete flag is sufficient.