-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration for username column db schema + data
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...el/migrations/alembic/versions_gxy/c63848676caf_update_username_column_schema_and_data.py
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,53 @@ | ||
"""Update username column schema and data | ||
Revision ID: c63848676caf | ||
Revises: c14a3c93d66b | ||
Create Date: 2024-06-11 13:41:36.411803 | ||
""" | ||
|
||
from alembic import op | ||
from sqlalchemy import ( | ||
or_, | ||
select, | ||
update, | ||
) | ||
|
||
from galaxy.managers.users import username_from_email_with_connection | ||
from galaxy.model import User | ||
from galaxy.model.migrations.util import ( | ||
alter_column, | ||
transaction, | ||
) | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "c63848676caf" | ||
down_revision = "c14a3c93d66b" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
table_name = "galaxy_user" | ||
column_name = "username" | ||
|
||
|
||
def upgrade(): | ||
with transaction(): | ||
_generate_missing_usernames() | ||
alter_column(table_name, column_name, nullable=False) | ||
|
||
|
||
def downgrade(): | ||
with transaction(): | ||
# The data migration is one-way: we can change back the database schema, | ||
# but we can't remove the generated username values. | ||
alter_column(table_name, column_name, nullable=True) | ||
|
||
|
||
def _generate_missing_usernames(): | ||
stmt = select(User.id, User.email).where(or_(User.username.is_(None), User.username == "")) | ||
connection = op.get_bind() | ||
users = connection.execute(stmt).all() | ||
for id, email in users: | ||
new_username = username_from_email_with_connection(connection, email) | ||
stmt = update(User).where(User.id == id).values(username=new_username) | ||
connection.execute(stmt) |