Remove --model

This commit is contained in:
Patrick Arminio
2025-10-16 21:21:48 +01:00
parent 8f72e4e0a5
commit 7c98f59a23
3 changed files with 62 additions and 22 deletions

View File

@@ -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]

View File

@@ -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.",

View File

@@ -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),
],