2021-12-16 13:41:49 +00:00
|
|
|
"""Move files to filesystem
|
|
|
|
|
|
|
|
Revision ID: 7225d990740e
|
|
|
|
Revises: 44dd9397c555
|
|
|
|
Create Date: 2021-12-14 10:52:50.785342
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
from alembic import op
|
|
|
|
import sqlalchemy as sa
|
2022-01-04 12:24:21 +00:00
|
|
|
|
|
|
|
import crc
|
2021-12-16 19:22:37 +00:00
|
|
|
from crc import app, session
|
2022-01-04 12:24:21 +00:00
|
|
|
from crc.models.file import FileModel, FileDataModel, LookupFileModel
|
|
|
|
from crc.models.workflow import WorkflowSpecDependencyFile
|
2021-12-16 19:22:37 +00:00
|
|
|
from crc.services.file_service import FileService
|
2022-01-04 12:24:21 +00:00
|
|
|
from crc.services.temp_migration_service import FromFilesystemService, ToFilesystemService, SYNC_FILE_ROOT
|
2021-12-16 13:41:49 +00:00
|
|
|
|
2021-12-16 19:22:37 +00:00
|
|
|
import os
|
2021-12-16 13:41:49 +00:00
|
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
|
|
revision = '7225d990740e'
|
2021-12-17 16:48:24 +00:00
|
|
|
down_revision = '65b5ed6ae05b'
|
2021-12-16 13:41:49 +00:00
|
|
|
branch_labels = None
|
|
|
|
depends_on = None
|
|
|
|
|
|
|
|
|
2021-12-16 19:22:37 +00:00
|
|
|
def upgrade():
|
2021-12-16 13:41:49 +00:00
|
|
|
|
2021-12-16 19:22:37 +00:00
|
|
|
"""Starting this cautiously
|
|
|
|
Don't want to hork my dev system
|
|
|
|
Not deleting records yet
|
2021-12-16 13:41:49 +00:00
|
|
|
|
2021-12-16 19:22:37 +00:00
|
|
|
Originally, was only going to delete data in file_data.data
|
|
|
|
Now, thinking about deleting the record.
|
|
|
|
"""
|
|
|
|
|
|
|
|
processed_files = []
|
2022-01-04 12:24:21 +00:00
|
|
|
# Process workflow spec files
|
|
|
|
files = session.query(FileModel).filter(FileModel.workflow_spec_id is not None).all()
|
2021-12-16 13:41:49 +00:00
|
|
|
for file in files:
|
|
|
|
if file.archived is not True:
|
2021-12-16 19:22:37 +00:00
|
|
|
ToFilesystemService().write_file_to_system(file)
|
|
|
|
processed_files.append(file.id)
|
2021-12-16 13:41:49 +00:00
|
|
|
|
2022-01-04 12:24:21 +00:00
|
|
|
# TODO:
|
|
|
|
# delete processed files from file_data table
|
|
|
|
# delete workflow_spec_dependency_file table
|
|
|
|
# delete file_data_model_id from lookup_file table
|
2021-12-17 17:57:28 +00:00
|
|
|
for file_id in processed_files:
|
|
|
|
processed_models = session.query(FileDataModel).filter(FileDataModel.file_model_id==file_id).all()
|
|
|
|
for processed_model in processed_models:
|
2022-01-04 12:24:21 +00:00
|
|
|
dependency = session.query(WorkflowSpecDependencyFile).filter(WorkflowSpecDependencyFile.file_data_id==processed_model.id).first()
|
|
|
|
if dependency is not None:
|
|
|
|
session.delete(dependency)
|
|
|
|
lookups = session.query(LookupFileModel).filter(LookupFileModel.file_data_model_id==processed_model.id).all()
|
|
|
|
for lookup in lookups:
|
|
|
|
lookup.file_data_model_id = None
|
2021-12-17 17:57:28 +00:00
|
|
|
session.delete(processed_model)
|
|
|
|
print(f'upgrade: in processed files: file_id: {file_id}')
|
2021-12-16 13:41:49 +00:00
|
|
|
|
2022-01-04 12:24:21 +00:00
|
|
|
session.commit()
|
|
|
|
|
|
|
|
op.drop_table('workflow_spec_dependency_file')
|
2021-12-16 19:22:37 +00:00
|
|
|
print('upgrade: done: ')
|
2021-12-16 13:41:49 +00:00
|
|
|
|
|
|
|
|
2021-12-16 19:22:37 +00:00
|
|
|
def downgrade():
|
2021-12-16 13:41:49 +00:00
|
|
|
|
2021-12-16 19:22:37 +00:00
|
|
|
# TODO: This is a work in progress, and depends on what we do in upgrade()
|
2022-01-04 12:24:21 +00:00
|
|
|
op.create_table('workflow_spec_dependency_file',
|
|
|
|
sa.Column('file_data_id', sa.Integer(), nullable=False),
|
|
|
|
sa.Column('workflow_id', sa.Integer(), nullable=False),
|
|
|
|
sa.ForeignKeyConstraint(['file_data_id'], ['file_data.id'], ),
|
|
|
|
sa.ForeignKeyConstraint(['workflow_id'], ['workflow.id'], ),
|
|
|
|
sa.PrimaryKeyConstraint('file_data_id', 'workflow_id')
|
|
|
|
)
|
|
|
|
|
2021-12-16 19:22:37 +00:00
|
|
|
FromFilesystemService().update_file_metadata_from_filesystem(SYNC_FILE_ROOT)
|
2021-12-16 13:41:49 +00:00
|
|
|
|
|
|
|
print('downgrade: ')
|