Skip to content

Commit

Permalink
Migrate existing submissions to sync templates
Browse files Browse the repository at this point in the history
Submissions created between the original introduction of the top-level
templates array and now could be in an inconsistent state.
  • Loading branch information
naglepuff committed Sep 18, 2024
1 parent cab0b9b commit 532211f
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions nmdc_server/migrations/versions/ff4e651c3007_migrate_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Migrate study templates

Revision ID: ff4e651c3007
Revises: 317274ad8137
Create Date: 2024-09-18 17:21:05.171525

"""

from typing import Optional

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import column, table

# revision identifiers, used by Alembic.
revision: str = "ff4e651c3007"
down_revision: Optional[str] = "317274ad8137"
branch_labels: Optional[str] = None
depends_on: Optional[str] = None


def upgrade():
submission_metadata = table(
"submission_metadata",
column("id", sa.String),
column("metadata_submission", JSONB),
column("templates", JSONB),
)

connection = op.get_bind()
submissions = connection.execute(
sa.select([submission_metadata.c.id, submission_metadata.c.metadata_submission])
)

for submission in submissions:
templates = submission.metadata_submission.get("templates")
if templates:
connection.execute(
submission_metadata.update()
.where(submission_metadata.c.id == submission.id)
.values(templates=templates)
)


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###

0 comments on commit 532211f

Please sign in to comment.