From 7c98f59a2377ed629d2dc4a57d3f6453799665ae Mon Sep 17 00:00:00 2001 From: Patrick Arminio Date: Thu, 16 Oct 2025 21:21:48 +0100 Subject: [PATCH] Remove --model --- pyproject.toml | 1 + sqlmodel/cli/migrations.py | 64 ++++++++++++++++++++++++++----- tests/test_cli/test_migrations.py | 19 ++++----- 3 files changed, 62 insertions(+), 22 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9c14dfdf..cbc3f798 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,6 +40,7 @@ dependencies = [ "pydantic >=1.10.13,<3.0.0", "typer >=0.9.0", "alembic >=1.13.0", + "tomli >=1.1.0 ; python_version < '3.11'", ] [project.scripts] diff --git a/sqlmodel/cli/migrations.py b/sqlmodel/cli/migrations.py index 3c5cc9fa..114d406f 100644 --- a/sqlmodel/cli/migrations.py +++ b/sqlmodel/cli/migrations.py @@ -9,9 +9,49 @@ from alembic.config import Config from alembic.runtime.migration import MigrationContext from sqlalchemy import create_engine, pool +try: + import tomllib +except ImportError: + import tomli as tomllib # type: ignore + migrations_app = typer.Typer() +def get_models_path_from_config() -> str: + """Get the models path from pyproject.toml configuration.""" + pyproject_path = Path.cwd() / "pyproject.toml" + + if not pyproject_path.exists(): + raise ValueError( + "Could not find pyproject.toml in the current directory. " + "Please create one with [tool.sqlmodel] section containing 'models = \"your.models.path\"'" + ) + + with open(pyproject_path, "rb") as f: + config = tomllib.load(f) + + # Try to get models path from [tool.sqlmodel] + if "tool" not in config or "sqlmodel" not in config["tool"]: + raise ValueError( + "No [tool.sqlmodel] section found in pyproject.toml. " + "Please add:\n\n" + "[tool.sqlmodel]\n" + "models = \"your.models.path\"\n" + ) + + sqlmodel_config = config["tool"]["sqlmodel"] + + if "models" not in sqlmodel_config: + raise ValueError( + "No 'models' key found in [tool.sqlmodel] section. " + "Please add:\n\n" + "[tool.sqlmodel]\n" + "models = \"your.models.path\"\n" + ) + + return sqlmodel_config["models"] + + def get_migrations_dir(migrations_path: Optional[str] = None) -> Path: """Get the migrations directory path.""" if migrations_path: @@ -115,11 +155,6 @@ def generate_migration_ops(db_url: str, metadata): @migrations_app.command() def create( message: str = typer.Option(..., "--message", "-m", help="Migration message"), - models: str = typer.Option( - ..., - "--models", - help="Python import path to models module (e.g., 'models' or 'app.models')", - ), migrations_path: Optional[str] = typer.Option( None, "--path", "-p", help="Path to migrations directory" ), @@ -127,6 +162,13 @@ def create( """Create a new migration with autogenerate.""" migrations_dir = get_migrations_dir(migrations_path) + # Get models path from pyproject.toml + try: + models = get_models_path_from_config() + except ValueError as e: + typer.echo(f"Error: {e}", err=True) + raise typer.Exit(1) + # Create migrations directory if it doesn't exist migrations_dir.mkdir(parents=True, exist_ok=True) @@ -418,11 +460,6 @@ def apply_migrations_programmatically( @migrations_app.command() def migrate( - models: str = typer.Option( - ..., - "--models", - help="Python import path to models module (e.g., 'models' or 'app.models')", - ), migrations_path: Optional[str] = typer.Option( None, "--path", "-p", help="Path to migrations directory" ), @@ -430,6 +467,13 @@ def migrate( """Apply all pending migrations to the database.""" migrations_dir = get_migrations_dir(migrations_path) + # Get models path from pyproject.toml + try: + models = get_models_path_from_config() + except ValueError as e: + typer.echo(f"Error: {e}", err=True) + raise typer.Exit(1) + if not migrations_dir.exists(): typer.echo( f"Error: {migrations_dir} not found. Run 'sqlmodel migrations init' first.", diff --git a/tests/test_cli/test_migrations.py b/tests/test_cli/test_migrations.py index 771d57c2..243e44af 100644 --- a/tests/test_cli/test_migrations.py +++ b/tests/test_cli/test_migrations.py @@ -44,6 +44,13 @@ 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 = """\ +[tool.sqlmodel] +models = "test_models.models" +""" + (tmp_path / "pyproject.toml").write_text(pyproject_content) + monkeypatch.setenv("DATABASE_URL", db_url) monkeypatch.chdir(tmp_path) @@ -67,8 +74,6 @@ def test_create_first_migration(migration_env: MigrationTestEnv): "create", "-m", "Initial migration", - "--models", - "test_models.models", "--path", str(migration_env.migrations_dir), ], @@ -105,8 +110,6 @@ def test_running_migration_twice_only_generates_migration_once( "create", "-m", "Initial migration", - "--models", - "test_models.models", "--path", str(migration_env.migrations_dir), ], @@ -121,8 +124,6 @@ def test_running_migration_twice_only_generates_migration_once( [ "migrations", "migrate", - "--models", - "test_models.models", "--path", str(migration_env.migrations_dir), ], @@ -138,8 +139,6 @@ def test_running_migration_twice_only_generates_migration_once( "create", "-m", "Initial migration", - "--models", - "test_models.models", "--path", str(migration_env.migrations_dir), ], @@ -169,8 +168,6 @@ def test_cannot_create_migration_with_pending_migrations( "create", "-m", "Initial migration", - "--models", - "test_models.models", "--path", str(migration_env.migrations_dir), ], @@ -187,8 +184,6 @@ def test_cannot_create_migration_with_pending_migrations( "create", "-m", "Second migration", - "--models", - "test_models.models", "--path", str(migration_env.migrations_dir), ],