1+ import dataclasses
2+ import typing as t
3+
14from piccolo .apps .migrations .commands .base import BaseMigrationManager
25from piccolo .apps .migrations .tables import Migration
36from piccolo .utils .printing import get_fixed_length_string
47
58
9+ @dataclasses .dataclass
10+ class MigrationStatus :
11+ app_name : str
12+ migration_id : str
13+ has_ran : bool
14+
15+
616class CheckMigrationManager (BaseMigrationManager ):
717 def __init__ (self , app_name : str ):
818 self .app_name = app_name
919 super ().__init__ ()
1020
11- async def run (self ):
12- print ("Listing migrations ..." )
13-
21+ async def get_migration_statuses (self ) -> t .List [MigrationStatus ]:
1422 # Make sure the migration table exists, otherwise we'll get an error.
1523 await self .create_migration_table ()
1624
17- print (
18- f'{ get_fixed_length_string ("APP NAME" )} | '
19- f'{ get_fixed_length_string ("MIGRATION_ID" )} | RAN'
20- )
25+ migration_statuses : t .List [MigrationStatus ] = []
2126
2227 app_modules = self .get_app_modules ()
2328
@@ -29,8 +34,6 @@ async def run(self):
2934 if (self .app_name != "all" ) and (self .app_name != app_name ):
3035 continue
3136
32- fixed_length_app_name = get_fixed_length_string (app_name )
33-
3437 migration_modules = self .get_migration_modules (
3538 app_config .migrations_folder_path
3639 )
@@ -44,11 +47,53 @@ async def run(self):
4447 )
4548 .run ()
4649 )
47- fixed_length_id = get_fixed_length_string (_id )
48- print (
49- f"{ fixed_length_app_name } | { fixed_length_id } | { has_ran } "
50+ migration_statuses .append (
51+ MigrationStatus (
52+ app_name = app_name , migration_id = _id , has_ran = has_ran
53+ )
5054 )
5155
56+ return migration_statuses
57+
58+ async def have_ran_count (self ) -> int :
59+ """
60+ :returns:
61+ The number of migrations which have been ran.
62+ """
63+ migration_statuses = await self .get_migration_statuses ()
64+ return len ([i for i in migration_statuses if i .has_ran ])
65+
66+ async def havent_ran_count (self ) -> int :
67+ """
68+ :returns:
69+ The number of migrations which haven't been ran.
70+ """
71+ migration_statuses = await self .get_migration_statuses ()
72+ return len ([i for i in migration_statuses if not i .has_ran ])
73+
74+ async def run (self ):
75+ """
76+ Prints out the migrations which have and haven't ran.
77+ """
78+ print ("Listing migrations ..." )
79+
80+ print (
81+ f'{ get_fixed_length_string ("APP NAME" )} | '
82+ f'{ get_fixed_length_string ("MIGRATION_ID" )} | RAN'
83+ )
84+
85+ migration_statuses = await self .get_migration_statuses ()
86+
87+ for migration_status in migration_statuses :
88+ fixed_length_app_name = get_fixed_length_string (
89+ migration_status .app_name
90+ )
91+ fixed_length_id = get_fixed_length_string (
92+ migration_status .migration_id
93+ )
94+ has_ran = migration_status .has_ran
95+ print (f"{ fixed_length_app_name } | { fixed_length_id } | { has_ran } " )
96+
5297
5398async def check (app_name : str = "all" ):
5499 """
0 commit comments