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

[24.1] Add username and email deduplication scripts #18492

Closed
Closed
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
7 changes: 6 additions & 1 deletion lib/galaxy/managers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,8 +908,13 @@ def username_exists(session, username: str, model_class=User):


def generate_next_available_username(session, username, model_class=User):
"""Generate unique username; user can change it later"""
return generate_next_available_username_with_connection(session.connection(), username, model_class)


def generate_next_available_username_with_connection(connection, username, model_class=User):
"""Generate unique username; user can change it later"""
i = 1
while session.execute(select(model_class).where(model_class.username == f"{username}-{i}")).first():
while connection.execute(select(model_class).where(model_class.username == f"{username}-{i}")).first():
i += 1
return f"{username}-{i}"
24 changes: 24 additions & 0 deletions lib/galaxy/model/scripts/dedup_emails.py
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()
24 changes: 24 additions & 0 deletions lib/galaxy/model/scripts/dedup_usernames.py
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()
81 changes: 81 additions & 0 deletions lib/galaxy/model/scripts/user_deduplicator.py
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:
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

@mvdbeek mvdbeek Jul 4, 2024

Choose a reason for hiding this comment

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

there is not much else we can do:

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.

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}"
4 changes: 3 additions & 1 deletion packages/data/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ console_scripts =
galaxy-load-objects = galaxy.model.store.load_objects:main
galaxy-manage-db = galaxy.model.orm.scripts:manage_db
galaxy-prune-histories = galaxy.model.scripts:prune_history_table
galaxy-dedup-usernames = galaxy.model.scripts:dedup_usernames
galaxy-dedup-emails = galaxy.model.scripts:dedup_emails

[options.packages.find]
exclude =
tests*
tests*
Loading