From bb12fb807e59cbe124c32c0b4fa2a74e0b81aade Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Thu, 7 Aug 2014 13:24:07 -0500 Subject: [PATCH] Add a new migration which removes/fixes the old migration MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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! --- mediagoblin/db/migrations.py | 50 ++++++++++++++++++++++++++++++++++++ mediagoblin/db/models.py | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/mediagoblin/db/migrations.py b/mediagoblin/db/migrations.py index 88cda6f1..3bcf2a65 100644 --- a/mediagoblin/db/migrations.py +++ b/mediagoblin/db/migrations.py @@ -789,6 +789,7 @@ def fix_privilege_user_association_table(db): db.commit() + @RegisterMigration(22, MIGRATIONS) def add_index_username_field(db): """ @@ -802,3 +803,52 @@ def add_index_username_field(db): new_index.create() 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() diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py index b3f7e23d..932ba074 100644 --- a/mediagoblin/db/models.py +++ b/mediagoblin/db/models.py @@ -58,7 +58,7 @@ class User(Base, UserMixin): __tablename__ = "core__users" 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 # reliable (many email systems case insensitive despite against # the RFC) and because it would be a mess to implement at this -- 2.25.1