-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add piccolo db #104
Merged
Merged
Add piccolo db #104
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a88cb39
Add piccolo db
sayanarijit 9b02a99
Fix tests
sayanarijit 6290c5d
Optional db
sayanarijit 701bf99
Make engine non-optional
sayanarijit 1bfe683
Fix FastAPI transaction dep
sayanarijit 4d71169
Cleanup
sayanarijit 082d6d9
Added piccolo+fastapi test
1681cd2
Missing file
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
try: | ||
from apphelpers.db.piccolo import * # noqa: F401, F403 | ||
|
||
print("apphelpers: using Piccolo") | ||
except ImportError: | ||
from apphelpers.db.peewee import * # noqa: F401, F403 | ||
|
||
print("apphelpers: using Peewee") |
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,62 @@ | ||
from contextlib import asynccontextmanager | ||
from functools import wraps | ||
from typing import List, Set, Type | ||
|
||
from piccolo.engine.postgres import PostgresEngine | ||
from piccolo.table import Table, create_db_tables_sync, drop_db_tables_sync | ||
|
||
|
||
@asynccontextmanager | ||
async def connection_pool_lifespan(engine: PostgresEngine, **kwargs): | ||
print("db: starting connection pool") | ||
await engine.start_connection_pool(**kwargs) | ||
yield | ||
print("db: closing connection pool") | ||
await engine.close_connection_pool() | ||
|
||
|
||
dbtransaction_ctx = PostgresEngine.transaction | ||
|
||
|
||
def dbtransaction(engine: PostgresEngine, allow_nested=True): | ||
def wrapper(f): | ||
@wraps(f) | ||
async def f_wrapped(*args, **kw): | ||
async with dbtransaction_ctx(engine, allow_nested=allow_nested): | ||
return await f(*args, **kw) | ||
|
||
return f_wrapped | ||
|
||
return wrapper | ||
|
||
|
||
class BaseTable(Table): | ||
@classmethod | ||
def all_column_names(cls) -> Set[str]: | ||
return {col._meta.name for col in cls._meta.columns} | ||
|
||
|
||
def get_sub_tables(basetable: Type[Table]) -> List[Type[Table]]: | ||
tables = [] | ||
for subtable in basetable.__subclasses__(): | ||
tables.append(subtable) | ||
tables.extend(get_sub_tables(subtable)) | ||
return tables | ||
|
||
|
||
def setup_db(tables: List[Type[Table]]): | ||
create_db_tables_sync(*tables, if_not_exists=True) | ||
|
||
|
||
def setup_db_from_basetable(basetable: Type[Table]): | ||
tables = get_sub_tables(basetable) | ||
setup_db(tables) | ||
|
||
|
||
def destroy_db(tables: List[Type[Table]]): | ||
drop_db_tables_sync(*tables) | ||
|
||
|
||
def destroy_db_from_basetable(basetable: Type[Table]): | ||
tables = get_sub_tables(basetable) | ||
destroy_db(tables) |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import settings | ||
from piccolo import columns as col | ||
from piccolo.engine.postgres import PostgresEngine | ||
|
||
from apphelpers.db.piccolo import BaseTable | ||
|
||
db = PostgresEngine( | ||
config=dict( | ||
host=settings.DB_HOST, | ||
database=settings.DB_NAME, | ||
user=settings.DB_USER, | ||
password=settings.DB_PASS, | ||
) | ||
) | ||
|
||
|
||
class Book(BaseTable, db=db): | ||
name = col.Text() |
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 |
---|---|---|
|
@@ -29,3 +29,4 @@ types-pytz | |
types-redis | ||
redis | ||
loguru | ||
piccolo[postgres] |
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,70 @@ | ||
import asyncio | ||
|
||
import settings | ||
from piccolo import columns as col | ||
from piccolo.engine.postgres import PostgresEngine | ||
|
||
from apphelpers.db.piccolo import ( | ||
BaseTable, | ||
dbtransaction_ctx, | ||
destroy_db_from_basetable, | ||
setup_db_from_basetable, | ||
) | ||
|
||
db = PostgresEngine( | ||
config=dict( | ||
host=settings.DB_HOST, | ||
database=settings.DB_NAME, | ||
user=settings.DB_USER, | ||
password=settings.DB_PASS, | ||
) | ||
) | ||
|
||
|
||
class Book(BaseTable, db=db): | ||
name = col.Text() | ||
|
||
|
||
async def _add_book(name): | ||
await Book.insert(Book(name=name)).run() | ||
|
||
|
||
async def _add_book_loser(name): | ||
await _add_book(name) | ||
loser # will raise # noqa: F821 | ||
|
||
|
||
def setup_module(): | ||
setup_db_from_basetable(BaseTable) | ||
|
||
|
||
def teardown_module(): | ||
destroy_db_from_basetable(BaseTable) | ||
|
||
|
||
async def add_with_tr(): | ||
async with dbtransaction_ctx(db): | ||
name = "The Pillars of the Earth" | ||
await _add_book(name) | ||
names = [b[Book.name] for b in await Book.select().run()] | ||
assert name in names | ||
|
||
try: | ||
async with dbtransaction_ctx(db): | ||
name = "The Cathedral and the Bazaar" | ||
await _add_book_loser(name) | ||
except NameError: | ||
pass | ||
|
||
names = [b[Book.name] for b in await Book.select().run()] | ||
assert name not in names | ||
|
||
async with dbtransaction_ctx(db): | ||
name = "The Ego Trick" | ||
await _add_book(name) | ||
names = [b[Book.name] for b in await Book.select().run()] | ||
assert name in names | ||
|
||
|
||
def test_add_with_tr(): | ||
asyncio.run(add_with_tr()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We won't be using it in FastAPI. This is only for hug compatibility (and apphelpers tests).