Load projects and subprojects
public
Load projects and subprojects
| Changeset | e4ac89a9a2f0b03b5b85d3bfd716b74243eb8b88 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@gmail.com>, |
| Parents | e2ef4095244f |
| Children | |
| Pushlog | by timo via ssh |
modified src/mitch/project.py +37 −21
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | from datetime import date, datetime, time, timezone |
| 4 | 4 | from graphlib import TopologicalSorter |
| 5 | 5 | from pathlib import Path |
| 6 | -from typing import Any, Generator, Iterable, Literal | |
| 6 | +from typing import Any, Generator, Iterable, Literal, Self | |
| 7 | 7 | from weakref import WeakValueDictionary |
| 8 | 8 | |
| 9 | 9 | import typer |
| @@ -19,11 +19,12 @@ | ||
| 19 | 19 | def __init__(self, root_folder: Path | str = Path.cwd()) -> None: |
| 20 | 20 | self.path = Path(root_folder).resolve() |
| 21 | 21 | self.config = self._load_project_config() |
| 22 | + self.subprojects: dict[str, Project] = {} | |
| 22 | 23 | self.id = self.config["project"]["id"] |
| 23 | 24 | self._load_migrations() |
| 24 | 25 | |
| 25 | 26 | @classmethod |
| 26 | - def from_path(cls, path: Path) -> "Project": | |
| 27 | + def from_path(cls, path: Path) -> Self: | |
| 27 | 28 | search_path = Path(path).resolve() |
| 28 | 29 | if not search_path.is_dir(): |
| 29 | 30 | search_path = search_path.parent |
| @@ -61,25 +62,40 @@ | ||
| 61 | 62 | def _load_migrations(self): |
| 62 | 63 | # Fetch migrations |
| 63 | 64 | self.migrations: list[Migration] = [] |
| 64 | - for up_script in self.path.rglob("*.up.sql"): | |
| 65 | - stem = up_script.name.removesuffix(".up.sql") | |
| 66 | - down_script = Path(up_script.parent) / f"{stem}.down.sql" | |
| 67 | - | |
| 68 | - config_file = Path(up_script.parent) / f"{stem}.config.toml" | |
| 69 | - config = self._provide_config(config_file) | |
| 70 | - | |
| 71 | - migration = Migration( | |
| 72 | - id=config.get("id", stem), | |
| 73 | - up_script=up_script.resolve(), | |
| 74 | - down_script=down_script.resolve() if down_script.exists() else None, | |
| 75 | - config_file=config_file.resolve() if config_file.exists() else None, | |
| 76 | - transactional=config.get("transactional", True), | |
| 77 | - idempotent=config.get("idempotent", False), | |
| 78 | - created_at=self._created_at_from_config(config.get("created_at"), config_file), | |
| 79 | - dependencies={*config.get("dependencies", [])}, | |
| 80 | - project=self, | |
| 81 | - ) | |
| 82 | - self.migrations.append(migration) | |
| 65 | + | |
| 66 | + for current, dirs, files in self.path.walk(): | |
| 67 | + # Check for subprojects and skip their directories if found | |
| 68 | + dirs_with_subprojects = [d for d in dirs if (current / d / "mitch.toml").is_file()] | |
| 69 | + dirs[:] = [d for d in dirs if d not in dirs_with_subprojects] | |
| 70 | + | |
| 71 | + # Recursively load subprojects | |
| 72 | + for d in dirs_with_subprojects: | |
| 73 | + subproject = Project.from_path(current / d) | |
| 74 | + self.subprojects[subproject.id] = subproject | |
| 75 | + | |
| 76 | + for filename in files: | |
| 77 | + # Only consider files that end with .up.sql as migration scripts | |
| 78 | + if not filename.endswith(".up.sql"): | |
| 79 | + continue | |
| 80 | + | |
| 81 | + stem = filename.removesuffix(".up.sql") | |
| 82 | + up_script = self.path / current / filename | |
| 83 | + down_script = self.path / current / f"{stem}.down.sql" | |
| 84 | + config_file = self.path / current / f"{stem}.config.toml" | |
| 85 | + config = self._provide_config(config_file) | |
| 86 | + | |
| 87 | + migration = Migration( | |
| 88 | + id=config.get("id", stem), | |
| 89 | + up_script=up_script.resolve(), | |
| 90 | + down_script=down_script.resolve() if down_script.exists() else None, | |
| 91 | + config_file=config_file.resolve() if config_file.exists() else None, | |
| 92 | + transactional=config.get("transactional", True), | |
| 93 | + idempotent=config.get("idempotent", False), | |
| 94 | + created_at=self._created_at_from_config(config.get("created_at"), config_file), | |
| 95 | + dependencies={*config.get("dependencies", [])}, | |
| 96 | + project=self, | |
| 97 | + ) | |
| 98 | + self.migrations.append(migration) | |
| 83 | 99 | |
| 84 | 100 | # Index migrations by ID |
| 85 | 101 | self.migrations_by_id: WeakValueDictionary[str, Migration] = WeakValueDictionary( |