]> git.ipfire.org Git - thirdparty/fastapi/sqlmodel.git/commitdiff
WIP feature/wrap-migrations
authorPatrick Arminio <patrick.arminio@gmail.com>
Mon, 24 Nov 2025 19:46:20 +0000 (19:46 +0000)
committerPatrick Arminio <patrick.arminio@gmail.com>
Mon, 24 Nov 2025 19:46:20 +0000 (19:46 +0000)
sqlmodel/cli/migrations.py
tests/test_cli/test_migrations.py

index 114d406faef80f2a11dc7873296d65a5759e6779..ef4fb184d6798ee5218ba9bfa286b4459d891c62 100644 (file)
@@ -17,8 +17,8 @@ except ImportError:
 migrations_app = typer.Typer()
 
 
-def get_models_path_from_config() -> str:
-    """Get the models path from pyproject.toml configuration."""
+def get_config_from_pyproject() -> dict:
+    """Load and return the [tool.sqlmodel] configuration from pyproject.toml."""
     pyproject_path = Path.cwd() / "pyproject.toml"
 
     if not pyproject_path.exists():
@@ -30,7 +30,7 @@ def get_models_path_from_config() -> str:
     with open(pyproject_path, "rb") as f:
         config = tomllib.load(f)
 
-    # Try to get models path from [tool.sqlmodel]
+    # Try to get [tool.sqlmodel] section
     if "tool" not in config or "sqlmodel" not in config["tool"]:
         raise ValueError(
             "No [tool.sqlmodel] section found in pyproject.toml. "
@@ -39,7 +39,12 @@ def get_models_path_from_config() -> str:
             "models = \"your.models.path\"\n"
         )
 
-    sqlmodel_config = config["tool"]["sqlmodel"]
+    return config["tool"]["sqlmodel"]
+
+
+def get_models_path_from_config() -> str:
+    """Get the models path from pyproject.toml configuration."""
+    sqlmodel_config = get_config_from_pyproject()
 
     if "models" not in sqlmodel_config:
         raise ValueError(
@@ -53,9 +58,25 @@ def get_models_path_from_config() -> str:
 
 
 def get_migrations_dir(migrations_path: Optional[str] = None) -> Path:
-    """Get the migrations directory path."""
+    """Get the migrations directory path.
+
+    Priority:
+    1. Explicit migrations_path parameter
+    2. migrations_path in [tool.sqlmodel] in pyproject.toml
+    3. Default to ./migrations
+    """
     if migrations_path:
         return Path(migrations_path)
+
+    # Try to get from config
+    try:
+        sqlmodel_config = get_config_from_pyproject()
+        if "migrations_path" in sqlmodel_config:
+            return Path(sqlmodel_config["migrations_path"])
+    except ValueError:
+        # No pyproject.toml or no [tool.sqlmodel] section, use default
+        pass
+
     return Path.cwd() / "migrations"
 
 
index 243e44afaa71e9aa59184bbe935c359ae05a015b..36da3a6d5c9ce9e480d4947ab771f4fb907beeeb 100644 (file)
@@ -45,9 +45,10 @@ def migration_env(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> MigrationT
     shutil.copy(model_source, models_file)
 
     # Create pyproject.toml with [tool.sqlmodel] configuration
-    pyproject_content = """\
+    pyproject_content = f"""\
 [tool.sqlmodel]
 models = "test_models.models"
+migrations_path = "{migrations_dir}"
 """
     (tmp_path / "pyproject.toml").write_text(pyproject_content)
 
@@ -74,8 +75,6 @@ def test_create_first_migration(migration_env: MigrationTestEnv):
             "create",
             "-m",
             "Initial migration",
-            "--path",
-            str(migration_env.migrations_dir),
         ],
     )
 
@@ -110,8 +109,6 @@ def test_running_migration_twice_only_generates_migration_once(
             "create",
             "-m",
             "Initial migration",
-            "--path",
-            str(migration_env.migrations_dir),
         ],
     )
 
@@ -124,8 +121,6 @@ def test_running_migration_twice_only_generates_migration_once(
         [
             "migrations",
             "migrate",
-            "--path",
-            str(migration_env.migrations_dir),
         ],
     )
 
@@ -139,8 +134,6 @@ def test_running_migration_twice_only_generates_migration_once(
             "create",
             "-m",
             "Initial migration",
-            "--path",
-            str(migration_env.migrations_dir),
         ],
     )
 
@@ -168,8 +161,6 @@ def test_cannot_create_migration_with_pending_migrations(
             "create",
             "-m",
             "Initial migration",
-            "--path",
-            str(migration_env.migrations_dir),
         ],
     )
 
@@ -184,8 +175,6 @@ def test_cannot_create_migration_with_pending_migrations(
             "create",
             "-m",
             "Second migration",
-            "--path",
-            str(migration_env.migrations_dir),
         ],
     )