-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6909c1
commit 7ef871d
Showing
4 changed files
with
163 additions
and
2 deletions.
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
44 changes: 44 additions & 0 deletions
44
magpie/alembic/versions/2023-11-10_91af68bcdc67_case_insensitive_user_name_constraint.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,44 @@ | ||
""" | ||
Case Insensitive User_Name Constraint | ||
Revision ID: 91af68bcdc67 | ||
Revises: 5e5acc33adce | ||
Create Date: 2023-11-10 15:58:23.068465 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.orm.session import sessionmaker | ||
|
||
# Revision identifiers, used by Alembic. | ||
# pylint: disable=C0103,invalid-name # revision control variables not uppercase | ||
revision = "91af68bcdc67" | ||
down_revision = "5e5acc33adce" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
Session = sessionmaker() | ||
|
||
users = sa.table( | ||
"users", | ||
sa.column("id", sa.Integer), | ||
sa.column("user_name", sa.String), | ||
) | ||
|
||
|
||
def upgrade(): | ||
op.create_index( | ||
"ix_users_user_name_unique_case_insensitive", | ||
"users", | ||
[sa.text("lower(user_name)")], | ||
unique=True, | ||
) | ||
session = Session(bind=op.get_bind()) | ||
for user in session.execute(sa.select(users)): | ||
session.execute(users.update().where(users.c.id == user.id).values(user_name=user.user_name.lower())) | ||
session.commit() | ||
|
||
|
||
def downgrade(): | ||
op.drop_index("ix_users_user_name_unique_case_insensitive", "users") | ||
# Previous case information is lost and cannot be restored |
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