-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Replace vfolder's status_history's type map with list
- Loading branch information
1 parent
16af161
commit 3beaf57
Showing
3 changed files
with
84 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Change the type of `vfolders.status_history` from a mapping of status and timestamps to a list of log entries containing status and timestamps, to preserve the log entries. |
67 changes: 67 additions & 0 deletions
67
...d/manager/models/alembic/versions/786be66ef4e5_replace_vfolders_status_history_s_type_.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,67 @@ | ||
"""Replace vfolders status_history's type map with list | ||
Revision ID: 786be66ef4e5 | ||
Revises: 8c8e90aebacd | ||
Create Date: 2024-05-07 05:10:23.799723 | ||
""" | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "786be66ef4e5" | ||
down_revision = "8c8e90aebacd" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.execute( | ||
""" | ||
WITH data AS ( | ||
SELECT id, | ||
(jsonb_each(status_history)).key AS status, | ||
(jsonb_each(status_history)).value AS timestamp | ||
FROM vfolders | ||
) | ||
UPDATE vfolders | ||
SET status_history = ( | ||
SELECT jsonb_agg( | ||
jsonb_build_object('status', status, 'timestamp', timestamp) | ||
) | ||
FROM data | ||
WHERE data.id = vfolders.id | ||
); | ||
""" | ||
) | ||
|
||
op.execute("UPDATE vfolders SET status_history = '[]'::jsonb WHERE status_history IS NULL;") | ||
op.alter_column( | ||
"vfolders", | ||
"status_history", | ||
nullable=False, | ||
default=[], | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.execute( | ||
""" | ||
WITH data AS ( | ||
SELECT id, | ||
jsonb_object_agg( | ||
elem->>'status', elem->>'timestamp' | ||
) AS new_status_history | ||
FROM vfolders, | ||
jsonb_array_elements(status_history) AS elem | ||
GROUP BY id | ||
) | ||
UPDATE vfolders | ||
SET status_history = data.new_status_history | ||
FROM data | ||
WHERE data.id = vfolders.id; | ||
""" | ||
) | ||
|
||
op.alter_column("vfolders", "status_history", nullable=True, default=None) | ||
op.execute("UPDATE vfolders SET status_history = NULL WHERE status_history = '[]'::jsonb;") |
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