Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tags-db): correct tag and study_tag migration script #1934

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ def upgrade() -> None:
# insert the tags in the `tag` table
labels = set(itertools.chain.from_iterable(tags_by_ids.values()))
bulk_tags = [{"label": label, "color": secrets.choice(COLOR_NAMES)} for label in labels]
sql = sa.text("INSERT INTO tag (label, color) VALUES (:label, :color)")
connexion.execute(sql, *bulk_tags)
if bulk_tags:
sql = sa.text("INSERT INTO tag (label, color) VALUES (:label, :color)")
connexion.execute(sql, *bulk_tags)

# Create relationships between studies and tags in the `study_tag` table
bulk_study_tags = ({"study_id": id_, "tag_label": lbl} for id_, tags in tags_by_ids.items() for lbl in tags)
sql = sa.text("INSERT INTO study_tag (study_id, tag_label) VALUES (:study_id, :tag_label)")
connexion.execute(sql, *bulk_study_tags)
bulk_study_tags = [{"study_id": id_, "tag_label": lbl} for id_, tags in tags_by_ids.items() for lbl in tags]
if bulk_study_tags:
sql = sa.text("INSERT INTO study_tag (study_id, tag_label) VALUES (:study_id, :tag_label)")
connexion.execute(sql, *bulk_study_tags)


def downgrade() -> None:
Expand Down Expand Up @@ -92,9 +94,10 @@ def downgrade() -> None:
objects_by_ids[study_id] = obj

# Updating objects in the `study_additional_data` table
sql = sa.text("UPDATE study_additional_data SET patch = :patch WHERE study_id = :study_id")
bulk_patches = [{"study_id": id_, "patch": json.dumps(obj)} for id_, obj in objects_by_ids.items()]
connexion.execute(sql, *bulk_patches)
if bulk_patches:
sql = sa.text("UPDATE study_additional_data SET patch = :patch WHERE study_id = :study_id")
connexion.execute(sql, *bulk_patches)

# Deleting study_tags and tags
connexion.execute("DELETE FROM study_tag")
Expand Down
Loading