MCPcopy Index your code
hub / github.com/tortoise/tortoise-orm / sqlmigrate

Function sqlmigrate

tortoise/migrations/api/sqlmigrate.py:26–68  ·  view source on GitHub ↗

Collect the SQL statements for a single migration without executing them. Args: config: Tortoise ORM config dict or TortoiseConfig object. config_file: Path to a JSON/YAML config file for Tortoise ORM. app_label: The application label. migration_name: The migrati

(
    *,
    config: dict[str, Any] | TortoiseConfig | None = None,
    config_file: str | None = None,
    app_label: str,
    migration_name: str,
    backward: bool = False,
)

Source from the content-addressed store, hash-verified

24
25
26async def sqlmigrate(
27 *,
28 config: dict[str, Any] | TortoiseConfig | None = None,
29 config_file: str | None = None,
30 app_label: str,
31 migration_name: str,
32 backward: bool = False,
33) -> list[str]:
34 """Collect the SQL statements for a single migration without executing them.
35
36 Args:
37 config: Tortoise ORM config dict or TortoiseConfig object.
38 config_file: Path to a JSON/YAML config file for Tortoise ORM.
39 app_label: The application label.
40 migration_name: The migration name (exact or prefix match).
41 backward: If True, collect SQL for unapplying the migration.
42
43 Returns:
44 A list of SQL strings (including descriptive comment annotations).
45 """
46 if isinstance(config, TortoiseConfig):
47 config = config.to_dict()
48 if config_file:
49 config = Tortoise._get_config_from_config_file(config_file)
50 if not config:
51 raise ValueError("sqlmigrate requires a config or config_file")
52
53 await Tortoise.init(config=config, init_connections=False)
54
55 configured_apps = config.get("apps", {})
56 if app_label not in configured_apps:
57 raise ValueError(f"Unknown app label {app_label!r}")
58
59 app_config = configured_apps[app_label]
60 connection_name = app_config.get("default_connection", "default")
61 connection = get_connection(connection_name)
62
63 apps_config = {app_label: app_config}
64 executor = MigrationExecutor(connection, apps_config)
65 # Replace the recorder with a noop so build_graph() does not query the DB.
66 executor.loader.recorder = _NoopRecorder()
67
68 return await executor.collect_sql(app_label, migration_name, backward=backward)

Callers

nothing calls this directly

Calls 8

collect_sqlMethod · 0.95
get_connectionFunction · 0.90
MigrationExecutorClass · 0.90
_NoopRecorderClass · 0.70
to_dictMethod · 0.45
initMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…