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:
parent
70bceff85f
commit
c56a88b43e
@ -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))
|
||||
|
||||
db.commit()
|
||||
|
||||
privilege_user_assoc.drop()
|
||||
new_privilege_user_assoc.rename('core__privileges_users')
|
||||
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()
|
||||
|
||||
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()
|
||||
|
@ -877,11 +877,11 @@ class PrivilegeUserAssociation(Base):
|
||||
|
||||
__tablename__ = 'core__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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user