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

Method collect_sql

tortoise/migrations/executor.py:156–194  ·  view source on GitHub ↗

Collect SQL statements for a single migration without executing them. Args: app_label: The application label. migration_name: The migration name (exact or prefix match). backward: If True, collect SQL for unapplying the migration. Returns:

(
        self,
        app_label: str,
        migration_name: str,
        *,
        backward: bool = False,
    )

Source from the content-addressed store, hash-verified

154 return BaseSchemaEditor(self.connection, atomic=atomic, collect_sql=collect_sql)
155
156 async def collect_sql(
157 self,
158 app_label: str,
159 migration_name: str,
160 *,
161 backward: bool = False,
162 ) -> list[str]:
163 """Collect SQL statements for a single migration without executing them.
164
165 Args:
166 app_label: The application label.
167 migration_name: The migration name (exact or prefix match).
168 backward: If True, collect SQL for unapplying the migration.
169
170 Returns:
171 A list of SQL strings (including comment annotations).
172 """
173 await self.loader.build_graph()
174
175 # Resolve migration key — support prefix matching
176 key = self._resolve_migration_key(app_label, migration_name)
177 migration = self.loader.graph.nodes[key]
178 if not isinstance(migration, Migration):
179 raise ValueError(f"Missing migration for {key}")
180
181 # For SQL collection we don't need the real database — treat all
182 # migrations in the graph as "applied" so _project_state replays
183 # them up to the target.
184 all_keys = set(self.loader.graph.nodes.keys())
185 state = await self._project_state(all_keys, upto=key)
186
187 editor = self._schema_editor(atomic=False, collect_sql=True)
188
189 if backward:
190 await migration.unapply(state, schema_editor=editor, collect_sql=True)
191 else:
192 await migration.apply(state, schema_editor=editor, collect_sql=True)
193
194 return editor.collected_sql
195
196 def _resolve_migration_key(self, app_label: str, migration_name: str) -> MigrationKey:
197 """Resolve a migration name to a MigrationKey, supporting prefix matching."""

Callers 1

sqlmigrateFunction · 0.95

Calls 7

_project_stateMethod · 0.95
_schema_editorMethod · 0.95
build_graphMethod · 0.80
keysMethod · 0.80
unapplyMethod · 0.80
applyMethod · 0.80

Tested by

no test coverage detected