Skip to content

Commit

Permalink
fix migration downgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
bouttier committed Jan 4, 2024
1 parent 87faa9b commit a23dde9
Showing 1 changed file with 19 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,38 +67,25 @@ def upgrade():


def downgrade():
bind = op.get_bind()
session = sa.orm.Session(bind=bind)
# Do not use NotificationCategory.query as it is not the same session!
category = session.scalars(
sa.select(NotificationCategory).where(NotificationCategory.code == CATEGORY_CODE)
).one_or_none()

if category is not None:
session.delete(
session.scalars(
sa.select(NotificationRule).where(NotificationRule.code_category == category.code)
).all()
)
# Since there is no cascade, need to delete template manually
session.delete(
session.scalars(
sa.select(NotificationTemplate).where(
NotificationTemplate.code_category == category.code
)
).all()
conn = op.get_bind()
metadata = sa.MetaData(bind=conn)
notification_category = sa.Table(
"bib_notifications_categories", metadata, schema="gn_notifications", autoload_with=conn
)
notification_template = sa.Table(
"bib_notifications_templates", metadata, schema="gn_notifications", autoload_with=conn
)
notification_rule = sa.Table(
"t_notifications_rules", metadata, schema="gn_notifications", autoload_with=conn
)
category = conn.execute(
sa.select(notification_category).where(notification_category.c.code == CATEGORY_CODE)
).one()
op.execute(
sa.delete(notification_template).where(
notification_template.c.code_category == category.code
)

session.delete(category)
session.commit()

)
op.execute(
f"""
DELETE FROM
gn_notifications.t_notifications_rules
WHERE
code_category = '{CATEGORY_CODE}'
AND
id_role IS NULL
"""
sa.delete(notification_rule).where(notification_rule.c.code_category == category.code)
)

0 comments on commit a23dde9

Please sign in to comment.