diff options
author | Christopher Allan Webber <cwebber@dustycloud.org> | 2014-06-10 18:02:34 -0500 |
---|---|---|
committer | Christopher Allan Webber <cwebber@dustycloud.org> | 2014-06-10 18:02:34 -0500 |
commit | c56a88b43e168196c8c4b3778fccde2497d2e32b (patch) | |
tree | 9b61d1977a121b8fa7ac2d91bf69648f2f9d87ad /mediagoblin/db/migrations.py | |
parent | 70bceff85fd678a7b6653b2bf377b075ebd11590 (diff) | |
download | mediagoblin-c56a88b43e168196c8c4b3778fccde2497d2e32b.tar.lz mediagoblin-c56a88b43e168196c8c4b3778fccde2497d2e32b.tar.xz mediagoblin-c56a88b43e168196c8c4b3778fccde2497d2e32b.zip |
Switch "user_id" to "privilege" and "privilege_id" to "user".
This builds on the previous code Natalie wrote, but makes some changes:
- More direct alterations for non-sqlite code
- In both cases, I've made it so that we switched the field names from
privilege_id and user_id to user and privilege respectively. This
way we can do the name swap, but in one case it's "easy": just
changing the name. (In the sqlite case it's still tricky though.)
Diffstat (limited to 'mediagoblin/db/migrations.py')
-rw-r--r-- | mediagoblin/db/migrations.py | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/mediagoblin/db/migrations.py b/mediagoblin/db/migrations.py index c94fbda0..4660ba94 100644 --- a/mediagoblin/db/migrations.py +++ b/mediagoblin/db/migrations.py @@ -709,6 +709,7 @@ def create_moderation_tables(db): db.commit() + @RegisterMigration(19, MIGRATIONS) def drop_MediaEntry_collected(db): """ @@ -739,16 +740,15 @@ def add_metadata_column(db): class PrivilegeUserAssociation_R1(declarative_base()): __tablename__ = 'rename__privileges_users' - user_id = Column( + user = Column( Integer, ForeignKey(User.id), primary_key=True) - privilege_id = Column( + privilege = Column( Integer, ForeignKey(Privilege.id), primary_key=True) - @RegisterMigration(21, MIGRATIONS) def fix_privilege_user_association_table(db): """ @@ -760,22 +760,28 @@ def fix_privilege_user_association_table(db): privilege_user_assoc = inspect_table( metadata, 'core__privileges_users') - PrivilegeUserAssociation_R1.__table__.create(db.bind) - db.commit() - new_privilege_user_assoc = inspect_table( - metadata, 'rename__privileges_users') - result = db.execute(privilege_user_assoc.select()) - for row in result: - # The columns were improperly named before, so we switch the columns - user_id, priv_id = row['core__privilege_id'], row['core__user_id'] - db.execute(new_privilege_user_assoc.insert().values( - user_id=user_id, - privilege_id=priv_id)) + if db.bind.url.drivername == 'sqlite': + PrivilegeUserAssociation_R1.__table__.create(db.bind) + db.commit() + + new_privilege_user_assoc = inspect_table( + metadata, 'rename__privileges_users') + result = db.execute(privilege_user_assoc.select()) + for row in result: + # The columns were improperly named before, so we switch the columns + user_id, priv_id = row['core__privilege_id'], row['core__user_id'] + db.execute(new_privilege_user_assoc.insert().values( + user=user_id, + privilege=priv_id)) - db.commit() + db.commit() + + privilege_user_assoc.drop() + new_privilege_user_assoc.rename('core__privileges_users') - privilege_user_assoc.drop() - new_privilege_user_assoc.rename('core__privileges_users') + else: + privilege_user_assoc.c.user_id.alter(name="privilege") + privilege_user_assoc.c.privilege_id.alter(name="user") db.commit() |