Skip to content

Commit 6241a76

Browse files
committed
Assign study_name on patch and migrate existing names
1 parent d4e4ab5 commit 6241a76

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

nmdc_server/api.py

+1
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ async def update_submission(
825825
submission.metadata_submission = (
826826
submission.metadata_submission | body_dict["metadata_submission"]
827827
)
828+
submission.study_name = body_dict["metadata_submission"]["studyForm"]["studyName"]
828829
# Update permissions and status iff the user is an "owner"
829830
if current_user_role and current_user_role.role == models.SubmissionEditorRole.owner:
830831
new_permissions = body_dict.get("permissions", None)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Migrate Study names
2+
3+
Revision ID: 5403c7fe0b33
4+
Revises: 0ff690fb929d
5+
Create Date: 2024-09-09 18:50:11.771035
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 = "5403c7fe0b33"
18+
down_revision: Optional[str] = "0ff690fb929d"
19+
branch_labels: Optional[str] = None
20+
depends_on: Optional[str] = None
21+
22+
23+
def upgrade():
24+
# ### commands auto generated by Alembic - please adjust! ###
25+
submission_metadata = table(
26+
"submission_metadata",
27+
column("id", sa.String),
28+
column("metadata_submission", JSONB),
29+
column("study_name", sa.String),
30+
)
31+
32+
connection = op.get_bind()
33+
submissions = connection.execute(
34+
sa.select([submission_metadata.c.id, submission_metadata.c.metadata_submission])
35+
)
36+
37+
for submission in submissions:
38+
study_name = submission.metadata_submission["studyForm"].get("studyName")
39+
if study_name:
40+
connection.execute(
41+
submission_metadata.update()
42+
.where(submission_metadata.c.id == submission.id)
43+
.values(study_name=study_name)
44+
)
45+
pass
46+
# ### end Alembic commands ###
47+
48+
49+
def downgrade():
50+
# ### commands auto generated by Alembic - please adjust! ###
51+
pass
52+
# ### end Alembic commands ###

0 commit comments

Comments
 (0)