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

Update SQL queries to improve performance, fix alembic automigrations #68

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy import engine_from_config, pool

from alembic import context
from models import Base
from src.models import get_full_base

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand All @@ -19,7 +19,7 @@
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata
target_metadata = get_full_base().metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
Expand Down
2 changes: 1 addition & 1 deletion src/cr/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def get_latest_cr(db: Session) -> Cr:
stmt = select(Cr).order_by(Cr.creation_day.desc())
stmt = select(Cr).order_by(Cr.creation_day.desc()).limit(1)
result = db.execute(stmt).scalars().first()
return result

Expand Down
4 changes: 2 additions & 2 deletions src/diffs/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def get_cr_diff(db: Session, old_code: str | None, new_code: str | None) -> CrDi
stmt = stmt.order_by(CrDiff.creation_day.desc()).limit(1)
else:
if old_code:
stmt = stmt.where(src.set_code == old_code)
stmt = stmt.where(src.set_code == old_code).limit(1)
if new_code:
stmt = stmt.where(dst.set_code == new_code)
stmt = stmt.where(dst.set_code == new_code).limit(1)

return db.execute(stmt).scalars().first()

Expand Down
15 changes: 15 additions & 0 deletions src/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
from sqlalchemy.orm import declarative_base

Base = declarative_base()


# noinspection PyUnresolvedReferences


def get_full_base():
"""Imports all database models before returning the Base object. Used for alembic migrations"""

import src.cr.models # noqa: F401
import src.diffs.models # noqa: F401
import src.ipg.models # noqa: F401
import src.link.models # noqa: F401
import src.mtr.models # noqa: F401

return Base
2 changes: 1 addition & 1 deletion src/mtr/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def get_current_mtr(db: Session) -> Mtr:
stmt = select(Mtr).order_by(Mtr.creation_day.desc())
stmt = select(Mtr).order_by(Mtr.creation_day.desc()).limit(1)
result = db.execute(stmt).scalars().first()
return result

Expand Down
Loading