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:
@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"
),
"""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)
@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"
),
"""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.",
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)
"create",
"-m",
"Initial migration",
- "--models",
- "test_models.models",
"--path",
str(migration_env.migrations_dir),
],
"create",
"-m",
"Initial migration",
- "--models",
- "test_models.models",
"--path",
str(migration_env.migrations_dir),
],
[
"migrations",
"migrate",
- "--models",
- "test_models.models",
"--path",
str(migration_env.migrations_dir),
],
"create",
"-m",
"Initial migration",
- "--models",
- "test_models.models",
"--path",
str(migration_env.migrations_dir),
],
"create",
"-m",
"Initial migration",
- "--models",
- "test_models.models",
"--path",
str(migration_env.migrations_dir),
],
"create",
"-m",
"Second migration",
- "--models",
- "test_models.models",
"--path",
str(migration_env.migrations_dir),
],