-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix migrations & initializations when using multiple workers (#336)
### Description This PR fixes migrations when working with multiple workers. It allows to run migrations and initializations in a synchronous way before lifespan. Thus, gunicorn can preload the app to run them before creating workers. ### Checklist - [ ] Created tests which fail without the change (if possible) - [x] All tests passing - [ ] Extended the documentation, if necessary --------- Co-authored-by: Petitoto <[email protected]>
- Loading branch information
Showing
7 changed files
with
154 additions
and
75 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
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,77 @@ | ||
from sqlalchemy import select | ||
from sqlalchemy.engine import Engine, create_engine | ||
from sqlalchemy.exc import IntegrityError | ||
from sqlalchemy.orm import Session, selectinload | ||
|
||
from app.core.config import Settings | ||
from app.models import models_core | ||
|
||
# These utils are used at startup to run database initializations & migrations | ||
|
||
|
||
def get_sync_db_engine(settings: Settings) -> Engine: | ||
""" | ||
Create a synchronous database engine | ||
""" | ||
if settings.SQLITE_DB: | ||
SQLALCHEMY_DATABASE_URL = f"sqlite:///./{settings.SQLITE_DB}" | ||
else: | ||
SQLALCHEMY_DATABASE_URL = f"postgresql://{settings.POSTGRES_USER}:{settings.POSTGRES_PASSWORD}@{settings.POSTGRES_HOST}/{settings.POSTGRES_DB}" | ||
|
||
engine = create_engine(SQLALCHEMY_DATABASE_URL, echo=settings.DATABASE_DEBUG) | ||
return engine | ||
|
||
|
||
def get_all_module_visibility_membership_sync( | ||
db: Session, | ||
): | ||
""" | ||
Return the every module with their visibility | ||
""" | ||
result = db.execute(select(models_core.ModuleVisibility)) | ||
return result.unique().scalars().all() | ||
|
||
|
||
def create_module_visibility_sync( | ||
module_visibility: models_core.ModuleVisibility, | ||
db: Session, | ||
) -> models_core.ModuleVisibility: | ||
""" | ||
Create a new module visibility in database and return it | ||
""" | ||
db.add(module_visibility) | ||
try: | ||
db.commit() | ||
return module_visibility | ||
except IntegrityError as error: | ||
db.rollback() | ||
raise ValueError(error) | ||
|
||
|
||
def get_group_by_id_sync(group_id: str, db: Session) -> models_core.CoreGroup | None: | ||
""" | ||
Return group with id from database | ||
""" | ||
result = db.execute( | ||
select(models_core.CoreGroup) | ||
.where(models_core.CoreGroup.id == group_id) | ||
.options( | ||
selectinload(models_core.CoreGroup.members) | ||
) # needed to load the members from the relationship | ||
) | ||
return result.scalars().first() | ||
|
||
|
||
def create_group_sync( | ||
group: models_core.CoreGroup, db: Session | ||
) -> models_core.CoreGroup: | ||
""" | ||
Create a new group in database and return it | ||
""" | ||
db.add(group) | ||
try: | ||
db.commit() | ||
return group | ||
except IntegrityError: | ||
db.rollback() | ||
raise |
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