Skip to content

Commit

Permalink
fix: init folder now generates correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
cofin committed Aug 2, 2023
1 parent 065b636 commit 66f8f74
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 11 deletions.
33 changes: 23 additions & 10 deletions litestar/contrib/sqlalchemy/alembic/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ def __init__(
output_buffer: TextIO | None = None,
stdout: TextIO = sys.stdout,
cmd_opts: Namespace | None = None,
config_args: Mapping[str, Any] = ..., # type: ignore[assignment]
config_args: Mapping[str, Any] | None = None,
attributes: dict | None = None,
template_directory: Path | None = None,
) -> None:
self.template_directory = template_directory
if config_args is None:
config_args = {}
super().__init__(file_, ini_section, output_buffer, stdout, cmd_opts, config_args, attributes)

def get_template_directory(self) -> str:
Expand All @@ -46,9 +48,7 @@ def get_template_directory(self) -> str:
"""
if self.template_directory is not None:
return str(self.template_directory)

package_dir = Path(__file__).parent.resolve()
return str(Path(package_dir / "templates"))
return super().get_template_directory()


class AlembicSpannerImpl(DefaultImpl):
Expand All @@ -58,11 +58,13 @@ class AlembicSpannerImpl(DefaultImpl):


def get_alembic_command_config(
alembic_config: str | None = None, script_location: str = "migrations"
alembic_config: str | None = None, script_location: str = "migrations", template_directory: str | None = None
) -> AlembicCommandConfig:
kwargs = {}
if alembic_config:
kwargs.update({"file_": alembic_config})
if template_directory:
kwargs.update({"template_directory": template_directory})
alembic_cfg = AlembicCommandConfig(**kwargs) # type: ignore
alembic_cfg.set_main_option("script_location", script_location)
return alembic_cfg
Expand Down Expand Up @@ -173,7 +175,8 @@ def merge(
"""Merge two revisions together. Creates a new migration file."""
plugin = app.plugins.get(SQLAlchemyInitPlugin)
alembic_cfg = get_alembic_command_config(
alembic_config=plugin._alembic_config.alembic_config, script_location=plugin._alembic_config.script_location
alembic_config=plugin._alembic_config.alembic_config,
script_location=plugin._alembic_config.script_location,
)
migration_command.merge(
config=alembic_cfg, revisions=revisions, message=message, branch_label=branch_label, rev_id=rev_id
Expand Down Expand Up @@ -234,15 +237,25 @@ def init(
) -> None:
"""Initialize a new scripts directory."""
plugin = app.plugins.get(SQLAlchemyInitPlugin)
alembic_cfg = get_alembic_command_config(
alembic_config=plugin._alembic_config.alembic_config, script_location=plugin._alembic_config.script_location
)

template = "sync"
if isinstance(plugin._config, SQLAlchemyAsyncConfig):
template = "asyncio"
if multidb:
template = f"{template}-multidb"
migration_command.init(config=alembic_cfg, directory=directory, template=template, package=package)
if template_path is None:
template_path = f"{Path(__file__).parent}/templates"
alembic_cfg = get_alembic_command_config(
alembic_config=plugin._alembic_config.alembic_config,
script_location=plugin._alembic_config.script_location,
template_directory=template_path,
)
migration_command.init(
config=alembic_cfg,
directory=directory,
template=template,
package=package,
)


def list_templates(app: Litestar) -> None:
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Litestar Alembic Asyncio multidb Config
[alembic]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Litestar Alembic Asyncio Config
[alembic]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Litestar Alembic Sync multidb Config
[alembic]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Litestar Alembic Sync Config
[alembic]
2 changes: 1 addition & 1 deletion litestar/contrib/sqlalchemy/plugins/init/config/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class GenericAlembicConfig:
For details see: https://alembic.sqlalchemy.org/en/latest/api/config.html
"""

alembic_config: str | None = None
alembic_config: str = "alembic.ini"
"""A path to the Alembic configuration file such as ``alembic.ini``. If left unset, the default configuration
will be used.
"""
Expand Down

0 comments on commit 66f8f74

Please sign in to comment.