Skip to content

Commit 53cb348

Browse files
authored
fix(alembic): support for alembic 1.16 toml_file configuration (#479)
Updates the AlembicCommand to use named arguments and support Alembic 1.16's `toml_file` configuration.
1 parent 735ff44 commit 53cb348

File tree

4 files changed

+706
-655
lines changed

4 files changed

+706
-655
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ repos:
2222
- id: unasyncd
2323
additional_dependencies: ["ruff"]
2424
- repo: https://github.com/charliermarsh/ruff-pre-commit
25-
rev: "v0.11.9"
25+
rev: "v0.11.11"
2626
hooks:
2727
# Run the linter.
2828
- id: ruff

advanced_alchemy/alembic/commands.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import inspect # Added import
12
import sys
23
from typing import TYPE_CHECKING, Any, Optional, TextIO, Union
34

45
from advanced_alchemy.config.asyncio import SQLAlchemyAsyncConfig
6+
from advanced_alchemy.exceptions import ImproperConfigurationError
57
from alembic import command as migration_command
68
from alembic.config import Config as _AlembicCommandConfig
79
from alembic.ddl.impl import DefaultImpl
@@ -39,6 +41,7 @@ def __init__(
3941
version_table_name: str,
4042
bind_key: "Optional[str]" = None,
4143
file_: "Union[str, os.PathLike[str], None]" = None,
44+
toml_file: "Union[str, os.PathLike[str], None]" = None,
4245
ini_section: str = "alembic",
4346
output_buffer: "Optional[TextIO]" = None,
4447
stdout: "TextIO" = sys.stdout,
@@ -57,7 +60,8 @@ def __init__(
5760
engine (sqlalchemy.engine.Engine | sqlalchemy.ext.asyncio.AsyncEngine): The SQLAlchemy engine instance.
5861
version_table_name (str): The name of the version table.
5962
bind_key (str | None): The bind key for the metadata.
60-
file_ (str | os.PathLike[str] | None): The file path for the alembic configuration.
63+
file_ (str | os.PathLike[str] | None): The file path for the alembic .ini configuration.
64+
toml_file (str | os.PathLike[str] | None): The file path for the alembic pyproject.toml configuration.
6165
ini_section (str): The ini section name.
6266
output_buffer (typing.TextIO | None): The output buffer for alembic commands.
6367
stdout (typing.TextIO): The standard output stream.
@@ -80,9 +84,33 @@ def __init__(
8084
self.compare_type = compare_type
8185
self.engine = engine
8286
self.db_url = engine.url.render_as_string(hide_password=False)
83-
if config_args is None:
84-
config_args = {}
85-
super().__init__(file_, ini_section, output_buffer, stdout, cmd_opts, config_args, attributes)
87+
88+
_config_args = {} if config_args is None else dict(config_args)
89+
90+
# Prepare kwargs for super().__init__
91+
super_init_kwargs: dict[str, Any] = {
92+
"file_": file_,
93+
"ini_section": ini_section,
94+
"output_buffer": output_buffer,
95+
"stdout": stdout,
96+
"cmd_opts": cmd_opts,
97+
"config_args": _config_args, # Pass the mutable copy
98+
"attributes": attributes,
99+
}
100+
101+
# Inspect the parent class __init__ for toml_file parameter
102+
parent_init_sig = inspect.signature(super().__init__)
103+
if "toml_file" in parent_init_sig.parameters:
104+
super_init_kwargs["toml_file"] = toml_file
105+
elif toml_file is not None:
106+
msg = (
107+
"The 'toml_file' parameter is not supported by your current Alembic version. "
108+
"Please upgrade Alembic to 1.16.0 or later to use this feature, "
109+
"or remove the 'toml_file' argument from AlembicCommandConfig."
110+
)
111+
raise ImproperConfigurationError(msg)
112+
113+
super().__init__(**super_init_kwargs)
86114

87115
def get_template_directory(self) -> str:
88116
"""Return the directory where Alembic setup templates are found.
@@ -337,6 +365,8 @@ def _get_alembic_command_config(self) -> "AlembicCommandConfig":
337365
AlembicCommandConfig: The configuration for Alembic commands.
338366
"""
339367
kwargs: dict[str, Any] = {}
368+
if self.sqlalchemy_config.alembic_config.toml_file:
369+
kwargs["toml_file"] = self.sqlalchemy_config.alembic_config.toml_file
340370
if self.sqlalchemy_config.alembic_config.script_config:
341371
kwargs["file_"] = self.sqlalchemy_config.alembic_config.script_config
342372
if self.sqlalchemy_config.alembic_config.template_path:

advanced_alchemy/config/common.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,10 @@ class GenericAlembicConfig:
302302
"""Configure the schema to use for the alembic revisions revisions.
303303
If unset, it defaults to connection's default schema."""
304304
script_location: str = "migrations"
305-
"""A path to save generated migrations.
305+
"""A path to save generated migrations."""
306+
toml_file: "Optional[str]" = None
307+
"""A path to the Alembic pyproject.toml configuration file.
308+
If left unset, the default configuration will be used.
306309
"""
307310
user_module_prefix: "Optional[str]" = "sa."
308311
"""User module prefix."""

0 commit comments

Comments
 (0)