1515 from alembic import context , op
1616except ImportError as e :
1717 raise ImportError (
18- "'Add columns owner and last_updated to database tables' migration requires Alembic. Install with: 'pip install a2a-sdk[a2a- db-cli]'."
18+ "'Add columns owner and last_updated to database tables' migration requires Alembic. Install with: 'pip install a2a-sdk[db-cli]'."
1919 ) from e
2020
2121
@@ -32,6 +32,38 @@ def _get_inspector() -> sa.engine.reflection.Inspector:
3232 return inspector
3333
3434
35+ def _add_column (table : str , value : str , column_name : str ) -> None :
36+ if not _column_exists (table , column_name ):
37+ op .add_column (
38+ table ,
39+ sa .Column (
40+ column_name ,
41+ sa .String (128 ),
42+ nullable = False ,
43+ server_default = value ,
44+ ),
45+ )
46+
47+
48+ def _add_index (table : str , index_name : str , columns : list [str ]) -> None :
49+ if not _index_exists (table , index_name ):
50+ op .create_index (
51+ index_name ,
52+ table ,
53+ columns ,
54+ )
55+
56+
57+ def _drop_column (table : str , column_name : str ) -> None :
58+ if _column_exists (table , column_name , True ):
59+ op .drop_column (table , column_name )
60+
61+
62+ def _drop_index (table : str , index_name : str ) -> None :
63+ if _index_exists (table , index_name , True ):
64+ op .drop_index (index_name , table_name = table )
65+
66+
3567def _table_exists (table_name : str ) -> bool :
3668 if context .is_offline_mode ():
3769 return True
@@ -69,89 +101,54 @@ def upgrade() -> None:
69101 'add_columns_owner_last_updated_default_owner' ,
70102 'legacy_v03_no_user_info' ,
71103 )
72- tasks_tables = ['tasks' ]
73- push_notification_tables = ['push_notification_configs' ]
74-
75- if tasks_tables_str := context .config .get_main_option ('tasks_tables' , None ):
76- tasks_tables .extend ([t .strip () for t in tasks_tables_str .split (',' )])
77- if push_notification_tables_str := context .config .get_main_option (
78- 'push_notification_tables' , None
79- ):
80- push_notification_tables .extend (
81- [t .strip () for t in push_notification_tables_str .split (',' )]
104+ tasks_table = context .config .get_main_option ('tasks_table' , 'tasks' )
105+ push_notification_configs_table = context .config .get_main_option (
106+ 'push_notification_configs_table' , 'push_notification_configs'
107+ )
108+
109+ if _table_exists (tasks_table ):
110+ _add_column (tasks_table , owner , 'owner' )
111+ _add_column (tasks_table , '0' , 'last_updated' )
112+ _add_index (
113+ tasks_table ,
114+ f'idx_{ tasks_table } _owner_last_updated' ,
115+ ['owner' , 'last_updated' ],
116+ )
117+ else :
118+ logging .warning (
119+ f"Table '{ tasks_table } ' does not exist. Skipping upgrade for this table."
82120 )
83121
84- for table in tasks_tables + push_notification_tables :
85- if _table_exists (table ):
86- if not _column_exists (table , 'owner' ):
87- op .add_column (
88- table ,
89- sa .Column (
90- 'owner' ,
91- sa .String (128 ),
92- nullable = False ,
93- server_default = owner ,
94- ),
95- )
96- else :
97- if table in tasks_tables :
98- tasks_tables .remove (table )
99- logging .warning (
100- f"Table '{ table } ' does not exist. Skipping upgrade for this table."
101- )
102-
103- for table in tasks_tables :
104- if _table_exists (table ):
105- if not _column_exists (table , 'last_updated' ):
106- op .add_column (
107- table ,
108- sa .Column ('last_updated' , sa .String (22 ), nullable = True ),
109- )
110- if not _index_exists (table , f'idx_{ table } _owner_last_updated' ):
111- op .create_index (
112- f'idx_{ table } _owner_last_updated' ,
113- table ,
114- ['owner' , 'last_updated' ],
115- )
116- else :
117- logging .warning (
118- f"Table '{ table } ' does not exist. Skipping upgrade for this table."
119- )
122+ if _table_exists (push_notification_configs_table ):
123+ _add_column (push_notification_configs_table , owner , 'owner' )
124+ else :
125+ logging .warning (
126+ f"Table '{ push_notification_configs_table } ' does not exist. Skipping upgrade for this table."
127+ )
120128
121129
122130def downgrade () -> None :
123131 """Downgrade schema."""
124- tasks_tables = ['tasks' ]
125- push_notification_tables = ['push_notification_configs' ]
126-
127- if tasks_tables_str := context .config .get_main_option ('tasks_tables' , None ):
128- tasks_tables .extend ([t .strip () for t in tasks_tables_str .split (',' )])
129- if push_notification_tables_str := context .config .get_main_option (
130- 'push_notification_tables' , None
131- ):
132- push_notification_tables .extend (
133- [t .strip () for t in push_notification_tables_str .split (',' )]
132+ tasks_table = context .config .get_main_option ('tasks_table' , 'tasks' )
133+ push_notification_configs_table = context .config .get_main_option (
134+ 'push_notification_configs_table' , 'push_notification_configs'
135+ )
136+
137+ if _table_exists (tasks_table ):
138+ _drop_index (
139+ tasks_table ,
140+ f'idx_{ tasks_table } _owner_last_updated' ,
141+ )
142+ _drop_column (tasks_table , 'owner' )
143+ _drop_column (tasks_table , 'last_updated' )
144+ else :
145+ logging .warning (
146+ f"Table '{ tasks_table } ' does not exist. Skipping downgrade for this table."
134147 )
135148
136- for table in tasks_tables :
137- if _table_exists (table ):
138- if _index_exists (table , f'idx_{ table } _owner_last_updated' , True ):
139- op .drop_index (
140- f'idx_{ table } _owner_last_updated' , table_name = table
141- )
142- if _column_exists (table , 'last_updated' , True ):
143- op .drop_column (table , 'last_updated' )
144- else :
145- tasks_tables .remove (table )
146- logging .warning (
147- f"Table '{ table } ' does not exist. Skipping downgrade for this table."
148- )
149-
150- for table in tasks_tables + push_notification_tables :
151- if _table_exists (table ):
152- if _column_exists (table , 'owner' , True ):
153- op .drop_column (table , 'owner' )
154- else :
155- logging .warning (
156- f"Table '{ table } ' does not exist. Skipping downgrade for this table."
157- )
149+ if _table_exists (push_notification_configs_table ):
150+ _drop_column (push_notification_configs_table , 'owner' )
151+ else :
152+ logging .warning (
153+ f"Table '{ push_notification_configs_table } ' does not exist. Skipping downgrade for this table."
154+ )
0 commit comments