Skip to content

Commit 9b5944a

Browse files
authored
Merge pull request piccolo-orm#13 from piccolo-orm/fixtures
Fixtures
2 parents ec0a0a1 + 62cdd22 commit 9b5944a

File tree

15 files changed

+325
-192
lines changed

15 files changed

+325
-192
lines changed

piccolo/apps/app/commands/show_all.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1+
from piccolo.conf.apps import Finder
2+
3+
14
def show_all():
25
"""
36
Lists all registered Piccolo apps.
47
"""
5-
try:
6-
import piccolo_conf
7-
except ImportError:
8-
print("Unable to import piccolo_conf")
9-
10-
app_registry = getattr(piccolo_conf, "APP_REGISTRY")
8+
app_registry = Finder().get_app_registry()
119

1210
print("Registered apps:")
1311

piccolo/apps/migrations/commands/backwards.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class BackwardsMigrationManager(BaseMigrationManager):
99
def __init__(self, app_name: str, migration_id: str):
1010
self.migration_id = migration_id
1111
self.app_name = app_name
12+
super().__init__()
1213

1314
def run(self):
1415
app_modules = self.get_app_modules()

piccolo/apps/migrations/commands/base.py

Lines changed: 2 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
from __future__ import annotations
22
import asyncio
3-
import functools
43
import importlib
54
import os
65
import sys
76
import typing as t
87

98
from piccolo.conf.apps import (
10-
AppConfig,
119
MigrationModule,
12-
PiccoloAppModule,
13-
AppRegistry,
10+
Finder,
1411
)
1512
from piccolo.apps.migrations.auto.migration_manager import MigrationManager
1613
from piccolo.apps.migrations.auto.diffable_table import DiffableTable
1714
from piccolo.apps.migrations.auto.schema_snapshot import SchemaSnapshot
1815
from 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]:

piccolo/apps/migrations/commands/check.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class CheckMigrationManager(BaseMigrationManager):
77
def __init__(self, app_name: str):
88
self.app_name = app_name
9+
super().__init__()
910

1011
def run(self):
1112
print("Listing migrations ...")

piccolo/apps/migrations/commands/forwards.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(
2222
self.app_name = app_name
2323
self.migration_id = migration_id
2424
self.fake = fake
25+
super().__init__()
2526

2627
def run_migrations(self, app_config: AppConfig) -> None:
2728
already_ran = Migration.get_migrations_which_ran(

piccolo/apps/migrations/commands/new.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from .base import BaseMigrationManager
1313
from piccolo import __VERSION__
14-
from piccolo.conf.apps import AppConfig, AppRegistry
14+
from piccolo.conf.apps import AppConfig, Finder
1515
from piccolo.apps.migrations.auto import (
1616
AlterStatements,
1717
SchemaSnapshot,
@@ -149,26 +149,12 @@ def new(app_name: str, auto: bool = False):
149149
"""
150150
print("Creating new migration ...")
151151

152-
try:
153-
import piccolo_conf
154-
except ImportError:
155-
print("Can't find piccolo_conf")
156-
sys.exit(1)
157-
158-
if auto and isinstance(getattr(piccolo_conf, "DB"), SQLiteEngine):
152+
engine = Finder().get_engine()
153+
if auto and isinstance(engine, SQLiteEngine):
159154
print("Auto migrations aren't currently supported by SQLite.")
160155
sys.exit(1)
161156

162-
try:
163-
app_registry: AppRegistry = piccolo_conf.APP_REGISTRY
164-
except AttributeError:
165-
print("APP_REGISTRY isn't defined in piccolo_conf")
166-
sys.exit(1)
167-
168-
app_config = app_registry.get_app_config(app_name)
169-
170-
if not app_config:
171-
raise ValueError(f"Unrecognised app_name: {app_name}")
157+
app_config = Finder().get_app_config(app_name=app_name)
172158

173159
_create_migrations_folder(app_config.migrations_folder_path)
174160
_create_new_migration(app_config=app_config, auto=auto)

piccolo/apps/shell/commands/run.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
import typing as t
33

4-
from piccolo.conf.apps import AppRegistry, AppConfig
4+
from piccolo.conf.apps import AppRegistry, AppConfig, Finder
55
from piccolo.table import Table
66

77

@@ -28,13 +28,7 @@ def run():
2828
Runs an iPython shell, and automatically imports all of the Table classes
2929
from your project.
3030
"""
31-
try:
32-
import piccolo_conf
33-
except ImportError:
34-
print("Can't find piccolo_conf")
35-
sys.exit(1)
36-
37-
app_registry: AppRegistry = piccolo_conf.APP_REGISTRY
31+
app_registry: AppRegistry = Finder().get_app_registry()
3832

3933
tables = {}
4034
spacer = "-------"

0 commit comments

Comments
 (0)