def generate_migration(
name: str,
ops: Sequence[DiffOperation] | None = None,
migration_dir: str | Path = _DEFAULT_MIGRATION_DIR,
) -> Path:
migration = Migration(revision=_generate_revision(), name=name)
migration_file = Path(migration_dir) / migration.file_name
migration_file.parent.mkdir(parents=True, exist_ok=True)
if ops is not None:
content = _render_autogenerate_body(ops, migration)
else:
content = _get_template("migration").format(migration=migration)
migration_file.write_text(content)
return migration_file