Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/flask_sqlalchemy/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ def init_app(self, app: Flask) -> None:
for key, options in engine_options.items():
self._make_metadata(key)
options.setdefault("echo", echo)
options.setdefault("echo_pool", echo)
if "pool" not in options:
options.setdefault("echo_pool", echo)
self._apply_driver_defaults(options, app)
engines[key] = self._make_engine(key, options, app)

Expand Down Expand Up @@ -610,7 +611,8 @@ def _apply_driver_defaults(self, options: dict[str, t.Any], app: Flask) -> None:

if url.drivername in {"sqlite", "sqlite+pysqlite"}:
if url.database is None or url.database in {"", ":memory:"}:
options["poolclass"] = sa.pool.StaticPool
if "pool" not in options:
options["poolclass"] = sa.pool.StaticPool

if "connect_args" not in options:
options["connect_args"] = {}
Expand Down
10 changes: 10 additions & 0 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ def test_sqlite_driver_level_uri(app: Flask, model_class: t.Any) -> None:
assert os.path.exists(db_path[5:])


@pytest.mark.usefixtures("app_ctx")
def test_bind_with_pool_instance(app: Flask, model_class: t.Any) -> None:
import sqlite3

pool = sa.pool.StaticPool(creator=lambda: sqlite3.connect(":memory:"))
app.config["SQLALCHEMY_BINDS"] = {"a": {"url": "sqlite://", "pool": pool}}
db = SQLAlchemy(app, model_class=model_class)
assert db.engines["a"].pool is pool


@unittest.mock.patch.object(SQLAlchemy, "_make_engine", autospec=True)
def test_sqlite_memory_defaults(
make_engine: unittest.mock.Mock, app: Flask, model_class: t.Any
Expand Down
Loading