(
ctx: CLIContext, app_labels: tuple[str, ...], empty: bool, name: str | None
)
| 433 | |
| 434 | |
| 435 | async def makemigrations( |
| 436 | ctx: CLIContext, app_labels: tuple[str, ...], empty: bool, name: str | None |
| 437 | ) -> None: |
| 438 | if empty and not app_labels: |
| 439 | raise utils.CLIUsageError("--empty requires at least one APP_LABEL") |
| 440 | tortoise_config = _load_config(ctx) |
| 441 | apps_config = _select_apps(tortoise_config, app_labels or None) |
| 442 | |
| 443 | apps_dict = {label: app.to_dict() for label, app in apps_config.items()} |
| 444 | for label, app_config in apps_dict.items(): |
| 445 | migrations_module, _ = _ensure_migrations_package(label, app_config) |
| 446 | app_config["migrations"] = migrations_module |
| 447 | |
| 448 | config_dict = tortoise_config.to_dict() |
| 449 | config_dict["apps"] = apps_dict |
| 450 | |
| 451 | async with tortoise_cli_context(config_dict) as ctx: |
| 452 | if not ctx.apps: |
| 453 | raise utils.CLIError("Tortoise apps are not initialized") |
| 454 | autodetector = MigrationAutodetector(ctx.apps, apps_dict) |
| 455 | if empty: |
| 456 | await autodetector.loader.build_graph() |
| 457 | old_state = await autodetector._project_state() |
| 458 | new_state = autodetector._current_state() |
| 459 | writers = [] |
| 460 | for label, app_config in apps_dict.items(): |
| 461 | migrations_module_name = app_config.get("migrations") |
| 462 | if not isinstance(migrations_module_name, str): |
| 463 | continue |
| 464 | dependencies = sorted( |
| 465 | [(key.app_label, key.name) for key in autodetector._leaf_nodes(label)] |
| 466 | ) |
| 467 | migration_name, initial = autodetector._migration_name(label, old_state, new_state) |
| 468 | writers.append( |
| 469 | MigrationWriter( |
| 470 | migration_name, |
| 471 | label, |
| 472 | [], |
| 473 | dependencies=dependencies, |
| 474 | initial=initial, |
| 475 | migrations_module=migrations_module_name, |
| 476 | ) |
| 477 | ) |
| 478 | else: |
| 479 | writers = await autodetector.changes() |
| 480 | |
| 481 | if not writers: |
| 482 | print(f"{_DIM}No changes detected{_RESET}") |
| 483 | return |
| 484 | |
| 485 | for writer in writers: |
| 486 | if name: |
| 487 | try: |
| 488 | number = int(writer.name.split("_", 1)[0]) |
| 489 | except ValueError: |
| 490 | number = 1 |
| 491 | writer.name = format_migration_name(number, name) |
| 492 | path = writer.write() |
no test coverage detected
searching dependent graphs…