-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring for better separation of database package implementation
- Loading branch information
1 parent
5149255
commit 2c7d311
Showing
12 changed files
with
152 additions
and
121 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from . import base | ||
from . import mixins | ||
from . import session | ||
from __future__ import annotations | ||
|
||
from .db_context_manager import db_context_switcher | ||
from . import base, mixins, session | ||
from .db_context_manager import DBContextManager | ||
from .db_context_switcher import DBContextSwitcher |
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,32 @@ | ||
from __future__ import annotations | ||
|
||
from contextlib import contextmanager | ||
from typing import TYPE_CHECKING, Generator | ||
|
||
from kwik.database import DBContextManager | ||
from kwik.database.context_vars import db_conn_ctx_var | ||
|
||
if TYPE_CHECKING: | ||
from sqlalchemy.orm import Session | ||
|
||
|
||
@contextmanager | ||
def DBContextSwitcher() -> Generator[Session, None, None]: # noqa: N802 | ||
""" | ||
Context manager to switch to an alternate database. | ||
Example: | ||
with db_context_switcher(): | ||
# Do something with the alternate database. | ||
pass | ||
""" | ||
|
||
# Get the current database session from the context variable. | ||
prev_db_conn_ctx_var = db_conn_ctx_var.get() | ||
|
||
# Create a new database session. | ||
with DBContextManager() as db: | ||
yield db | ||
|
||
# Restore the previous database session in the context variable. | ||
db_conn_ctx_var.set(prev_db_conn_ctx_var) |
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,11 @@ | ||
from __future__ import annotations | ||
|
||
import kwik | ||
from sqlalchemy import create_engine | ||
|
||
engine = create_engine( | ||
url=kwik.settings.SQLALCHEMY_DATABASE_URI, | ||
pool_pre_ping=True, | ||
pool_size=kwik.settings.POSTGRES_MAX_CONNECTIONS // kwik.settings.BACKEND_WORKERS, | ||
max_overflow=0, | ||
) |
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,10 @@ | ||
from __future__ import annotations | ||
|
||
from kwik.database.engine import engine | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
SessionLocal = sessionmaker( | ||
autocommit=False, | ||
autoflush=False, | ||
bind=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
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 |
---|---|---|
@@ -1,17 +1,19 @@ | ||
import os | ||
from __future__ import annotations | ||
|
||
from typing import Callable | ||
|
||
import kwik | ||
from kwik.database import DBContextManager | ||
from kwik.database.base import Base | ||
|
||
|
||
def init_test_db(db_path: str, init_db: Callable, *args, **kwargs) -> None: | ||
# Create a temporary database | ||
if os.path.exists(db_path): | ||
os.remove(db_path) | ||
os.mknod(db_path) | ||
|
||
def init_test_db(init_db: Callable, *args, **kwargs) -> None: | ||
# Initialize the database | ||
with kwik.utils.tests.test_db(db_path=db_path, setup=False) as db: | ||
with DBContextManager() as db: | ||
Base.metadata.create_all(bind=db.get_bind()) | ||
init_db(*args, **kwargs) | ||
|
||
|
||
def drop_test_db() -> None: | ||
# Drop the database | ||
with DBContextManager() as db: | ||
Base.metadata.drop_all(bind=db.get_bind()) |
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 |
---|---|---|
@@ -1,13 +1,11 @@ | ||
from .files import store_file | ||
from __future__ import annotations | ||
|
||
from .emails import ( | ||
send_email, | ||
send_test_email, | ||
send_new_account_email, | ||
send_reset_password_email, | ||
send_test_email, | ||
) | ||
from .login import ( | ||
generate_password_reset_token, | ||
verify_password_reset_token, | ||
) | ||
from .files import store_file | ||
from .login import generate_password_reset_token, verify_password_reset_token | ||
from .query import sort_query | ||
from . import tests |
This file was deleted.
Oops, something went wrong.