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

Class MigrationWriter

tortoise/migrations/writer.py:253–510  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

251
252
253class MigrationWriter:
254 def __init__(
255 self,
256 name: str,
257 app_label: str,
258 operations: Iterable[Operation],
259 *,
260 dependencies: list[tuple[str, str]] | None = None,
261 run_before: list[tuple[str, str]] | None = None,
262 replaces: list[tuple[str, str]] | None = None,
263 initial: bool | None = None,
264 migrations_module: str | None = None,
265 ) -> None:
266 self.name = name
267 self.app_label = app_label
268 self.operations = list(operations)
269 self.dependencies = dependencies or []
270 self.run_before = run_before or []
271 self.replaces = replaces or []
272 self.initial = initial
273 self.migrations_module = migrations_module
274
275 def path(self) -> Path:
276 if not self.migrations_module:
277 raise ValueError("migrations_module is required to resolve the output path")
278 return migrations_module_path(self.migrations_module) / f"{self.name}.py"
279
280 def write(self) -> Path:
281 path = self.path()
282 path.write_text(self.as_string(), encoding="utf-8")
283 return path
284
285 def as_string(self) -> str:
286 imports = ImportManager()
287 operations = []
288 for operation in self.operations:
289 operations.extend(self._format_operation(operation, imports, indent=" " * 8))
290
291 lines: list[str] = [
292 "from tortoise import migrations",
293 "from tortoise.migrations import operations as ops",
294 ]
295 extra_imports = imports.render()
296 if extra_imports:
297 lines.extend(extra_imports)
298 lines.extend(["", "class Migration(migrations.Migration):"])
299 blocks: list[list[str]] = []
300 if self.dependencies:
301 blocks.append([f" dependencies = {self.dependencies!r}"])
302 if self.run_before:
303 blocks.append([f" run_before = {self.run_before!r}"])
304 if self.replaces:
305 blocks.append([f" replaces = {self.replaces!r}"])
306 if self.initial is not None:
307 blocks.append([f" initial = {self.initial!r}"])
308 blocks.append([" operations = [", *operations, " ]"])
309 for idx, block in enumerate(blocks):
310 lines.extend(block)

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…