aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/db/migrations.py
diff options
context:
space:
mode:
authorChristopher Allan Webber <cwebber@dustycloud.org>2014-08-07 13:24:07 -0500
committerChristopher Allan Webber <cwebber@dustycloud.org>2014-08-07 14:57:46 -0500
commitbb12fb807e59cbe124c32c0b4fa2a74e0b81aade (patch)
tree8f6e69b93d79d99659c06ca0c3f04775c0072cce /mediagoblin/db/migrations.py
parent18cd6b3015abee7329ae4e8295c525b7abbd0105 (diff)
downloadmediagoblin-bb12fb807e59cbe124c32c0b4fa2a74e0b81aade.tar.lz
mediagoblin-bb12fb807e59cbe124c32c0b4fa2a74e0b81aade.tar.xz
mediagoblin-bb12fb807e59cbe124c32c0b4fa2a74e0b81aade.zip
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!
Diffstat (limited to 'mediagoblin/db/migrations.py')
-rw-r--r--mediagoblin/db/migrations.py50
1 files changed, 50 insertions, 0 deletions
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()