-
-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add
create_engine()
method to SQLAlchemy configs. (#2382)
* fix: add `create_engine()` method to SQLAlchemy configs. 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. * fixes doc references.
- Loading branch information
1 parent
2d7ad67
commit 1b47eec
Showing
8 changed files
with
89 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
20 changes: 20 additions & 0 deletions
20
tests/unit/test_contrib/test_sqlalchemy/test_init_plugin/test_config/test_asyncio.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
20 changes: 20 additions & 0 deletions
20
tests/unit/test_contrib/test_sqlalchemy/test_init_plugin/test_config/test_sync.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |