Skip to content

Commit

Permalink
fix: standardize on autocommit_include_redirect (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
cofin authored Jan 17, 2025
1 parent 4d4510e commit 1421e23
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
8 changes: 4 additions & 4 deletions advanced_alchemy/extensions/flask/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class SQLAlchemySyncConfig(_SQLAlchemySyncConfig):

app: Flask | None = None
"""The Flask application instance."""
commit_mode: Literal["manual", "autocommit", "autocommit_with_redirect"] = "manual"
commit_mode: Literal["manual", "autocommit", "autocommit_include_redirect"] = "manual"
"""The commit mode to use for database sessions."""

def create_session_maker(self) -> Callable[[], Session]:
Expand Down Expand Up @@ -130,7 +130,7 @@ def handle_db_session(response: Response) -> Response: # pyright: ignore[report
db_session = cast("Optional[Session]", g.pop(f"advanced_alchemy_session_{self.bind_key}", None))
if db_session is not None:
if (self.commit_mode == "autocommit" and 200 <= response.status_code < 300) or ( # noqa: PLR2004
self.commit_mode == "autocommit_with_redirect" and 200 <= response.status_code < 400 # noqa: PLR2004
self.commit_mode == "autocommit_include_redirect" and 200 <= response.status_code < 400 # noqa: PLR2004
):
db_session.commit()
db_session.close()
Expand Down Expand Up @@ -167,7 +167,7 @@ class SQLAlchemyAsyncConfig(_SQLAlchemyAsyncConfig):

app: Flask | None = None
"""The Flask application instance."""
commit_mode: Literal["manual", "autocommit", "autocommit_with_redirect"] = "manual"
commit_mode: Literal["manual", "autocommit", "autocommit_include_redirect"] = "manual"
"""The commit mode to use for database sessions."""

def create_session_maker(self) -> Callable[[], AsyncSession]:
Expand Down Expand Up @@ -224,7 +224,7 @@ def handle_db_session(response: Response) -> Response: # pyright: ignore[report
if db_session is not None:
p = getattr(db_session, "_session_portal", None) or portal
if (self.commit_mode == "autocommit" and 200 <= response.status_code < 300) or ( # noqa: PLR2004
self.commit_mode == "autocommit_with_redirect" and 200 <= response.status_code < 400 # noqa: PLR2004
self.commit_mode == "autocommit_include_redirect" and 200 <= response.status_code < 400 # noqa: PLR2004
):
_ = p.call(db_session.commit)
_ = p.call(db_session.close)
Expand Down
4 changes: 2 additions & 2 deletions docs/usage/frameworks/flask.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Both sync and async configurations support these options:
- Create tables on startup
- ``False``
* - ``commit_mode``
- ``"autocommit", "autocommit_with_redirect", "manual"``
- ``"autocommit", "autocommit_include_redirect", "manual"``
- Session commit behavior
- ``"manual"``

Expand All @@ -152,7 +152,7 @@ The ``commit_mode`` option controls how database sessions are committed:

- ``"manual"`` (default): No automatic commits
- ``"autocommit"``: Commit on successful responses (2xx status codes)
- ``"autocommit_with_redirect"``: Commit on successful responses and redirects (2xx and 3xx status codes)
- ``"autocommit_include_redirect"``: Commit on successful responses and redirects (2xx and 3xx status codes)

Services
--------
Expand Down
14 changes: 8 additions & 6 deletions tests/unit/test_extensions/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,14 @@ def test_route() -> tuple[dict[str, str], int]:
assert result.scalar_one().name == "test"


def test_sync_autocommit_with_redirect(setup_database: Path) -> None:
def test_sync_autocommit_include_redirect(setup_database: Path) -> None:
app = Flask(__name__)

with app.test_client() as client:
config = SQLAlchemySyncConfig(
connection_string=f"sqlite:///{setup_database}", commit_mode="autocommit_with_redirect", metadata=metadata
connection_string=f"sqlite:///{setup_database}",
commit_mode="autocommit_include_redirect",
metadata=metadata,
)

extension = AdvancedAlchemy(config, app)
Expand All @@ -298,7 +300,7 @@ def test_route() -> tuple[str, int, dict[str, str]]:
session.add(User(name="test_redirect"))
return "", 302, {"Location": "/redirected"}

# Test redirect response (should commit with AUTOCOMMIT_WITH_REDIRECT)
# Test redirect response (should commit with autocommit_include_redirect)
response = client.post("/test")
assert response.status_code == 302

Expand Down Expand Up @@ -366,13 +368,13 @@ def test_route() -> tuple[dict[str, str], int]:
extension.portal_provider.stop()


def test_async_autocommit_with_redirect(setup_database: Path) -> None:
def test_async_autocommit_include_redirect(setup_database: Path) -> None:
app = Flask(__name__)

with app.test_client() as client:
config = SQLAlchemyAsyncConfig(
connection_string=f"sqlite+aiosqlite:///{setup_database}",
commit_mode="autocommit_with_redirect",
commit_mode="autocommit_include_redirect",
metadata=metadata,
)
extension = AdvancedAlchemy(config, app)
Expand All @@ -385,7 +387,7 @@ def test_route() -> tuple[str, int, dict[str, str]]:
session.add(user)
return "", 302, {"Location": "/redirected"}

# Test redirect response (should commit with AUTOCOMMIT_WITH_REDIRECT)
# Test redirect response (should commit with autocommit_include_redirect)
response = client.post("/test")
assert response.status_code == 302
session = extension.get_session()
Expand Down

0 comments on commit 1421e23

Please sign in to comment.