-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from uc-cdis/fix/remove-deprecated-metadata-keys
Fix/remove deprecated metadata keys
- Loading branch information
Showing
3 changed files
with
134 additions
and
3 deletions.
There are no files selected for viewing
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
57 changes: 57 additions & 0 deletions
57
migrations/versions/6819874e85b9_remove_deprecated_metadata.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,57 @@ | ||
"""remove deprecated metadata | ||
Revision ID: 6819874e85b9 | ||
Revises: 3354f2c466ec | ||
Create Date: 2022-09-27 13:43:39.827523 | ||
""" | ||
import json | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "6819874e85b9" | ||
down_revision = "3354f2c466ec" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def escape(str): | ||
# escape single quotes for SQL statement | ||
return str.replace("'", "''") | ||
|
||
|
||
def upgrade(): | ||
"""Remove deprecated metadata keys.""" | ||
|
||
remove_metadata_keys = ["_uploader_id", "_filename", "_bucket", "_file_extension"] | ||
|
||
# extract existing PK (guid) and metadata (data) columns | ||
connection = op.get_bind() | ||
offset = 0 | ||
limit = 500 | ||
query = ( | ||
f"SELECT guid, data FROM metadata ORDER BY guid LIMIT {limit} OFFSET {offset}" | ||
) | ||
results = connection.execute(query).fetchall() | ||
while results: | ||
for r in results: | ||
guid, data = r[0], r[1] | ||
# scrub internal fields from metadata | ||
for metadata_key in remove_metadata_keys: | ||
if metadata_key in data.keys(): | ||
data.pop(metadata_key) | ||
sql_statement = f"""UPDATE metadata | ||
SET data='{escape(json.dumps(data))}' | ||
WHERE guid='{guid}'""" | ||
connection.execute(sql_statement) | ||
# Grab another batch of rows | ||
offset += limit | ||
query = f"SELECT guid, data FROM metadata ORDER BY guid LIMIT {limit} OFFSET {offset} " | ||
results = connection.execute(query).fetchall() | ||
|
||
|
||
def downgrade(): | ||
pass |
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