mirror of
https://github.com/sartography/cr-connect-workflow.git
synced 2025-02-23 21:28:32 +00:00
First attempt at changes to LookupFile table
Want to migrate data and modify table Have to get session through bind
This commit is contained in:
parent
86a6039dc8
commit
d60ebf9a54
41
migrations/versions/4980cb3dea77_modify_lookup_table.py
Normal file
41
migrations/versions/4980cb3dea77_modify_lookup_table.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
"""modify lookup table file_data_model_id -> file_id
|
||||||
|
|
||||||
|
Revision ID: 4980cb3dea77
|
||||||
|
Revises: 7225d990740e
|
||||||
|
Create Date: 2022-01-06 12:07:47.066325
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
from crc.models.file import FileDataModel, LookupFileModel
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '4980cb3dea77'
|
||||||
|
down_revision = '7225d990740e'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
bind = op.get_bind()
|
||||||
|
session = sa.orm.Session(bind=bind)
|
||||||
|
|
||||||
|
op.add_column('lookup_file', sa.Column('file_model_id', sa.Integer(), nullable=True))
|
||||||
|
op.create_foreign_key(None, 'lookup_file', 'file', ['file_model_id'], ['id'])
|
||||||
|
|
||||||
|
lookup_file_records = session.query(LookupFileModel).all()
|
||||||
|
for lookup in lookup_file_records:
|
||||||
|
file_model_id = session.query(FileDataModel.file_model_id).filter(FileDataModel.id==lookup.file_data_model_id).scalar()
|
||||||
|
lookup.file_model_id = file_model_id
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
op.drop_column('lookup_file', 'file_data_model_id')
|
||||||
|
op.drop_constraint('lookup_file_file_data_model_id_fkey', 'lookup_file', type_='foreignkey')
|
||||||
|
|
||||||
|
print('revision 4980cb3dea77: upgrade: done: ')
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
pass
|
@ -57,6 +57,8 @@ def upgrade():
|
|||||||
session.delete(dependency)
|
session.delete(dependency)
|
||||||
lookups = session.query(LookupFileModel).filter(LookupFileModel.file_data_model_id==processed_model.id).all()
|
lookups = session.query(LookupFileModel).filter(LookupFileModel.file_data_model_id==processed_model.id).all()
|
||||||
for lookup in lookups:
|
for lookup in lookups:
|
||||||
|
# TODO: do the file_data_model_id -> file_model_id dance
|
||||||
|
# Add file_model_id column, populate file_model_id column, delete file_data_model_id column
|
||||||
lookup.file_data_model_id = None
|
lookup.file_data_model_id = None
|
||||||
session.delete(processed_model)
|
session.delete(processed_model)
|
||||||
print(f'upgrade: in processed files: file_id: {file_id}')
|
print(f'upgrade: in processed files: file_id: {file_id}')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user