-
Notifications
You must be signed in to change notification settings - Fork 0
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
🏭 Factories #8
🏭 Factories #8
Changes from 4 commits
fbe720b
3a89e2b
ce26c82
9652e05
f06e871
b70d093
4fd34b9
0534031
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__pycache__ | ||
|
||
# Development sqlite db | ||
chowda.sqlite | ||
*.sqlite |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from sqlalchemy import create_engine | ||
from .config import ENGINE_URI | ||
|
||
engine = create_engine(ENGINE_URI, connect_args={'check_same_thread': False}, echo=True) | ||
|
||
|
||
def create_async_engine(): | ||
from sqlmodel.ext.asyncio.session import AsyncEngine | ||
|
||
return AsyncEngine( | ||
create_engine(ENGINE_URI, connect_args={'check_same_thread': False}, echo=True) | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from chowda.db import engine | ||
from chowda.models import ( | ||
MediaFile, | ||
Collection, | ||
ClamsApp, | ||
User, | ||
MediaFileCollectionLink, | ||
MediaFileBatchLink, | ||
Pipeline, | ||
ClamsEvent, | ||
) | ||
from pydantic_factories import ( | ||
ModelFactory, | ||
SyncPersistenceProtocol, | ||
AsyncPersistenceProtocol, | ||
) | ||
from typing import TypeVar, List | ||
from pydantic import BaseModel | ||
from sqlmodel import Session | ||
from sqlmodel.ext.asyncio.session import AsyncSession | ||
|
||
T = TypeVar("T", bound=BaseModel) | ||
|
||
|
||
class SyncPersistenceHandler(SyncPersistenceProtocol[T]): | ||
def save(self, data: T) -> T: | ||
with Session(engine) as session: | ||
session.add(data) | ||
session.commit() | ||
|
||
def save_many(self, data: List[T]) -> List[T]: | ||
with Session(engine) as session: | ||
for d in data: | ||
session.add(d) | ||
session.commit() | ||
|
||
|
||
class AsyncPersistenceHandler(AsyncPersistenceProtocol[T]): | ||
async def save(self, data: T) -> T: | ||
async with AsyncSession(engine) as session: | ||
session.add(data) | ||
await session.commit() | ||
|
||
async def save_many(self, data: List[T]) -> List[T]: | ||
async with AsyncSession(engine) as session: | ||
for d in data: | ||
session.add(d) | ||
await session.commit() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shamelessly copied from fastapi/sqlmodel#161 (comment) Unfortunately, running it gives me:
But I think that's enough proof it works, so we should include it and we'll test it when we swap out for an async driver. |
||
|
||
|
||
class ChowdaFactory(ModelFactory): | ||
__allow_none_optionals__ = False | ||
__sync_persistence__ = SyncPersistenceHandler | ||
__async_persistence__ = AsyncPersistenceHandler | ||
|
||
|
||
class MediaFileFactory(ChowdaFactory): | ||
__model__ = MediaFile | ||
|
||
|
||
class CollectionFactory(ChowdaFactory): | ||
__model__ = Collection | ||
|
||
|
||
class ClamsAppFactory(ChowdaFactory): | ||
__model__ = ClamsApp | ||
|
||
|
||
class UserFactory(ChowdaFactory): | ||
__model__ = User | ||
|
||
|
||
class MediaFileCollectionLinkFactory(ChowdaFactory): | ||
__model__ = MediaFileCollectionLink | ||
|
||
|
||
class MediaFileBatchLinkFactory(ChowdaFactory): | ||
__model__ = MediaFileBatchLink | ||
|
||
|
||
class PipelineFactory(ChowdaFactory): | ||
__model__ = Pipeline | ||
|
||
|
||
class ClamsEventFactory(ChowdaFactory): | ||
__model__ = ClamsEvent |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ starlette-admin | |
uvicorn | ||
sqlmodel | ||
pydantic[email] | ||
pydantic-factories |
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.
Even creating the async engine throws the same
sqlalchemy.exc.InvalidRequestError
, hence the protection.