33import typing as t
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
811from piccolo .conf .apps import AppConfig , MigrationModule
912
1013
1114class ForwardsMigrationManager (BaseMigrationManager ):
12- def __init__ (
13- self ,
14- app_name : str ,
15- migration_id : str ,
16- fake : bool = False ,
17- * args ,
18- ** kwargs ,
19- ):
15+ def __init__ (self , app_name : str , migration_id : str , fake : bool = False ):
2016 self .app_name = app_name
2117 self .migration_id = migration_id
2218 self .fake = fake
2319 super ().__init__ ()
2420
25- async def run_migrations (self , app_config : AppConfig ) -> None :
21+ async def run_migrations (self , app_config : AppConfig ) -> MigrationResult :
2622 already_ran = await Migration .get_migrations_which_ran (
2723 app_name = self .app_name
2824 )
@@ -38,10 +34,11 @@ async def run_migrations(self, app_config: AppConfig) -> None:
3834 print (f"Haven't run = { havent_run } " )
3935
4036 if len (havent_run ) == 0 :
41- # Make sure a status of 0 is returned , as we don't want this
37+ # Make sure this still appears successful , as we don't want this
4238 # to appear as an error in automated scripts.
43- print ("No migrations left to run!" )
44- sys .exit (0 )
39+ message = "No migrations left to run!"
40+ print (message )
41+ return MigrationResult (success = True , message = message )
4542
4643 if self .migration_id == "all" :
4744 subset = havent_run
@@ -51,7 +48,9 @@ async def run_migrations(self, app_config: AppConfig) -> None:
5148 try :
5249 index = havent_run .index (self .migration_id )
5350 except ValueError :
54- sys .exit (f"{ self .migration_id } is unrecognised" )
51+ message = f"{ self .migration_id } is unrecognised"
52+ print (message , file = sys .stderr )
53+ return MigrationResult (success = False , message = message )
5554 else :
5655 subset = havent_run [: index + 1 ]
5756
@@ -71,13 +70,45 @@ async def run_migrations(self, app_config: AppConfig) -> None:
7170 Migration (name = _id , app_name = self .app_name )
7271 ).run ()
7372
74- async def run (self ):
73+ return MigrationResult (success = True , message = "Ran successfully" )
74+
75+ async def run (self ) -> MigrationResult :
7576 print ("Running migrations ..." )
7677 await self .create_migration_table ()
7778
7879 app_config = self .get_app_config (app_name = self .app_name )
7980
80- await self .run_migrations (app_config )
81+ return await self .run_migrations (app_config )
82+
83+
84+ async def run_forwards (
85+ app_name : str , migration_id : str = "all" , fake : bool = False
86+ ) -> MigrationResult :
87+ """
88+ Run the migrations. This function can be used to programatically run
89+ migrations - for example, in a unit test.
90+ """
91+ if app_name == "all" :
92+ sorted_app_names = BaseMigrationManager ().get_sorted_app_names ()
93+ for _app_name in sorted_app_names :
94+ print (f"\n Migrating { _app_name } " )
95+ print ("------------------------------------------------" )
96+ manager = ForwardsMigrationManager (
97+ app_name = _app_name , migration_id = "all" , fake = fake
98+ )
99+ response = await manager .run ()
100+ if not response .success :
101+ return response
102+
103+ else :
104+ manager = ForwardsMigrationManager (
105+ app_name = app_name , migration_id = migration_id , fake = fake
106+ )
107+ response = await manager .run ()
108+ if not response .success :
109+ return response
110+
111+ return MigrationResult (success = True )
81112
82113
83114async def forwards (
@@ -97,17 +128,9 @@ async def forwards(
97128 If set, will record the migrations as being run without actually
98129 running them.
99130 """
100- if app_name == "all" :
101- sorted_app_names = BaseMigrationManager ().get_sorted_app_names ()
102- for _app_name in sorted_app_names :
103- print (f"\n Migrating { _app_name } " )
104- print ("------------------------------------------------" )
105- manager = ForwardsMigrationManager (
106- app_name = _app_name , migration_id = "all" , fake = fake
107- )
108- await manager .run ()
109- else :
110- manager = ForwardsMigrationManager (
111- app_name = app_name , migration_id = migration_id , fake = fake
112- )
113- await manager .run ()
131+ response = await run_forwards (
132+ app_name = app_name , migration_id = migration_id , fake = fake
133+ )
134+
135+ if not response .success :
136+ sys .exit (1 )
0 commit comments