Skip to content

Commit

Permalink
fix: add create_engine() method to SQLAlchemy configs.
Browse files Browse the repository at this point in the history
This PR fixes a break in backward compatibility where the `create_engine()`
method was removed from the SQLAlchemy config types in a minor version
increment.
  • Loading branch information
peterschutt authored and provinzkraut committed Sep 30, 2023
1 parent 42e4914 commit 98000ea
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
11 changes: 10 additions & 1 deletion litestar/contrib/sqlalchemy/plugins/init/config/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

from advanced_alchemy.config.asyncio import AlembicAsyncConfig, AsyncSessionConfig
from advanced_alchemy.extensions.litestar.plugins.init.config.asyncio import (
SQLAlchemyAsyncConfig,
SQLAlchemyAsyncConfig as _SQLAlchemyAsyncConfig,
)
from advanced_alchemy.extensions.litestar.plugins.init.config.asyncio import (
autocommit_before_send_handler,
default_before_send_handler,
)
from sqlalchemy.ext.asyncio import AsyncEngine

from litestar.contrib.sqlalchemy.plugins.init.config.compat import _CreateEngineMixin

__all__ = (
"SQLAlchemyAsyncConfig",
Expand All @@ -14,3 +19,7 @@
"default_before_send_handler",
"autocommit_before_send_handler",
)


class SQLAlchemyAsyncConfig(_SQLAlchemyAsyncConfig, _CreateEngineMixin[AsyncEngine]):
...
23 changes: 23 additions & 0 deletions litestar/contrib/sqlalchemy/plugins/init/config/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Generic, Protocol, TypeVar

from litestar.utils.deprecation import deprecated

if TYPE_CHECKING:
from sqlalchemy import Engine
from sqlalchemy.ext.asyncio import AsyncEngine


EngineT_co = TypeVar("EngineT_co", bound="Engine | AsyncEngine", covariant=True)


class HasGetEngine(Protocol[EngineT_co]):
def get_engine(self) -> EngineT_co:
...


class _CreateEngineMixin(Generic[EngineT_co]):
@deprecated(version="2.1.1", removal_in="3.0.0", alternative="get_engine()")
def create_engine(self: HasGetEngine[EngineT_co]) -> EngineT_co:
return self.get_engine()
11 changes: 10 additions & 1 deletion litestar/contrib/sqlalchemy/plugins/init/config/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

from advanced_alchemy.config.sync import AlembicSyncConfig, SyncSessionConfig
from advanced_alchemy.extensions.litestar.plugins.init.config.sync import (
SQLAlchemySyncConfig,
SQLAlchemySyncConfig as _SQLAlchemySyncConfig,
)
from advanced_alchemy.extensions.litestar.plugins.init.config.sync import (
autocommit_before_send_handler,
default_before_send_handler,
)
from sqlalchemy import Engine

from litestar.contrib.sqlalchemy.plugins.init.config.compat import _CreateEngineMixin

__all__ = (
"SQLAlchemySyncConfig",
Expand All @@ -14,3 +19,7 @@
"default_before_send_handler",
"autocommit_before_send_handler",
)


class SQLAlchemySyncConfig(_SQLAlchemySyncConfig, _CreateEngineMixin[Engine]):
...
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from __future__ import annotations

import pytest
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine

from litestar.contrib.sqlalchemy.plugins.init.config.asyncio import SQLAlchemyAsyncConfig


def test_create_engine_with_engine_instance() -> None:
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
config = SQLAlchemyAsyncConfig(engine_instance=engine)
with pytest.deprecated_call():
assert engine is config.create_engine()


def test_create_engine_with_connection_string() -> None:
config = SQLAlchemyAsyncConfig(connection_string="sqlite+aiosqlite:///:memory:")
with pytest.deprecated_call():
engine = config.create_engine()
assert isinstance(engine, AsyncEngine)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from __future__ import annotations

import pytest
from sqlalchemy import Engine, create_engine

from litestar.contrib.sqlalchemy.plugins.init.config.sync import SQLAlchemySyncConfig


def test_create_engine_with_engine_instance() -> None:
engine = create_engine("sqlite:///:memory:")
config = SQLAlchemySyncConfig(engine_instance=engine)
with pytest.deprecated_call():
assert engine is config.create_engine()


def test_create_engine_with_connection_string() -> None:
config = SQLAlchemySyncConfig(connection_string="sqlite:///:memory:")
with pytest.deprecated_call():
engine = config.create_engine()
assert isinstance(engine, Engine)

0 comments on commit 98000ea

Please sign in to comment.