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