Skip to content

Commit

Permalink
incorporate PR feedback; create_table -> table_exists
Browse files Browse the repository at this point in the history
  • Loading branch information
jlgoolsbee committed Apr 24, 2024
1 parent e4c599d commit beb9f34
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
6 changes: 3 additions & 3 deletions docs/config_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ SqlAlchemy

Default: ``'sessions'``

.. py:data:: SESSION_SQLALCHEMY_CREATE_TABLE
.. py:data:: SESSION_SQLALCHEMY_TABLE_EXISTS
Whether (or not) Flask-Session should manage creation of the table for storing session data.
Whether (or not) the table for storing session data is managed by libraries (e.g. Flask-Migrate) or other means outside of Flask-Session. When set to ``True``, Flask-Session will not try to create the session table.

Default: ``True``
Default: ``False``

.. py:data:: SESSION_SQLALCHEMY_SEQUENCE
Expand Down
6 changes: 3 additions & 3 deletions src/flask_session/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def _get_interface(self, app):
SESSION_SQLALCHEMY_TABLE = config.get(
"SESSION_SQLALCHEMY_TABLE", Defaults.SESSION_SQLALCHEMY_TABLE
)
SESSION_SQLALCHEMY_CREATE_TABLE = config.get(
"SESSION_SQLALCHEMY_CREATE_TABLE", Defaults.SESSION_SQLALCHEMY_CREATE_TABLE
SESSION_SQLALCHEMY_TABLE_EXISTS = config.get(
"SESSION_SQLALCHEMY_TABLE_EXISTS", Defaults.SESSION_SQLALCHEMY_TABLE_EXISTS
)
SESSION_SQLALCHEMY_SEQUENCE = config.get(
"SESSION_SQLALCHEMY_SEQUENCE", Defaults.SESSION_SQLALCHEMY_SEQUENCE
Expand Down Expand Up @@ -185,7 +185,7 @@ def _get_interface(self, app):
**common_params,
client=SESSION_SQLALCHEMY,
table=SESSION_SQLALCHEMY_TABLE,
create_table=SESSION_SQLALCHEMY_CREATE_TABLE,
table_exists=SESSION_SQLALCHEMY_TABLE_EXISTS,
sequence=SESSION_SQLALCHEMY_SEQUENCE,
schema=SESSION_SQLALCHEMY_SCHEMA,
bind_key=SESSION_SQLALCHEMY_BIND_KEY,
Expand Down
2 changes: 1 addition & 1 deletion src/flask_session/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Defaults:
SESSION_SQLALCHEMY_SEQUENCE = None
SESSION_SQLALCHEMY_SCHEMA = None
SESSION_SQLALCHEMY_BIND_KEY = None
SESSION_SQLALCHEMY_CREATE_TABLE = True
SESSION_SQLALCHEMY_TABLE_EXISTS = False

# DynamoDB settings
SESSION_DYNAMODB = None
Expand Down
12 changes: 9 additions & 3 deletions src/flask_session/sqlalchemy/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Provides a Session Interface to SQLAlchemy"""

import warnings
from datetime import datetime
from datetime import timedelta as TimeDelta
Expand Down Expand Up @@ -54,11 +56,15 @@ class SqlAlchemySessionInterface(ServerSideSessionInterface):
:param sid_length: The length of the generated session id in bytes.
:param serialization_format: The serialization format to use for the session data.
:param table: The table name you want to use.
:param table_exists: Whether the session table is created/managed outside of Flask-Session (default=False).
:param sequence: The sequence to use for the primary key if needed.
:param schema: The db schema to use.
:param bind_key: The db bind key to use.
:param cleanup_n_requests: Delete expired sessions on average every N requests.
.. versionadded:: 0.9
The `table_exists` parameter was added.
.. versionadded:: 0.7
db changed to client to be standard on all session interfaces.
The `cleanup_n_request` parameter was added.
Expand Down Expand Up @@ -86,7 +92,7 @@ def __init__(
sequence: Optional[str] = Defaults.SESSION_SQLALCHEMY_SEQUENCE,
schema: Optional[str] = Defaults.SESSION_SQLALCHEMY_SCHEMA,
bind_key: Optional[str] = Defaults.SESSION_SQLALCHEMY_BIND_KEY,
create_table: bool = Defaults.SESSION_SQLALCHEMY_CREATE_TABLE,
table_exists: bool = Defaults.SESSION_SQLALCHEMY_TABLE_EXISTS,
cleanup_n_requests: Optional[int] = Defaults.SESSION_CLEANUP_N_REQUESTS,
):
self.app = app
Expand All @@ -104,9 +110,9 @@ def __init__(
self.sql_session_model = create_session_model(
client, table, schema, bind_key, sequence
)

# Optionally create the table if it does not exist
if create_table:
if not table_exists:
with app.app_context():
if bind_key:
engine = self.client.get_engine(app, bind=bind_key)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_database_not_created_automatically(self, app_utils):
{
"SESSION_TYPE": "sqlalchemy",
"SQLALCHEMY_DATABASE_URI": "sqlite:///",
"SESSION_SQLALCHEMY_CREATE_TABLE": False,
"SESSION_SQLALCHEMY_TABLE_EXISTS": True,
}
)
with app.app_context() and self.setup_sqlalchemy(
Expand Down

0 comments on commit beb9f34

Please sign in to comment.