Skip to content

Commit 532211f

Browse files
committed
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.
1 parent cab0b9b commit 532211f

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Migrate study templates
2+
3+
Revision ID: ff4e651c3007
4+
Revises: 317274ad8137
5+
Create Date: 2024-09-18 17:21:05.171525
6+
7+
"""
8+
9+
from typing import Optional
10+
11+
import sqlalchemy as sa
12+
from alembic import op
13+
from sqlalchemy.dialects.postgresql import JSONB
14+
from sqlalchemy.sql import column, table
15+
16+
# revision identifiers, used by Alembic.
17+
revision: str = "ff4e651c3007"
18+
down_revision: Optional[str] = "317274ad8137"
19+
branch_labels: Optional[str] = None
20+
depends_on: Optional[str] = None
21+
22+
23+
def upgrade():
24+
submission_metadata = table(
25+
"submission_metadata",
26+
column("id", sa.String),
27+
column("metadata_submission", JSONB),
28+
column("templates", JSONB),
29+
)
30+
31+
connection = op.get_bind()
32+
submissions = connection.execute(
33+
sa.select([submission_metadata.c.id, submission_metadata.c.metadata_submission])
34+
)
35+
36+
for submission in submissions:
37+
templates = submission.metadata_submission.get("templates")
38+
if templates:
39+
connection.execute(
40+
submission_metadata.update()
41+
.where(submission_metadata.c.id == submission.id)
42+
.values(templates=templates)
43+
)
44+
45+
46+
def downgrade():
47+
# ### commands auto generated by Alembic - please adjust! ###
48+
pass
49+
# ### end Alembic commands ###

0 commit comments

Comments
 (0)