1+ from __future__ import annotations
2+ import os
3+ import sys
4+
15from piccolo .apps .migrations .auto import MigrationManager
6+ from piccolo .apps .migrations .commands .base import BaseMigrationManager
27from piccolo .apps .migrations .tables import Migration
38from piccolo .utils .sync import run_sync
4- from .base import BaseMigrationManager
59
610
711class BackwardsMigrationManager (BaseMigrationManager ):
8- def __init__ (self , app_name : str , migration_id : str ):
12+ def __init__ (
13+ self ,
14+ app_name : str ,
15+ migration_id : str ,
16+ auto_agree : bool = False ,
17+ clean : bool = False ,
18+ ):
919 self .migration_id = migration_id
1020 self .app_name = app_name
21+ self .auto_agree = auto_agree
22+ self .clean = clean
1123 super ().__init__ ()
1224
1325 def run (self ):
26+ self .create_migration_table ()
27+
1428 app_modules = self .get_app_modules ()
1529
1630 migration_modules = []
@@ -27,8 +41,7 @@ def run(self):
2741 app_name = self .app_name
2842 )
2943 if len (ran_migration_ids ) == 0 :
30- print ("No migrations to reverse!" )
31- return
44+ sys .exit ("No migrations to reverse!" )
3245
3346 #######################################################################
3447
@@ -40,7 +53,7 @@ def run(self):
4053 earliest_migration_id = self .migration_id
4154
4255 if earliest_migration_id not in ran_migration_ids :
43- print (
56+ sys . exit (
4457 "Unrecognized migration name - must be one of "
4558 f"{ ran_migration_ids } "
4659 )
@@ -57,10 +70,14 @@ def run(self):
5770
5871 #######################################################################
5972
60- _continue = input (
61- "About to undo the following migrations:\n "
62- f"{ reversed_migration_ids } \n "
63- "Enter y to continue.\n "
73+ _continue = (
74+ "y"
75+ if self .auto_agree
76+ else input (
77+ "About to undo the following migrations:\n "
78+ f"{ reversed_migration_ids } \n "
79+ "Enter y to continue.\n "
80+ )
6481 )
6582 if _continue == "y" :
6683 print ("Undoing migrations" )
@@ -76,11 +93,20 @@ def run(self):
7693 Migration .delete ().where (
7794 Migration .name == migration_id
7895 ).run_sync ()
79- else :
80- print ("Not proceeding." )
8196
97+ if self .clean :
98+ os .unlink (migration_module .__file__ )
8299
83- def backwards (app_name : str , migration_id : str = "1" ):
100+ else : # pragma: no cover
101+ sys .exit ("Not proceeding." )
102+
103+
104+ def backwards (
105+ app_name : str ,
106+ migration_id : str = "1" ,
107+ auto_agree : bool = False ,
108+ clean : bool = False ,
109+ ):
84110 """
85111 Undo migrations up to a specific migration.
86112
@@ -91,26 +117,41 @@ def backwards(app_name: str, migration_id: str = "1"):
91117 Migrations will be reversed up to and including this migration_id.
92118 Specify a value of 'all' to undo all of the migrations. Specify a
93119 value of '1' to undo the most recent migration.
120+ :param auto_agree:
121+ Automatically agree to any input prompts.
122+ :param clean:
123+ If true, the migration files which have been run backwards are deleted
124+ from the disk after completing.
125+
94126 """
95127 if app_name == "all" :
96128 sorted_app_names = BaseMigrationManager ().get_sorted_app_names ()
97129 sorted_app_names .reverse ()
98130
99- _continue = input (
100- "You're about to undo the migrations for the following apps:\n "
101- f"{ sorted_app_names } \n "
102- "Are you sure you want to continue?\n "
103- "Enter y to continue.\n "
131+ _continue = (
132+ "y"
133+ if auto_agree
134+ else input (
135+ "You're about to undo the migrations for the following apps:\n "
136+ f"{ sorted_app_names } \n "
137+ "Are you sure you want to continue?\n "
138+ "Enter y to continue.\n "
139+ )
104140 )
105141 if _continue == "y" :
106142 for _app_name in sorted_app_names :
107143 print (f"Undoing { _app_name } " )
108144 manager = BackwardsMigrationManager (
109- app_name = _app_name , migration_id = "all"
145+ app_name = _app_name ,
146+ migration_id = "all" ,
147+ auto_agree = auto_agree ,
110148 )
111149 manager .run ()
112150 else :
113151 manager = BackwardsMigrationManager (
114- app_name = app_name , migration_id = migration_id
152+ app_name = app_name ,
153+ migration_id = migration_id ,
154+ auto_agree = auto_agree ,
155+ clean = clean ,
115156 )
116157 manager .run ()
0 commit comments