33import sys
44
55from piccolo .apps .migrations .auto import MigrationManager
6- from piccolo .apps .migrations .commands .base import BaseMigrationManager
6+ from piccolo .apps .migrations .commands .base import (
7+ BaseMigrationManager ,
8+ MigrationResult ,
9+ )
710from piccolo .apps .migrations .tables import Migration
811
912
@@ -21,12 +24,12 @@ def __init__(
2124 self .clean = clean
2225 super ().__init__ ()
2326
24- async def run (self ):
27+ async def run (self ) -> MigrationResult :
2528 await self .create_migration_table ()
2629
2730 app_modules = self .get_app_modules ()
2831
29- migration_modules = []
32+ migration_modules = {}
3033
3134 for app_module in app_modules :
3235 app_config = getattr (app_module , "APP_CONFIG" )
@@ -42,8 +45,9 @@ async def run(self):
4245 if len (ran_migration_ids ) == 0 :
4346 # Make sure a status of 0 is returned, as we don't want this
4447 # to appear as an error in automated scripts.
45- print ("No migrations to reverse!" )
46- sys .exit (0 )
48+ message = "No migrations to reverse!"
49+ print (message )
50+ return MigrationResult (success = True , message = message )
4751
4852 #######################################################################
4953
@@ -55,10 +59,12 @@ async def run(self):
5559 earliest_migration_id = self .migration_id
5660
5761 if earliest_migration_id not in ran_migration_ids :
58- sys . exit (
62+ message = (
5963 "Unrecognized migration name - must be one of "
6064 f"{ ran_migration_ids } "
6165 )
66+ print (message , file = sys .stderr )
67+ return MigrationResult (success = False , message = message )
6268
6369 #######################################################################
6470
@@ -99,33 +105,20 @@ async def run(self):
99105 if self .clean :
100106 os .unlink (migration_module .__file__ )
101107
108+ return MigrationResult (success = True )
109+
102110 else : # pragma: no cover
103- sys .exit ("Not proceeding." )
111+ message = "Not proceeding."
112+ print (message , file = sys .stderr )
113+ return MigrationResult (success = False , message = message )
104114
105115
106- async def backwards (
116+ async def run_backwards (
107117 app_name : str ,
108118 migration_id : str = "1" ,
109119 auto_agree : bool = False ,
110120 clean : bool = False ,
111- ):
112- """
113- Undo migrations up to a specific migration.
114-
115- :param app_name:
116- The app to reverse migrations for. Specify a value of 'all' to reverse
117- migrations for all apps.
118- :param migration_id:
119- Migrations will be reversed up to and including this migration_id.
120- Specify a value of 'all' to undo all of the migrations. Specify a
121- value of '1' to undo the most recent migration.
122- :param auto_agree:
123- If true, automatically agree to any input prompts.
124- :param clean:
125- If true, the migration files which have been run backwards are deleted
126- from the disk after completing.
127-
128- """
121+ ) -> MigrationResult :
129122 if app_name == "all" :
130123 sorted_app_names = BaseMigrationManager ().get_sorted_app_names ()
131124 sorted_app_names .reverse ()
@@ -149,11 +142,48 @@ async def backwards(
149142 auto_agree = auto_agree ,
150143 )
151144 await manager .run ()
145+ return MigrationResult (success = True )
146+ else :
147+ return MigrationResult (success = False , message = "User cancelled" )
152148 else :
153149 manager = BackwardsMigrationManager (
154150 app_name = app_name ,
155151 migration_id = migration_id ,
156152 auto_agree = auto_agree ,
157153 clean = clean ,
158154 )
159- await manager .run ()
155+ return await manager .run ()
156+
157+
158+ async def backwards (
159+ app_name : str ,
160+ migration_id : str = "1" ,
161+ auto_agree : bool = False ,
162+ clean : bool = False ,
163+ ):
164+ """
165+ Undo migrations up to a specific migration.
166+
167+ :param app_name:
168+ The app to reverse migrations for. Specify a value of 'all' to reverse
169+ migrations for all apps.
170+ :param migration_id:
171+ Migrations will be reversed up to and including this migration_id.
172+ Specify a value of 'all' to undo all of the migrations. Specify a
173+ value of '1' to undo the most recent migration.
174+ :param auto_agree:
175+ If true, automatically agree to any input prompts.
176+ :param clean:
177+ If true, the migration files which have been run backwards are deleted
178+ from the disk after completing.
179+
180+ """
181+ response = await run_backwards (
182+ app_name = app_name ,
183+ migration_id = migration_id ,
184+ auto_agree = auto_agree ,
185+ clean = clean ,
186+ )
187+
188+ if not response .success :
189+ sys .exit (1 )
0 commit comments