From e6253170149e32b88cba07eb5834389e22b028b4 Mon Sep 17 00:00:00 2001 From: Patrick Arminio Date: Mon, 24 Nov 2025 19:46:20 +0000 Subject: [PATCH] WIP --- sqlmodel/cli/migrations.py | 31 ++++++++++++++++++++++++++----- tests/test_cli/test_migrations.py | 15 ++------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/sqlmodel/cli/migrations.py b/sqlmodel/cli/migrations.py index 114d406f..ef4fb184 100644 --- a/sqlmodel/cli/migrations.py +++ b/sqlmodel/cli/migrations.py @@ -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" diff --git a/tests/test_cli/test_migrations.py b/tests/test_cli/test_migrations.py index 243e44af..36da3a6d 100644 --- a/tests/test_cli/test_migrations.py +++ b/tests/test_cli/test_migrations.py @@ -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), ], )