From efc37ee59f48378d155d22fa86f23cc27eafc744 Mon Sep 17 00:00:00 2001 From: mike cullerton Date: Wed, 13 Apr 2022 08:52:22 -0400 Subject: [PATCH] Prepare for migrating data_stores --- crc/models/data_store.py | 1 + crc/models/file.py | 2 +- ...89d5a6a2c0_migrate_file_data_to_document_table.py | 12 ++++++++++-- migrations/versions/92d554ab6e32_file_refactor.py | 4 ++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crc/models/data_store.py b/crc/models/data_store.py index 9ec961da..c1c21fc0 100644 --- a/crc/models/data_store.py +++ b/crc/models/data_store.py @@ -15,6 +15,7 @@ class DataStoreModel(db.Model): spec_id = db.Column(db.String) user_id = db.Column(db.String, nullable=True) file_id = db.Column(db.Integer, db.ForeignKey('file.id'), nullable=True) + document_id = db.Column(db.Integer, db.ForeignKey('document.id'), nullable=True) value = db.Column(db.String) diff --git a/crc/models/file.py b/crc/models/file.py index ed405fae..b75fa898 100644 --- a/crc/models/file.py +++ b/crc/models/file.py @@ -72,7 +72,7 @@ class DocumentModel(db.Model): task_spec = db.Column(db.String, nullable=True) irb_doc_code = db.Column(db.String, nullable=False) # Code reference to the documents.xlsx reference file. # TODO: Fix relationship with data_store table, then add this back in - # data_stores = relationship(DataStoreModel, cascade="all,delete", backref="file") + data_stores = relationship(DataStoreModel, cascade="all,delete", backref="document") md5_hash = db.Column(UUID(as_uuid=True), unique=False, nullable=False) data = deferred(db.Column(db.LargeBinary)) # Don't load it unless you have to. # TODO: Determine whether size is used (in frontend/bpmn) diff --git a/migrations/versions/3489d5a6a2c0_migrate_file_data_to_document_table.py b/migrations/versions/3489d5a6a2c0_migrate_file_data_to_document_table.py index 6d78559c..2b488435 100644 --- a/migrations/versions/3489d5a6a2c0_migrate_file_data_to_document_table.py +++ b/migrations/versions/3489d5a6a2c0_migrate_file_data_to_document_table.py @@ -9,8 +9,9 @@ from alembic import op import sqlalchemy as sa from sqlalchemy.exc import IntegrityError -from crc.models.file import FileModel, FileDataModel, DocumentModel from crc import app +from crc.models.data_store import DataStoreModel +from crc.models.file import FileModel, FileDataModel, DocumentModel @@ -21,6 +22,13 @@ branch_labels = None depends_on = None +def update_data_store(file_id, document_id, session): + data_stores = session.query(DataStoreModel).filter(DataStoreModel.file_id == file_id).all() + for data_store in data_stores: + data_store.document_id = document_id + session.commit() + + def upgrade(): bind = op.get_bind() session = sa.orm.Session(bind=bind) @@ -67,4 +75,4 @@ def upgrade(): def downgrade(): - pass + op.execute('DELETE FROM document;') diff --git a/migrations/versions/92d554ab6e32_file_refactor.py b/migrations/versions/92d554ab6e32_file_refactor.py index 6ade535d..e72b310c 100644 --- a/migrations/versions/92d554ab6e32_file_refactor.py +++ b/migrations/versions/92d554ab6e32_file_refactor.py @@ -39,7 +39,11 @@ def upgrade(): sa.ForeignKeyConstraint(['user_uid'], ['user.uid'], ), sa.ForeignKeyConstraint(['workflow_id'], ['workflow.id'], ), ) + op.add_column('data_store', sa.Column('document_id', sa.Integer(), nullable=True)) + op.create_foreign_key('document_id_key', 'data_store', 'document', ['document_id'], ['id']) def downgrade(): op.drop_table('document') + # op.drop_column('data_store', 'document_id') + # op.drop_constraint('document_id_key', 'data_store', type_='foreignkey')