From f08b9826c1c7c73579f31987e85e9f59bb4bb7be Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Fri, 15 Dec 2023 15:08:44 -0500 Subject: [PATCH 1/2] migrations: Upgrade to SQLAlchemy 2 --- .../385fb9a01efc_add_single_column_indexes_for_slow_.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/migrations/versions/385fb9a01efc_add_single_column_indexes_for_slow_.py b/migrations/versions/385fb9a01efc_add_single_column_indexes_for_slow_.py index 85801898..18e742e1 100644 --- a/migrations/versions/385fb9a01efc_add_single_column_indexes_for_slow_.py +++ b/migrations/versions/385fb9a01efc_add_single_column_indexes_for_slow_.py @@ -6,6 +6,7 @@ """ from alembic import op +from sqlalchemy import text # revision identifiers, used by Alembic. revision = "385fb9a01efc" @@ -17,7 +18,7 @@ def index_exists(name): connection = op.get_bind() result = connection.execute( - "SELECT exists(SELECT 1 from pg_indexes where indexname = '{}') as ix_exists;".format(name) + text("SELECT exists(SELECT 1 from pg_indexes where indexname = :indexname) as ix_exists"), {"indexname": name} ).first() return result.ix_exists From 04f931d9ab39b68041e0e9f38817f43aebb5391b Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Fri, 15 Dec 2023 15:15:38 -0500 Subject: [PATCH 2/2] fix: Statistic.created_at should have default value and should not change onupdate --- app/models.py | 4 +++- app/utils/statistics.py | 3 --- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/models.py b/app/models.py index e90c8e89..3d065124 100644 --- a/app/models.py +++ b/app/models.py @@ -758,5 +758,7 @@ class Statistic(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) type: StatisticType = Field(nullable=True) data: dict[str, Any] = Field(default={}, sa_column=Column(JSON)) - created_at: datetime | None = Field(sa_column=Column(DateTime(timezone=True), nullable=False, onupdate=func.now())) + created_at: datetime | None = Field( + sa_column=Column(DateTime(timezone=True), nullable=False, default=datetime.utcnow(), server_default=func.now()) + ) lender_id: int | None = Field(foreign_key="lender.id", nullable=True) diff --git a/app/utils/statistics.py b/app/utils/statistics.py index 571662d4..c3923541 100644 --- a/app/utils/statistics.py +++ b/app/utils/statistics.py @@ -70,7 +70,6 @@ def update_statistics(db_provider: Callable[[], Generator[Session, None, None]] statistic_kpi_data = Statistic( type=StatisticType.APPLICATION_KPIS, data=statistic_kpis, - created_at=datetime.now(), ) session.add(statistic_kpi_data) @@ -103,7 +102,6 @@ def update_statistics(db_provider: Callable[[], Generator[Session, None, None]] statistic_opt_data = Statistic( type=StatisticType.MSME_OPT_IN_STATISTICS, data=statistics_msme_opt_in, - created_at=datetime.now(), ) session.add(statistic_opt_data) @@ -133,7 +131,6 @@ def update_statistics(db_provider: Callable[[], Generator[Session, None, None]] type=StatisticType.APPLICATION_KPIS, data=statistic_kpis, lender_id=lender_id, - created_at=datetime.now(), ) session.add(statistic_kpi_data)