MCPcopy
hub / github.com/tortoise/tortoise-orm / _ensure_migrations_package

Function _ensure_migrations_package

tortoise/cli/cli.py:215–263  ·  view source on GitHub ↗
(app_label: str, app_config: dict[str, Any])

Source from the content-addressed store, hash-verified

213
214
215def _ensure_migrations_package(app_label: str, app_config: dict[str, Any]) -> tuple[str, Path]:
216 migrations_module = app_config.get("migrations")
217 if not migrations_module:
218 migrations_module = utils.infer_migrations_module(app_config.get("models"))
219 if not migrations_module:
220 raise utils.CLIError(
221 f"Cannot infer migrations module for app {app_label}; set apps.{app_label}.migrations"
222 )
223
224 if "." not in migrations_module:
225 spec = importlib.util.find_spec(migrations_module)
226 if spec and spec.submodule_search_locations:
227 package_path = Path(next(iter(spec.submodule_search_locations)))
228 elif spec and spec.origin and spec.origin != "built-in":
229 raise utils.CLIError(
230 f"Migrations module {migrations_module} exists but is not a package"
231 )
232 else:
233 package_path = Path.cwd() / migrations_module
234 package_path.mkdir(parents=True, exist_ok=True)
235 init_path = package_path / "__init__.py"
236 if not init_path.exists():
237 init_path.write_text("", encoding="utf-8")
238 importlib.invalidate_caches()
239 return migrations_module, package_path
240
241 parent_module_name, package_name = migrations_module.rsplit(".", 1)
242 try:
243 parent_module = importlib.import_module(parent_module_name)
244 except ModuleNotFoundError as exc:
245 raise utils.CLIError(
246 f"Cannot import parent module {parent_module_name} for app {app_label}: {exc}"
247 ) from None
248
249 if hasattr(parent_module, "__path__"):
250 parent_path = Path(next(iter(parent_module.__path__)))
251 else:
252 module_file = getattr(parent_module, "__file__", None)
253 if not module_file:
254 raise utils.CLIError(f"Cannot resolve filesystem path for module {parent_module_name}")
255 parent_path = Path(module_file).parent
256
257 package_path = parent_path / package_name
258 package_path.mkdir(parents=True, exist_ok=True)
259 init_path = package_path / "__init__.py"
260 if not init_path.exists():
261 init_path.write_text("", encoding="utf-8")
262 importlib.invalidate_caches()
263 return migrations_module, package_path
264
265
266def _supports_color() -> bool:

Callers 2

initFunction · 0.85
makemigrationsFunction · 0.85

Calls 2

getMethod · 0.45
existsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…