-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate existing submissions to sync templates
Submissions created between the original introduction of the top-level templates array and now could be in an inconsistent state.
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
nmdc_server/migrations/versions/ff4e651c3007_migrate_templates.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ### |