upgrade actual datas in migration

This commit is contained in:
Cayo Puigdefabregas 2021-03-04 20:42:50 +01:00
parent 083ce4e660
commit 3a4410a4ce

View file

@ -10,6 +10,10 @@ import sqlalchemy as sa
from alembic import op from alembic import op
from alembic import context 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 identifiers, used by Alembic.
revision = '8cb91ad1cc40' revision = '8cb91ad1cc40'
@ -25,11 +29,22 @@ def get_inv():
return 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(): def upgrade():
op.add_column('device', sa.Column('code', citext.CIText(), op.add_column('device', sa.Column('code', citext.CIText(),
unique=True, unique=True,
nullable=True), schema=f'{get_inv()}') nullable=True), schema=f'{get_inv()}')
upgrade_data()
def downgrade(): def downgrade():
op.drop_column('device', 'code', schema=f'{get_inv()}') op.drop_column('device', 'code', schema=f'{get_inv()}')