11from __future__ import annotations
2+ import os
23import sys
34
45from piccolo .apps .migrations .auto import MigrationManager
910
1011class BackwardsMigrationManager (BaseMigrationManager ):
1112 def __init__ (
12- self , app_name : str , migration_id : str , auto_agree : bool = False
13+ self ,
14+ app_name : str ,
15+ migration_id : str ,
16+ auto_agree : bool = False ,
17+ clean : bool = False ,
1318 ):
1419 self .migration_id = migration_id
1520 self .app_name = app_name
1621 self .auto_agree = auto_agree
22+ self .clean = clean
1723 super ().__init__ ()
1824
1925 def run (self ):
@@ -87,12 +93,19 @@ def run(self):
8793 Migration .delete ().where (
8894 Migration .name == migration_id
8995 ).run_sync ()
96+
97+ if self .clean :
98+ os .unlink (migration_module .__file__ )
99+
90100 else : # pragma: no cover
91101 sys .exit ("Not proceeding." )
92102
93103
94104def backwards (
95- app_name : str , migration_id : str = "1" , auto_agree : bool = False
105+ app_name : str ,
106+ migration_id : str = "1" ,
107+ auto_agree : bool = False ,
108+ clean : bool = False ,
96109):
97110 """
98111 Undo migrations up to a specific migration.
@@ -106,6 +119,9 @@ def backwards(
106119 value of '1' to undo the most recent migration.
107120 :param auto_agree:
108121 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.
109125
110126 """
111127 if app_name == "all" :
@@ -133,6 +149,9 @@ def backwards(
133149 manager .run ()
134150 else :
135151 manager = BackwardsMigrationManager (
136- app_name = app_name , migration_id = migration_id , auto_agree = auto_agree
152+ app_name = app_name ,
153+ migration_id = migration_id ,
154+ auto_agree = auto_agree ,
155+ clean = clean ,
137156 )
138157 manager .run ()
0 commit comments