11from __future__ import annotations
22import asyncio
3- import functools
43import importlib
54import os
65import sys
76import typing as t
87
98from piccolo .conf .apps import (
10- AppConfig ,
119 MigrationModule ,
12- PiccoloAppModule ,
13- AppRegistry ,
10+ Finder ,
1411)
1512from piccolo .apps .migrations .auto .migration_manager import MigrationManager
1613from piccolo .apps .migrations .auto .diffable_table import DiffableTable
1714from piccolo .apps .migrations .auto .schema_snapshot import SchemaSnapshot
1815from piccolo .apps .migrations .tables import Migration
1916
2017
21- class BaseMigrationManager :
18+ class BaseMigrationManager ( Finder ) :
2219 def create_migration_table (self ) -> bool :
2320 """
2421 Creates the migration table in the database. Returns True/False
@@ -29,90 +26,6 @@ def create_migration_table(self) -> bool:
2926 return True
3027 return False
3128
32- def _deduplicate (
33- self , config_modules : t .List [PiccoloAppModule ]
34- ) -> t .List [PiccoloAppModule ]:
35- """
36- Remove all duplicates - just leaving the first instance.
37- """
38- # Deduplicate, but preserve order - which is why set() isn't used.
39- return list (dict ([(c , None ) for c in config_modules ]).keys ())
40-
41- def _import_app_modules (
42- self , config_module_paths : t .List [str ]
43- ) -> t .List [PiccoloAppModule ]:
44- """
45- Import all piccolo_app.py modules, and all dependencies.
46- """
47- config_modules = []
48-
49- for config_module_path in config_module_paths :
50- try :
51- config_module = t .cast (
52- PiccoloAppModule ,
53- importlib .import_module (config_module_path ),
54- )
55- except ImportError :
56- raise Exception (f"Unable to import { config_module_path } " )
57- app_config : AppConfig = getattr (config_module , "APP_CONFIG" )
58- dependency_config_modules = self ._import_app_modules (
59- app_config .migration_dependencies
60- )
61- config_modules .extend (dependency_config_modules + [config_module ])
62-
63- return config_modules
64-
65- def get_app_registry (self ) -> AppRegistry :
66- try :
67- from piccolo_conf import APP_REGISTRY
68- except ImportError :
69- raise Exception (
70- "Unable to import APP_REGISTRY from piccolo_conf - make sure "
71- "it's in your path."
72- )
73- return APP_REGISTRY
74-
75- def get_app_modules (self ) -> t .List [PiccoloAppModule ]:
76- """
77- Returns the piccolo_app.py modules for each registered Piccolo app.
78- """
79- app_registry = self .get_app_registry ()
80- app_modules = self ._import_app_modules (app_registry .apps )
81-
82- # Now deduplicate any dependencies
83- app_modules = self ._deduplicate (app_modules )
84-
85- return app_modules
86-
87- def get_sorted_app_names (self ) -> t .List [str ]:
88- """
89- Sorts the app names using the migration dependencies, so dependencies
90- are before dependents in the list.
91- """
92- modules = self .get_app_modules ()
93- configs : t .List [AppConfig ] = [module .APP_CONFIG for module in modules ]
94-
95- def sort_app_configs (app_config_1 : AppConfig , app_config_2 : AppConfig ):
96- return (
97- app_config_1 in app_config_2 .migration_dependency_app_configs
98- )
99-
100- sorted_configs = sorted (
101- configs , key = functools .cmp_to_key (sort_app_configs )
102- )
103- return [i .app_name for i in sorted_configs ]
104-
105- def get_app_config (self , app_name : str ) -> AppConfig :
106- """
107- Returns an `AppConfig` for the given app name.
108- """
109- modules = self .get_app_modules ()
110- for module in modules :
111- app_config = module .APP_CONFIG
112- if app_config .app_name == app_name :
113- return app_config
114- raise ValueError (f"No app found with name { app_name } " )
115-
11629 def get_migration_modules (
11730 self , folder_path : str
11831 ) -> t .Dict [str , MigrationModule ]:
0 commit comments