From 3a4410a4ce137ac24fa93e0215e6500f83202f59 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 4 Mar 2021 20:42:50 +0100 Subject: [PATCH] upgrade actual datas in migration --- .../versions/8cb91ad1cc40_add_code_to_device.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ereuse_devicehub/migrations/versions/8cb91ad1cc40_add_code_to_device.py b/ereuse_devicehub/migrations/versions/8cb91ad1cc40_add_code_to_device.py index cbf1254e..46f0dca9 100644 --- a/ereuse_devicehub/migrations/versions/8cb91ad1cc40_add_code_to_device.py +++ b/ereuse_devicehub/migrations/versions/8cb91ad1cc40_add_code_to_device.py @@ -10,6 +10,10 @@ import sqlalchemy as sa from alembic import op from alembic import context +from ereuse_devicehub.db import db +from ereuse_devicehub.resources.device.utils import hashids +from ereuse_devicehub.resources.device.models import Device + # revision identifiers, used by Alembic. revision = '8cb91ad1cc40' @@ -25,11 +29,22 @@ def get_inv(): return INV +def upgrade_data(): + con = op.get_bind() + devices = con.execute(f"select id from {get_inv()}.device") + for d in devices: + id_dev = d.id + code = hashids(d.id) + sql = f"update {get_inv()}.device set code='{code}' where id={id_dev};" + con.execute(sql) + + def upgrade(): op.add_column('device', sa.Column('code', citext.CIText(), unique=True, nullable=True), schema=f'{get_inv()}') + upgrade_data() def downgrade(): op.drop_column('device', 'code', schema=f'{get_inv()}')