Public repository: timo / mitch

Reader

A migration stitcher (therefore mitch) for PostgreSQL, SQLite, MariaDB, and others

Load projects and subprojects

public

Load projects and subprojects
Changesete4ac89a9a2f0b03b5b85d3bfd716b74243eb8b88
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parentse2ef4095244f
Children
Pushlog
by timo via ssh

modified src/mitch/project.py +37 −21

@@ -3,7 +3,7 @@
33
 from datetime import date, datetime, time, timezone
44
 from graphlib import TopologicalSorter
55
 from pathlib import Path
6
-from typing import Any, Generator, Iterable, Literal
6
+from typing import Any, Generator, Iterable, Literal, Self
77
 from weakref import WeakValueDictionary
88
 
99
 import typer
@@ -19,11 +19,12 @@
1919
     def __init__(self, root_folder: Path | str = Path.cwd()) -> None:
2020
         self.path = Path(root_folder).resolve()
2121
         self.config = self._load_project_config()
22
+        self.subprojects: dict[str, Project] = {}
2223
         self.id = self.config["project"]["id"]
2324
         self._load_migrations()
2425
 
2526
     @classmethod
26
-    def from_path(cls, path: Path) -> "Project":
27
+    def from_path(cls, path: Path) -> Self:
2728
         search_path = Path(path).resolve()
2829
         if not search_path.is_dir():
2930
             search_path = search_path.parent
@@ -61,25 +62,40 @@
6162
     def _load_migrations(self):
6263
         # Fetch migrations
6364
         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)
8399
 
84100
         # Index migrations by ID
85101
         self.migrations_by_id: WeakValueDictionary[str, Migration] = WeakValueDictionary(