forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
176 lines (157 loc) · 5.22 KB
/
cli.py
File metadata and controls
176 lines (157 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import argparse
import logging
import os
from importlib.resources import files
from alembic import command
from alembic.config import Config
def create_parser() -> argparse.ArgumentParser:
"""Create the argument parser for the migration tool."""
parser = argparse.ArgumentParser(description='A2A Database Migration Tool')
# Global options
parser.add_argument(
'-o',
'--owner',
help="Value for the 'owner' column (used in specific migrations). If not set defaults to 'unknown'",
)
parser.add_argument(
'-u',
'--database-url',
help='Database URL to use for the migrations. If not set, the DATABASE_URL environment variable will be used.',
)
parser.add_argument(
'-t',
'--table',
help="Specific table to update. If not set, both 'tasks' and 'push_notification_configs' are updated.",
action='append',
)
parser.add_argument(
'-v',
'--verbose',
help='Enable verbose output (sets sqlalchemy.engine logging to INFO)',
action='store_true',
)
parser.add_argument(
'--sql',
help='Run migrations in sql mode (generate SQL instead of executing)',
action='store_true',
)
subparsers = parser.add_subparsers(dest='cmd', help='Migration command')
# Upgrade command
up_parser = subparsers.add_parser(
'upgrade', help='Upgrade to a later version'
)
up_parser.add_argument(
'revision',
nargs='?',
default='head',
help='Revision target (default: head)',
)
up_parser.add_argument(
'-o', '--owner', dest='sub_owner', help='Alias for top-level --owner'
)
up_parser.add_argument(
'-u',
'--database-url',
dest='sub_database_url',
help='Alias for top-level --database-url',
)
up_parser.add_argument(
'-t',
'--table',
dest='sub_table',
help='Alias for top-level --table',
action='append',
)
up_parser.add_argument(
'-v',
'--verbose',
dest='sub_verbose',
help='Enable verbose output (sets sqlalchemy.engine logging to INFO)',
action='store_true',
)
up_parser.add_argument(
'--sql',
dest='sub_sql',
help='Run migrations in sql mode (generate SQL instead of executing)',
action='store_true',
)
# Downgrade command
down_parser = subparsers.add_parser(
'downgrade', help='Revert to a previous version'
)
down_parser.add_argument(
'revision',
nargs='?',
default='base',
help='Revision target (e.g., -1, base or a specific ID)',
)
down_parser.add_argument(
'-u',
'--database-url',
dest='sub_database_url',
help='Alias for top-level --database-url',
)
down_parser.add_argument(
'-t',
'--table',
dest='sub_table',
help='Alias for top-level --table',
action='append',
)
down_parser.add_argument(
'-v',
'--verbose',
dest='sub_verbose',
help='Enable verbose output (sets sqlalchemy.engine logging to INFO)',
action='store_true',
)
down_parser.add_argument(
'--sql',
dest='sub_sql',
help='Run migrations in sql mode (generate SQL instead of executing)',
action='store_true',
)
return parser
def run_migrations() -> None:
"""CLI tool to manage database migrations."""
# Configure logging to show INFO messages
logging.basicConfig(level=logging.INFO, format='%(levelname)s %(message)s')
parser = create_parser()
args = parser.parse_args()
# Default to upgrade head if no command is provided
if not args.cmd:
args.cmd = 'upgrade'
args.revision = 'head'
# Locate the bundled alembic.ini
ini_path = files('a2a').joinpath('alembic.ini')
cfg = Config(str(ini_path))
# Dynamically set the script location
migrations_path = files('a2a').joinpath('migrations')
cfg.set_main_option('script_location', str(migrations_path))
# Consolidate owner, db_url, tables, verbose and sql values
owner = args.owner or getattr(args, 'sub_owner', None)
db_url = args.database_url or getattr(args, 'sub_database_url', None)
tables = args.table or getattr(args, 'sub_table', None)
verbose = args.verbose or getattr(args, 'sub_verbose', False)
sql = args.sql or getattr(args, 'sub_sql', False)
# Pass custom arguments to the migration context
if owner:
if args.cmd == 'downgrade':
parser.error(
"The --owner option is not supported for the 'downgrade' command."
)
cfg.set_main_option('owner', owner)
if db_url:
os.environ['DATABASE_URL'] = db_url
if tables:
cfg.set_main_option('tables', ','.join(tables))
if verbose:
cfg.set_main_option('verbose', 'true')
# Execute the requested command
if args.cmd == 'upgrade':
logging.info('Upgrading database to %s', args.revision)
command.upgrade(cfg, args.revision, sql=sql)
elif args.cmd == 'downgrade':
logging.info('Downgrading database to %s', args.revision)
command.downgrade(cfg, args.revision, sql=sql)
logging.info('Done.')