66
77"""
88
9- from alembic import op
10- from dataclasses import dataclass
119import sqlalchemy as sa
12-
10+ from alembic import op
1311
1412# revision identifiers, used by Alembic.
1513revision = "47e51c42e391"
@@ -22,6 +20,10 @@ def upgrade() -> None:
2220 connection = op .get_bind ()
2321 with connection .begin_nested () as tx :
2422 op .execute (sa .text ("LOCK TABLE common.namespaces IN EXCLUSIVE MODE" ))
23+ op .execute (sa .text ("LOCK TABLE common.entity_slugs IN EXCLUSIVE MODE" ))
24+ op .execute (sa .text ("LOCK TABLE storage.data_connectors IN EXCLUSIVE MODE" ))
25+
26+ # Step 1: update the namespaces table and add the _global namespace
2527 op .drop_constraint (
2628 "either_group_id_or_user_id_is_set" ,
2729 "namespaces" ,
@@ -34,7 +36,6 @@ def upgrade() -> None:
3436 "(user_id IS NOT NULL) OR (group_id IS NOT NULL) OR (slug = '_global')" ,
3537 schema = "common" ,
3638 )
37-
3839 insert_global_namespace_stmt = (
3940 sa .insert (
4041 sa .table (
@@ -50,10 +51,9 @@ def upgrade() -> None:
5051 namespace_id = connection .execute (insert_global_namespace_stmt ).scalar ()
5152 if not namespace_id :
5253 raise RuntimeError ("Failed to insert the _global namespace" )
53-
5454 print (f"namespace_id={ namespace_id } " )
5555
56- op . execute ( sa . text ( "LOCK TABLE storage.data_connectors IN EXCLUSIVE MODE" ))
56+ # Step 2: create a row in the entity_slug table for each global data connector
5757 select_global_data_connectors_stmt = (
5858 sa .select (sa .column ("id" , type_ = sa .VARCHAR ), sa .column ("global_slug" , type_ = sa .VARCHAR ))
5959 .select_from (sa .table ("data_connectors" , schema = "storage" ))
@@ -86,13 +86,45 @@ def upgrade() -> None:
8686 if not slug_id :
8787 raise RuntimeError (f"Failed to insert the entity slug for data connector '{ dc_id } '" )
8888
89+ # Step 3: update the data_connectors table
90+ op .drop_index ("ix_storage_data_connectors_global_slug" , table_name = "data_connectors" , schema = "storage" )
91+ op .drop_column ("data_connectors" , "global_slug" , schema = "storage" )
92+ # TODO:
93+
8994 tx .commit ()
9095
9196
9297def downgrade () -> None :
9398 connection = op .get_bind ()
9499 with connection .begin_nested () as tx :
95100 op .execute (sa .text ("LOCK TABLE common.namespaces IN EXCLUSIVE MODE" ))
101+ op .execute (sa .text ("LOCK TABLE common.entity_slugs IN EXCLUSIVE MODE" ))
102+ op .execute (sa .text ("LOCK TABLE storage.data_connectors IN EXCLUSIVE MODE" ))
103+
104+ # Step 1: update the data_connectors table
105+ op .add_column (
106+ "data_connectors" ,
107+ sa .Column ("global_slug" , sa .VARCHAR (length = 99 ), autoincrement = False , nullable = True ),
108+ schema = "storage" ,
109+ )
110+ op .create_index (
111+ "ix_storage_data_connectors_global_slug" , "data_connectors" , ["global_slug" ], unique = True , schema = "storage"
112+ )
113+ # TODO:
114+
115+ # Step 2: create a row in the entity_slug table for each global data connector
116+ # TODO:
117+
118+ # Step 3: update the namespaces table and add the _global namespace
119+ delete_global_namespace_stmt = sa .delete (
120+ sa .table (
121+ "namespaces" ,
122+ sa .column ("id" , type_ = sa .VARCHAR ),
123+ sa .column ("slug" , type_ = sa .VARCHAR ),
124+ schema = "common" ,
125+ )
126+ ).where (sa .column ("slug" , type_ = sa .VARCHAR ) == sa .literal ("_global" ))
127+ op .execute (delete_global_namespace_stmt )
96128 op .drop_constraint (
97129 "either_group_id_or_user_id_is_set" ,
98130 "namespaces" ,
@@ -106,4 +138,3 @@ def downgrade() -> None:
106138 schema = "common" ,
107139 )
108140 tx .commit ()
109- # CheckConstraint("(user_id IS NULL) <> (group_id IS NULL)", name="either_group_id_or_user_id_is_set"),
0 commit comments