diff --git a/ereuse_devicehub/migrations/versions/054a3aea9f08_transfer.py b/ereuse_devicehub/migrations/versions/054a3aea9f08_transfer.py index 5246c508..d68fbe50 100644 --- a/ereuse_devicehub/migrations/versions/054a3aea9f08_transfer.py +++ b/ereuse_devicehub/migrations/versions/054a3aea9f08_transfer.py @@ -5,6 +5,8 @@ Revises: 8571fb32c912 Create Date: 2022-05-27 11:07:18.245322 """ +from uuid import uuid4 + import citext import sqlalchemy as sa from alembic import context, op @@ -24,6 +26,31 @@ def get_inv(): return INV +def upgrade_datas(): + sql = f'select user_from_id, user_to_id, lot_id, code from {get_inv()}.trade where confirm=False' + con = op.get_bind() + + sql_phantom = 'select id from common.user where phantom=True' + phantoms = [x[0] for x in con.execute(sql_phantom)] + + for ac in con.execute(sql): + id = uuid4() + user_from = ac.user_from_id + user_to = ac.user_to_id + lot = ac.lot_id + code = ac.code + columns = '(id, user_from_id, user_to_id, lot_id, code)' + values = f'(\'{id}\', \'{user_from}\', \'{user_to}\', \'{lot}\', \'{code}\')' + if user_from not in phantoms: + columns = '(id, user_to_id, lot_id, code)' + values = f'(\'{id}\', \'{user_to}\', \'{lot}\', \'{code}\')' + if user_to not in phantoms: + columns = '(id, user_from_id, lot_id, code)' + values = f'(\'{id}\', \'{user_from}\', \'{lot}\', \'{code}\')' + new_transfer = f'insert into {get_inv()}.transfer {columns} values {values}' + op.execute(new_transfer) + + def upgrade(): # creating transfer table op.create_table( @@ -45,7 +72,7 @@ def upgrade(): sa.Column( 'description', citext.CIText(), - nullable=False, + nullable=True, comment='A comment about the action.', ), sa.Column('date', sa.TIMESTAMP(timezone=True), nullable=True), @@ -83,6 +110,8 @@ def upgrade(): schema=f'{get_inv()}', ) + upgrade_datas() + def downgrade(): op.drop_index(