1- """
2- Associate materialization with availability and change metadata
1+ """Associate availability and materialization
32
4- Revision ID: 0c3d8cd664b4
3+ Revision ID: b8ef80efd70c
54Revises: c3d5f327296c
6- Create Date: 2025-02-23 07:58:08.850294+00:00
5+ Create Date: 2025-02-24 05:49:06.588675+00:00
6+
77"""
88
9+ # pylint: disable=no-member, invalid-name, missing-function-docstring, unused-import, no-name-in-module
910import json
1011import sqlalchemy as sa
1112from sqlalchemy .sql import table , column
1213from alembic import op
1314from sqlalchemy .dialects import postgresql
1415
1516# revision identifiers, used by Alembic.
16- revision = "0c3d8cd664b4 "
17+ revision = "b8ef80efd70c "
1718down_revision = "c3d5f327296c"
1819branch_labels = None
1920depends_on = None
2021
2122
2223def upgrade ():
2324 with op .batch_alter_table ("availabilitystate" , schema = None ) as batch_op :
24- batch_op .add_column (sa .Column ("custom_metadata" , sa .JSON (), nullable = True ))
25+ batch_op .add_column (
26+ sa .Column (
27+ "custom_metadata" ,
28+ postgresql .JSONB (astext_type = sa .Text ()),
29+ nullable = True ,
30+ ),
31+ )
2532 batch_op .add_column (
2633 sa .Column ("materialization_id" , sa .BigInteger (), nullable = True ),
2734 )
@@ -37,7 +44,7 @@ def upgrade():
3744 column ("id" , sa .BigInteger ()),
3845 column ("url" , sa .String ()),
3946 column ("links" , postgresql .JSON ),
40- column ("custom_metadata" , sa . JSON ),
47+ column ("custom_metadata" , postgresql . JSONB ),
4148 )
4249
4350 # Move data from url and links to custom_metadata
@@ -61,7 +68,7 @@ def upgrade():
6168 connection .execute (
6269 sa .update (availabilitystate )
6370 .where (availabilitystate .c .id == row .id )
64- .values (custom_metadata = json . dumps ( metadata ) ),
71+ .values (custom_metadata = metadata ),
6572 )
6673
6774 with op .batch_alter_table ("availabilitystate" , schema = None ) as batch_op :
@@ -86,14 +93,12 @@ def downgrade():
8693 "fk_availability_materialization_id_materialization" ,
8794 type_ = "foreignkey" ,
8895 )
89- batch_op .drop_column ("materialization_id" )
90- batch_op .drop_column ("custom_metadata" )
9196
9297 # Restore `url` and `links` from `custom_metadata`
9398 availabilitystate = table (
9499 "availabilitystate" ,
95100 column ("id" , sa .BigInteger ()),
96- column ("custom_metadata" , sa . JSON ),
101+ column ("custom_metadata" , postgresql . JSONB ),
97102 column ("url" , sa .String ()),
98103 column ("links" , postgresql .JSON ),
99104 )
@@ -104,7 +109,7 @@ def downgrade():
104109 ).fetchall ()
105110
106111 for row in results :
107- metadata = row .custom_metadata or {}
112+ metadata = json . loads ( row .custom_metadata ) if row . custom_metadata else {}
108113 conn .execute (
109114 sa .update (availabilitystate )
110115 .where (availabilitystate .c .id == row .id )
@@ -113,3 +118,7 @@ def downgrade():
113118 links = metadata .get ("links" ),
114119 ),
115120 )
121+
122+ with op .batch_alter_table ("availabilitystate" , schema = None ) as batch_op :
123+ batch_op .drop_column ("materialization_id" )
124+ batch_op .drop_column ("custom_metadata" )
0 commit comments