|
| 1 | +import argparse |
| 2 | +import logging |
| 3 | +import os |
| 4 | + |
| 5 | +from importlib.resources import files |
| 6 | + |
| 7 | + |
| 8 | +try: |
| 9 | + from alembic import command |
| 10 | + from alembic.config import Config |
| 11 | + |
| 12 | +except ImportError as e: |
| 13 | + raise ImportError( |
| 14 | + "CLI requires Alembic. Install with: 'pip install a2a-sdk[db-cli]'." |
| 15 | + ) from e |
| 16 | + |
| 17 | + |
| 18 | +def _add_shared_args( |
| 19 | + parser: argparse.ArgumentParser, is_sub: bool = False |
| 20 | +) -> None: |
| 21 | + """Add common arguments to the given parser.""" |
| 22 | + prefix = 'sub_' if is_sub else '' |
| 23 | + parser.add_argument( |
| 24 | + '--database-url', |
| 25 | + dest=f'{prefix}database_url', |
| 26 | + help='Database URL to use for the migrations. If not set, the DATABASE_URL environment variable will be used.', |
| 27 | + ) |
| 28 | + parser.add_argument( |
| 29 | + '--tasks-table', |
| 30 | + dest=f'{prefix}tasks_table', |
| 31 | + help='Custom tasks table to update. If not set, the default is "tasks".', |
| 32 | + ) |
| 33 | + parser.add_argument( |
| 34 | + '--push-notification-configs-table', |
| 35 | + dest=f'{prefix}push_notification_configs_table', |
| 36 | + help='Custom push notification configs table to update. If not set, the default is "push_notification_configs".', |
| 37 | + ) |
| 38 | + parser.add_argument( |
| 39 | + '-v', |
| 40 | + '--verbose', |
| 41 | + dest=f'{prefix}verbose', |
| 42 | + help='Enable verbose output (sets sqlalchemy.engine logging to INFO)', |
| 43 | + action='store_true', |
| 44 | + ) |
| 45 | + parser.add_argument( |
| 46 | + '--sql', |
| 47 | + dest=f'{prefix}sql', |
| 48 | + help='Run migrations in sql mode (generate SQL instead of executing)', |
| 49 | + action='store_true', |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +def create_parser() -> argparse.ArgumentParser: |
| 54 | + """Create the argument parser for the migration tool.""" |
| 55 | + parser = argparse.ArgumentParser(description='A2A Database Migration Tool') |
| 56 | + |
| 57 | + # Global options |
| 58 | + parser.add_argument( |
| 59 | + '--add_columns_owner_last_updated-default-owner', |
| 60 | + dest='owner', |
| 61 | + help="Value for the 'owner' column (used in specific migrations). If not set defaults to 'unknown'", |
| 62 | + ) |
| 63 | + _add_shared_args(parser) |
| 64 | + |
| 65 | + subparsers = parser.add_subparsers(dest='cmd', help='Migration command') |
| 66 | + |
| 67 | + # Upgrade command |
| 68 | + up_parser = subparsers.add_parser( |
| 69 | + 'upgrade', help='Upgrade to a later version' |
| 70 | + ) |
| 71 | + up_parser.add_argument( |
| 72 | + 'revision', |
| 73 | + nargs='?', |
| 74 | + default='head', |
| 75 | + help='Revision target (default: head)', |
| 76 | + ) |
| 77 | + up_parser.add_argument( |
| 78 | + '--add_columns_owner_last_updated-default-owner', |
| 79 | + dest='sub_owner', |
| 80 | + help="Value for the 'owner' column (used in specific migrations). If not set defaults to 'legacy_v03_no_user_info'", |
| 81 | + ) |
| 82 | + _add_shared_args(up_parser, is_sub=True) |
| 83 | + |
| 84 | + # Downgrade command |
| 85 | + down_parser = subparsers.add_parser( |
| 86 | + 'downgrade', help='Revert to a previous version' |
| 87 | + ) |
| 88 | + down_parser.add_argument( |
| 89 | + 'revision', |
| 90 | + nargs='?', |
| 91 | + default='base', |
| 92 | + help='Revision target (e.g., -1, base or a specific ID)', |
| 93 | + ) |
| 94 | + _add_shared_args(down_parser, is_sub=True) |
| 95 | + |
| 96 | + return parser |
| 97 | + |
| 98 | + |
| 99 | +def run_migrations() -> None: |
| 100 | + """CLI tool to manage database migrations.""" |
| 101 | + # Configure logging to show INFO messages |
| 102 | + logging.basicConfig(level=logging.INFO, format='%(levelname)s %(message)s') |
| 103 | + |
| 104 | + parser = create_parser() |
| 105 | + args = parser.parse_args() |
| 106 | + |
| 107 | + # Default to upgrade head if no command is provided |
| 108 | + if not args.cmd: |
| 109 | + args.cmd = 'upgrade' |
| 110 | + args.revision = 'head' |
| 111 | + |
| 112 | + # Locate the bundled alembic.ini |
| 113 | + ini_path = files('a2a').joinpath('alembic.ini') |
| 114 | + cfg = Config(str(ini_path)) |
| 115 | + |
| 116 | + # Dynamically set the script location |
| 117 | + migrations_path = files('a2a').joinpath('migrations') |
| 118 | + cfg.set_main_option('script_location', str(migrations_path)) |
| 119 | + |
| 120 | + # Consolidate owner, db_url, tables, verbose and sql values |
| 121 | + owner = args.owner or getattr(args, 'sub_owner', None) |
| 122 | + db_url = args.database_url or getattr(args, 'sub_database_url', None) |
| 123 | + task_table = args.tasks_table or getattr(args, 'sub_tasks_table', None) |
| 124 | + push_notification_configs_table = ( |
| 125 | + args.push_notification_configs_table |
| 126 | + or getattr(args, 'sub_push_notification_configs_table', None) |
| 127 | + ) |
| 128 | + |
| 129 | + verbose = args.verbose or getattr(args, 'sub_verbose', False) |
| 130 | + sql = args.sql or getattr(args, 'sub_sql', False) |
| 131 | + |
| 132 | + # Pass custom arguments to the migration context |
| 133 | + if owner: |
| 134 | + cfg.set_main_option( |
| 135 | + 'add_columns_owner_last_updated_default_owner', owner |
| 136 | + ) |
| 137 | + if db_url: |
| 138 | + os.environ['DATABASE_URL'] = db_url |
| 139 | + if task_table: |
| 140 | + cfg.set_main_option('tasks_table', task_table) |
| 141 | + if push_notification_configs_table: |
| 142 | + cfg.set_main_option( |
| 143 | + 'push_notification_configs_table', push_notification_configs_table |
| 144 | + ) |
| 145 | + if verbose: |
| 146 | + cfg.set_main_option('verbose', 'true') |
| 147 | + |
| 148 | + # Execute the requested command |
| 149 | + if args.cmd == 'upgrade': |
| 150 | + logging.info('Upgrading database to %s', args.revision) |
| 151 | + command.upgrade(cfg, args.revision, sql=sql) |
| 152 | + elif args.cmd == 'downgrade': |
| 153 | + logging.info('Downgrading database to %s', args.revision) |
| 154 | + command.downgrade(cfg, args.revision, sql=sql) |
| 155 | + |
| 156 | + logging.info('Done.') |
0 commit comments