Add a new migration which removes/fixes the old migration

The previous migration, as it turns out, was not needed, and there
were many inconsistencies put in place by adding it.  See issue #920.

This commit sponsored by Gergő Tisza.  Thank you!
This commit is contained in:
Christopher Allan Webber 2014-08-07 13:24:07 -05:00
parent 18cd6b3015
commit bb12fb807e
2 changed files with 51 additions and 1 deletions

View File

@ -789,6 +789,7 @@ def fix_privilege_user_association_table(db):
db.commit() db.commit()
@RegisterMigration(22, MIGRATIONS) @RegisterMigration(22, MIGRATIONS)
def add_index_username_field(db): def add_index_username_field(db):
""" """
@ -802,3 +803,52 @@ def add_index_username_field(db):
new_index.create() new_index.create()
db.commit() db.commit()
@RegisterMigration(23, MIGRATIONS)
def revert_username_index(db):
"""
"""
metadata = MetaData(bind=db.bind)
user_table = inspect_table(metadata, "core__users")
indexes = {index.name: index for index in user_table.indexes}
if not (u'ix_core__users_uploader' in indexes or
u'ix_core__users_username' in indexes):
# We don't need to do anything.
# The database isn't in a state where it needs fixing
#
# (ie, either went through the previous borked migration or
# was initialized with a models.py where core__users was both
# unique=True and index=True)
return
if db.bind.url.drivername == 'sqlite':
# Again, sqlite has problems. So this is tricky.
# Yes, this is correct to use User_vR1! Nothing has changed
# between the *correct* version of this table and migration 18.
User_vR1.__table__.create(db.bind)
db.commit()
new_user_table = inspect_table(metadata, 'rename__users')
replace_table_hack(db, user_table, new_user_table)
else:
# If the db is not run using SQLite, this process is much simpler...
# ...as usual ;)
# Remove whichever of the not-used indexes are in place
if u'ix_core__users_uploader' in indexes:
index = indexes[u'ix_core__users_uploader']
index.drop()
if u'ix_core__users_username' in indexes:
index = indexes[u'ix_core__users_username']
index.drop()
db.commit()
# Add the unique constraint
constraint = UniqueConstraint(
'username', table=user_table)
constraint.create()
db.commit()

View File

@ -58,7 +58,7 @@ class User(Base, UserMixin):
__tablename__ = "core__users" __tablename__ = "core__users"
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
username = Column(Unicode, nullable=False, unique=True, index=True) username = Column(Unicode, nullable=False, unique=True)
# Note: no db uniqueness constraint on email because it's not # Note: no db uniqueness constraint on email because it's not
# reliable (many email systems case insensitive despite against # reliable (many email systems case insensitive despite against
# the RFC) and because it would be a mess to implement at this # the RFC) and because it would be a mess to implement at this