@@ -27,30 +27,26 @@ def migrate():
27
27
@click .option ("--migrations-dir" , help = "Directory containing schema migration files" )
28
28
def status (migrations_dir : Optional [str ]):
29
29
"""Show current schema migration status from files."""
30
+ dir_path = migrations_dir or os .path .join (
31
+ get_root_migrations_dir (), "schema-migrations"
32
+ )
33
+ migrator = SchemaMigrator (migrations_dir = dir_path )
34
+ status_info = run_async (migrator .status ())
30
35
31
- async def _status ():
32
- dir_path = migrations_dir or os .path .join (
33
- get_root_migrations_dir (), "schema-migrations"
34
- )
35
- migrator = SchemaMigrator (migrations_dir = dir_path )
36
- status_info = await migrator .status ()
37
-
38
- click .echo ("Schema Migration Status:" )
39
- click .echo (f" Total migrations: { status_info ['total_migrations' ]} " )
40
- click .echo (f" Applied: { status_info ['applied_count' ]} " )
41
- click .echo (f" Pending: { status_info ['pending_count' ]} " )
36
+ click .echo ("Schema Migration Status:" )
37
+ click .echo (f" Total migrations: { status_info ['total_migrations' ]} " )
38
+ click .echo (f" Applied: { status_info ['applied_count' ]} " )
39
+ click .echo (f" Pending: { status_info ['pending_count' ]} " )
42
40
43
- if status_info ["pending_migrations" ]:
44
- click .echo ("\n Pending migrations:" )
45
- for migration_id in status_info ["pending_migrations" ]:
46
- click .echo (f"- { migration_id } " )
41
+ if status_info ["pending_migrations" ]:
42
+ click .echo ("\n Pending migrations:" )
43
+ for migration_id in status_info ["pending_migrations" ]:
44
+ click .echo (f"- { migration_id } " )
47
45
48
- if status_info ["applied_migrations" ]:
49
- click .echo ("\n Applied migrations:" )
50
- for migration_id in status_info ["applied_migrations" ]:
51
- click .echo (f"- { migration_id } " )
52
-
53
- run_async (_status ())
46
+ if status_info ["applied_migrations" ]:
47
+ click .echo ("\n Applied migrations:" )
48
+ for migration_id in status_info ["applied_migrations" ]:
49
+ click .echo (f"- { migration_id } " )
54
50
55
51
56
52
@migrate .command ()
@@ -74,44 +70,40 @@ def run(
74
70
yes : bool ,
75
71
):
76
72
"""Run pending schema migrations from files."""
73
+ dir_path = migrations_dir or os .path .join (
74
+ get_root_migrations_dir (), "schema-migrations"
75
+ )
76
+
77
+ if not os .path .exists (dir_path ):
78
+ if yes or click .confirm (
79
+ f"Create schema migrations directory at '{ dir_path } '?"
80
+ ):
81
+ os .makedirs (dir_path , exist_ok = True )
82
+ else :
83
+ click .echo ("Aborted." )
84
+ return
77
85
78
- async def _run ():
79
- dir_path = migrations_dir or os .path .join (
80
- get_root_migrations_dir (), "schema-migrations"
81
- )
86
+ migrator = SchemaMigrator (migrations_dir = dir_path )
82
87
83
- if not os .path .exists (dir_path ):
84
- if yes or click .confirm (
85
- f"Create schema migrations directory at '{ dir_path } '?"
88
+ # Show list for confirmation
89
+ if not dry_run and not yes :
90
+ status_info = run_async (migrator .status ())
91
+ if status_info ["pending_migrations" ]:
92
+ listing = "\n " .join (
93
+ f"- { m } "
94
+ for m in status_info ["pending_migrations" ][
95
+ : (limit or len (status_info ["pending_migrations" ]))
96
+ ]
97
+ )
98
+ if not click .confirm (
99
+ f"Run { min (limit or len (status_info ['pending_migrations' ]), len (status_info ['pending_migrations' ]))} migration(s)?\n { listing } "
86
100
):
87
- os .makedirs (dir_path , exist_ok = True )
88
- else :
89
101
click .echo ("Aborted." )
90
102
return
91
103
92
- migrator = SchemaMigrator (migrations_dir = dir_path )
93
-
94
- # Show list for confirmation
95
- if not dry_run and not yes :
96
- status_info = await migrator .status ()
97
- if status_info ["pending_migrations" ]:
98
- listing = "\n " .join (
99
- f"- { m } "
100
- for m in status_info ["pending_migrations" ][
101
- : (limit or len (status_info ["pending_migrations" ]))
102
- ]
103
- )
104
- if not click .confirm (
105
- f"Run { min (limit or len (status_info ['pending_migrations' ]), len (status_info ['pending_migrations' ]))} migration(s)?\n { listing } "
106
- ):
107
- click .echo ("Aborted." )
108
- return
109
-
110
- count = await migrator .run (dry_run = dry_run , limit = limit , verbose = verbose )
111
- if verbose and not dry_run :
112
- click .echo (f"Successfully applied { count } migration(s)." )
113
-
114
- run_async (_run ())
104
+ count = run_async (migrator .run (dry_run = dry_run , limit = limit , verbose = verbose ))
105
+ if verbose and not dry_run :
106
+ click .echo (f"Successfully applied { count } migration(s)." )
115
107
116
108
117
109
@migrate .command ()
@@ -122,29 +114,25 @@ async def _run():
122
114
)
123
115
def create (name : str , migrations_dir : Optional [str ], yes : bool ):
124
116
"""Create a new schema migration snapshot file from current pending operations."""
125
-
126
- async def _create ():
127
- dir_path = migrations_dir or os .path .join (
128
- get_root_migrations_dir (), "schema-migrations"
129
- )
130
-
131
- if not os .path .exists (dir_path ):
132
- if yes or click .confirm (
133
- f"Create schema migrations directory at '{ dir_path } '?"
134
- ):
135
- os .makedirs (dir_path , exist_ok = True )
136
- else :
137
- click .echo ("Aborted." )
138
- return
139
-
140
- migrator = SchemaMigrator (migrations_dir = dir_path )
141
- filepath = await migrator .create_migration_file (name )
142
- if filepath :
143
- click .echo (f"Created migration: { filepath } " )
117
+ dir_path = migrations_dir or os .path .join (
118
+ get_root_migrations_dir (), "schema-migrations"
119
+ )
120
+
121
+ if not os .path .exists (dir_path ):
122
+ if yes or click .confirm (
123
+ f"Create schema migrations directory at '{ dir_path } '?"
124
+ ):
125
+ os .makedirs (dir_path , exist_ok = True )
144
126
else :
145
- click .echo ("No pending schema changes detected. Nothing to snapshot." )
127
+ click .echo ("Aborted." )
128
+ return
146
129
147
- run_async (_create ())
130
+ migrator = SchemaMigrator (migrations_dir = dir_path )
131
+ filepath = run_async (migrator .create_migration_file (name ))
132
+ if filepath :
133
+ click .echo (f"Created migration: { filepath } " )
134
+ else :
135
+ click .echo ("No pending schema changes detected. Nothing to snapshot." )
148
136
149
137
150
138
@migrate .command ()
@@ -168,38 +156,34 @@ def rollback(
168
156
yes : bool ,
169
157
):
170
158
"""Rollback a specific schema migration by ID."""
171
-
172
- async def _rollback ():
173
- dir_path = migrations_dir or os .path .join (
174
- get_root_migrations_dir (), "schema-migrations"
175
- )
176
-
177
- if not os .path .exists (dir_path ):
178
- if yes or click .confirm (
179
- f"Create schema migrations directory at '{ dir_path } '?"
180
- ):
181
- os .makedirs (dir_path , exist_ok = True )
182
- else :
183
- click .echo ("Aborted." )
184
- return
185
-
186
- migrator = SchemaMigrator (migrations_dir = dir_path )
187
-
188
- if not yes and not dry_run :
189
- if not click .confirm (f"Rollback migration '{ migration_id } '?" ):
190
- click .echo ("Aborted." )
191
- return
192
-
193
- success = await migrator .rollback (
194
- migration_id , dry_run = dry_run , verbose = verbose
195
- )
196
- if success :
197
- if verbose :
198
- click .echo (f"Successfully rolled back migration: { migration_id } " )
159
+ dir_path = migrations_dir or os .path .join (
160
+ get_root_migrations_dir (), "schema-migrations"
161
+ )
162
+
163
+ if not os .path .exists (dir_path ):
164
+ if yes or click .confirm (
165
+ f"Create schema migrations directory at '{ dir_path } '?"
166
+ ):
167
+ os .makedirs (dir_path , exist_ok = True )
199
168
else :
200
- click .echo (
201
- f"Migration '{ migration_id } ' does not support rollback or is not applied." ,
202
- err = True ,
203
- )
204
-
205
- run_async (_rollback ())
169
+ click .echo ("Aborted." )
170
+ return
171
+
172
+ migrator = SchemaMigrator (migrations_dir = dir_path )
173
+
174
+ if not yes and not dry_run :
175
+ if not click .confirm (f"Rollback migration '{ migration_id } '?" ):
176
+ click .echo ("Aborted." )
177
+ return
178
+
179
+ success = run_async (migrator .rollback (
180
+ migration_id , dry_run = dry_run , verbose = verbose
181
+ ))
182
+ if success :
183
+ if verbose :
184
+ click .echo (f"Successfully rolled back migration: { migration_id } " )
185
+ else :
186
+ click .echo (
187
+ f"Migration '{ migration_id } ' does not support rollback or is not applied." ,
188
+ err = True ,
189
+ )
0 commit comments