1+ # src/rhosocial/activerecord/backend/impl/postgres/cli/named_migration.py
2+ """named-migration subcommand — PostgreSQL adapter for the shared CLI helper."""
3+
4+ from __future__ import annotations
5+
6+ from rhosocial .activerecord .backend .impl .postgres import PostgresBackend , AsyncPostgresBackend
7+
8+ from .connection import create_connection_parent_parser , resolve_connection_config_from_args
9+ from .output import create_provider
10+
11+
12+ def create_parser (subparsers ):
13+ """Create the named-migration subcommand parser.
14+
15+ Provides connection and output arguments via a parent parser.
16+ """
17+ from rhosocial .activerecord .backend .migration .cli import create_named_migration_parser
18+
19+ local_parent = create_connection_parent_parser ()
20+ nm_epilog = """Examples:
21+ # Apply a migration
22+ %(prog)s myapp.migrations.v001.CreateUsersTable --host localhost --database mydb --direction up
23+
24+ # Rollback
25+ %(prog)s myapp.migrations.v001.CreateUsersTable --host localhost --database mydb --direction down
26+
27+ # Apply with record store
28+ %(prog)s myapp.migrations.v001.CreateUsersTable --database mydb --record-store ./migrations.json
29+
30+ # Dry-run
31+ %(prog)s myapp.migrations.v001.CreateUsersTable --database mydb --direction up --dry-run
32+
33+ # Apply asynchronously
34+ %(prog)s myapp.migrations.v001.CreateUsersTable --database mydb --direction up --async
35+
36+ # Pass parameters to the migration
37+ %(prog)s myapp.migrations.v001.CreateUsersTable --param table_name=custom_table
38+
39+ # List available migrations
40+ %(prog)s myapp.migrations --list
41+
42+ # Describe a migration
43+ %(prog)s myapp.migrations.v001.CreateUsersTable --describe
44+
45+ # Batch: apply all pending migrations in a module
46+ %(prog)s myapp.migrations --all --record-store ./migrations.json
47+
48+ # Batch: dry-run all pending migrations
49+ %(prog)s myapp.migrations --all --record-store ./migrations.json --dry-run
50+
51+ # Batch: rollback all applied migrations
52+ %(prog)s myapp.migrations --all --record-store ./migrations.json --direction down
53+
54+ # Batch: wrap all migrations in a single transaction
55+ %(prog)s myapp.migrations --all --record-store ./migrations.json --single-transaction
56+
57+ End-to-end demo scripts with UP/DOWN round-trip verification:
58+ https://github.com/rhosocial/python-activerecord-postgres/tree/main/src/rhosocial/activerecord/backend/impl/postgres/examples/named_migrations
59+ PYTHONPATH=src bash demo_basic.sh
60+ PYTHONPATH=src bash demo_chain.sh
61+ PYTHONPATH=src bash demo_all.sh
62+ """
63+ return create_named_migration_parser (subparsers , local_parent , epilog = nm_epilog )
64+
65+
66+ def handle (args ):
67+ """Handle the named-migration subcommand."""
68+ from rhosocial .activerecord .backend .migration .cli import handle_named_migration as handle_nm
69+
70+ provider = create_provider (args .output , ascii_borders = args .rich_ascii )
71+
72+ backend = None
73+
74+ def backend_factory ():
75+ nonlocal backend
76+ config = resolve_connection_config_from_args (args )
77+ backend = PostgresBackend (connection_config = config )
78+ backend .connect ()
79+ backend .introspect_and_adapt ()
80+ return backend
81+
82+ def disconnect ():
83+ if backend and getattr (backend , "_connection" , None ):
84+ backend .disconnect ()
85+
86+ is_async = getattr (args , "is_async" , False )
87+ if is_async :
88+ async_backend = None
89+
90+ def backend_async_factory ():
91+ nonlocal async_backend
92+ config = resolve_connection_config_from_args (args )
93+ async_backend = AsyncPostgresBackend (connection_config = config )
94+ return async_backend
95+
96+ async def disconnect_async ():
97+ if async_backend and getattr (async_backend , "_connection" , None ):
98+ await async_backend .disconnect ()
99+
100+ handle_nm (
101+ args ,
102+ provider ,
103+ backend_factory = backend_factory ,
104+ disconnect = disconnect ,
105+ backend_async_factory = backend_async_factory ,
106+ disconnect_async = disconnect_async ,
107+ )
108+ return
109+
110+ handle_nm (
111+ args ,
112+ provider ,
113+ backend_factory = backend_factory ,
114+ disconnect = disconnect ,
115+ )
0 commit comments