(
ctx: CLIContext,
app_label: str | None,
migration_name: str | None,
backward: bool,
)
| 630 | |
| 631 | |
| 632 | async def sqlmigrate_cmd( |
| 633 | ctx: CLIContext, |
| 634 | app_label: str | None, |
| 635 | migration_name: str | None, |
| 636 | backward: bool, |
| 637 | ) -> None: |
| 638 | config = _load_config(ctx) |
| 639 | if not app_label or not migration_name: |
| 640 | labels = sorted(config.apps) if config.apps else [] |
| 641 | available = ", ".join(labels) if labels else "(none)" |
| 642 | if not app_label: |
| 643 | raise utils.CLIUsageError(f"app_label is required. Available app labels: {available}") |
| 644 | raise utils.CLIUsageError( |
| 645 | f"migration_name is required. Usage: sqlmigrate {app_label} <migration_name>" |
| 646 | ) |
| 647 | try: |
| 648 | statements = await sqlmigrate_api( |
| 649 | config=config, |
| 650 | app_label=app_label, |
| 651 | migration_name=migration_name, |
| 652 | backward=backward, |
| 653 | ) |
| 654 | except ValueError as exc: |
| 655 | raise utils.CLIError(str(exc)) from None |
| 656 | |
| 657 | if not statements: |
| 658 | print(f"{_DIM}-- (no SQL statements){_RESET}") |
| 659 | return |
| 660 | |
| 661 | config_dict = config.to_dict() |
| 662 | app_cfg = config_dict.get("apps", {}).get(app_label, {}) |
| 663 | connection_name = app_cfg.get("default_connection", "default") |
| 664 | connection_url = config_dict.get("connections", {}).get(connection_name, "") |
| 665 | if isinstance(connection_url, dict): |
| 666 | engine = connection_url.get("engine", "") |
| 667 | supports_transactional_ddl = "postgres" in engine or "psycopg" in engine |
| 668 | else: |
| 669 | supports_transactional_ddl = "postgres" in str(connection_url) or "psycopg" in str( |
| 670 | connection_url |
| 671 | ) |
| 672 | |
| 673 | wrap_in_transaction = supports_transactional_ddl |
| 674 | |
| 675 | if wrap_in_transaction: |
| 676 | print(f"{_DIM}BEGIN;{_RESET}") |
| 677 | |
| 678 | for statement in statements: |
| 679 | if statement.startswith("--"): |
| 680 | print(f"{_DIM}{statement}{_RESET}") |
| 681 | else: |
| 682 | if not statement.rstrip().endswith(";"): |
| 683 | print(f"{statement};") |
| 684 | else: |
| 685 | print(statement) |
| 686 | |
| 687 | if wrap_in_transaction: |
| 688 | print(f"{_DIM}COMMIT;{_RESET}") |
| 689 |
no test coverage detected
searching dependent graphs…