Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for table creation to be optional to allow for table/schema management by other tooling #239

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Add option to defer table creation; allows management of table and sc…
…hema by other tooling (like Flask-Migrate)
jlgoolsbee committed Apr 24, 2024
commit c76d50ea76654e5d6af45cbf70468cb0d077e30a
4 changes: 4 additions & 0 deletions src/flask_session/__init__.py
Original file line number Diff line number Diff line change
@@ -91,6 +91,9 @@ 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_SEQUENCE = config.get(
"SESSION_SQLALCHEMY_SEQUENCE", Defaults.SESSION_SQLALCHEMY_SEQUENCE
)
@@ -182,6 +185,7 @@ def _get_interface(self, app):
**common_params,
client=SESSION_SQLALCHEMY,
table=SESSION_SQLALCHEMY_TABLE,
create_table=SESSION_SQLALCHEMY_CREATE_TABLE,
sequence=SESSION_SQLALCHEMY_SEQUENCE,
schema=SESSION_SQLALCHEMY_SCHEMA,
bind_key=SESSION_SQLALCHEMY_BIND_KEY,
1 change: 1 addition & 0 deletions src/flask_session/defaults.py
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ class Defaults:
SESSION_SQLALCHEMY_SEQUENCE = None
SESSION_SQLALCHEMY_SCHEMA = None
SESSION_SQLALCHEMY_BIND_KEY = None
SESSION_SQLALCHEMY_CREATE_TABLE = True

# DynamoDB settings
SESSION_DYNAMODB = None
17 changes: 10 additions & 7 deletions src/flask_session/sqlalchemy/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -86,6 +86,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,
cleanup_n_requests: Optional[int] = Defaults.SESSION_CLEANUP_N_REQUESTS,
):
self.app = app
@@ -103,13 +104,15 @@ def __init__(
self.sql_session_model = create_session_model(
client, table, schema, bind_key, sequence
)
# Create the table if it does not exist
with app.app_context():
if bind_key:
engine = self.client.get_engine(app, bind=bind_key)
else:
engine = self.client.engine
self.sql_session_model.__table__.create(bind=engine, checkfirst=True)

# Optionally create the table if it does not exist
if create_table:
with app.app_context():
if bind_key:
engine = self.client.get_engine(app, bind=bind_key)
else:
engine = self.client.engine
self.sql_session_model.__table__.create(bind=engine, checkfirst=True)

super().__init__(
app,