From 287ac8d7414605eaa72adaf0dbcb3ab2337e00b0 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 12 Nov 2021 22:16:02 +0100 Subject: [PATCH] fixing migration of olds confirmRevokes datas --- .../968b79fa7756_upgrade_confirmrevoke.py | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 ereuse_devicehub/migrations/versions/968b79fa7756_upgrade_confirmrevoke.py diff --git a/ereuse_devicehub/migrations/versions/968b79fa7756_upgrade_confirmrevoke.py b/ereuse_devicehub/migrations/versions/968b79fa7756_upgrade_confirmrevoke.py new file mode 100644 index 00000000..f1a1603b --- /dev/null +++ b/ereuse_devicehub/migrations/versions/968b79fa7756_upgrade_confirmrevoke.py @@ -0,0 +1,52 @@ +"""upgrade confirmrevoke + +Revision ID: 968b79fa7756 +Revises: d22d230d2850 +Create Date: 2021-11-12 19:18:39.135386 + +""" +from alembic import op +from alembic import context +import sqlalchemy as sa +import sqlalchemy_utils +import citext +import teal + + +# revision identifiers, used by Alembic. +revision = '968b79fa7756' +down_revision = 'd22d230d2850' +branch_labels = None +depends_on = None + + +def get_inv(): + INV = context.get_x_argument(as_dictionary=True).get('inventory') + if not INV: + raise ValueError("Inventory value is not specified") + return INV + +def upgrade(): + con = op.get_bind() + + + confirmsRevokes_sql = f"select * from {get_inv()}.action as action join {get_inv()}.confirm as confirm on action.id=confirm.id where action.type='ConfirmRevoke'" + revokes_sql = f"select confirm.id, confirm.action_id from {get_inv()}.action as action join {get_inv()}.confirm as confirm on action.id=confirm.id where action.type='Revoke'" + confirmsRevokes = [a for a in con.execute(confirmsRevokes_sql)] + revokes = {ac.id: ac.action_id for ac in con.execute(revokes_sql)} + + for ac in confirmsRevokes: + ac_id = ac.id + revoke_id = ac.action_id + trade_id = revokes[revoke_id] + sql_action = f"update {get_inv()}.action set type='Revoke' where type='ConfirmRevoke' and id='{ac_id}'" + sql_confirm = f"update {get_inv()}.confirm set action_id='{trade_id}' where id='{ac_id}'" + con.execute(sql_action) + con.execute(sql_confirm) + + assert 1 == 2 + + + +def downgrade(): + pass