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.)
This commit is contained in:
Christopher Allan Webber 2014-06-10 18:02:34 -05:00
parent 70bceff85f
commit c56a88b43e
2 changed files with 27 additions and 21 deletions

View File

@ -709,6 +709,7 @@ def create_moderation_tables(db):
db.commit() db.commit()
@RegisterMigration(19, MIGRATIONS) @RegisterMigration(19, MIGRATIONS)
def drop_MediaEntry_collected(db): def drop_MediaEntry_collected(db):
""" """
@ -739,16 +740,15 @@ def add_metadata_column(db):
class PrivilegeUserAssociation_R1(declarative_base()): class PrivilegeUserAssociation_R1(declarative_base()):
__tablename__ = 'rename__privileges_users' __tablename__ = 'rename__privileges_users'
user_id = Column( user = Column(
Integer, Integer,
ForeignKey(User.id), ForeignKey(User.id),
primary_key=True) primary_key=True)
privilege_id = Column( privilege = Column(
Integer, Integer,
ForeignKey(Privilege.id), ForeignKey(Privilege.id),
primary_key=True) primary_key=True)
@RegisterMigration(21, MIGRATIONS) @RegisterMigration(21, MIGRATIONS)
def fix_privilege_user_association_table(db): def fix_privilege_user_association_table(db):
""" """
@ -760,22 +760,28 @@ def fix_privilege_user_association_table(db):
privilege_user_assoc = inspect_table( privilege_user_assoc = inspect_table(
metadata, 'core__privileges_users') metadata, 'core__privileges_users')
PrivilegeUserAssociation_R1.__table__.create(db.bind)
db.commit()
new_privilege_user_assoc = inspect_table( if db.bind.url.drivername == 'sqlite':
metadata, 'rename__privileges_users') PrivilegeUserAssociation_R1.__table__.create(db.bind)
result = db.execute(privilege_user_assoc.select()) db.commit()
for row in result:
# The columns were improperly named before, so we switch the columns new_privilege_user_assoc = inspect_table(
user_id, priv_id = row['core__privilege_id'], row['core__user_id'] metadata, 'rename__privileges_users')
db.execute(new_privilege_user_assoc.insert().values( result = db.execute(privilege_user_assoc.select())
user_id=user_id, for row in result:
privilege_id=priv_id)) # The columns were improperly named before, so we switch the columns
user_id, priv_id = row['core__privilege_id'], row['core__user_id']
db.commit() db.execute(new_privilege_user_assoc.insert().values(
user=user_id,
privilege_user_assoc.drop() privilege=priv_id))
new_privilege_user_assoc.rename('core__privileges_users')
db.commit()
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() db.commit()

View File

@ -877,11 +877,11 @@ class PrivilegeUserAssociation(Base):
__tablename__ = 'core__privileges_users' __tablename__ = 'core__privileges_users'
user_id = Column( user = Column(
Integer, Integer,
ForeignKey(User.id), ForeignKey(User.id),
primary_key=True) primary_key=True)
privilege_id = Column( privilege = Column(
Integer, Integer,
ForeignKey(Privilege.id), ForeignKey(Privilege.id),
primary_key=True) primary_key=True)