From 30cf91963ef8fb4a23e8727794d662953f11205a Mon Sep 17 00:00:00 2001 From: Forest Gregg Date: Mon, 22 Apr 2024 13:03:58 -0400 Subject: [PATCH] drop sqlalchemy --- .../management/commands/aggregate_data.py | 43 ++++++------------- camp_fin/tests/test_unit.py | 18 ++++---- requirements.txt | 1 - 3 files changed, 22 insertions(+), 40 deletions(-) diff --git a/camp_fin/management/commands/aggregate_data.py b/camp_fin/management/commands/aggregate_data.py index a8a2a1c..b6a524d 100644 --- a/camp_fin/management/commands/aggregate_data.py +++ b/camp_fin/management/commands/aggregate_data.py @@ -1,21 +1,12 @@ -import sqlalchemy as sa -from django.conf import settings from django.core.management.base import BaseCommand - -DB_CONN = "postgresql://{USER}:{PASSWORD}@{HOST}:{PORT}/{NAME}" - -engine = sa.create_engine( - DB_CONN.format(**settings.DATABASES["default"]), - convert_unicode=True, - server_side_cursors=True, -) +from django.db import connections, transaction +from django.db.utils import ProgrammingError class Command(BaseCommand): help = "Import New Mexico Campaign Finance data" def handle(self, *args, **options): - self.connection = engine.connect() self.makeLoanBalanceView() self.makeTransactionAggregates() @@ -31,7 +22,7 @@ def makeTransactionAggregates(self): interval ) ) - except sa.exc.ProgrammingError: + except ProgrammingError: view = """ CREATE MATERIALIZED VIEW contributions_by_{0} AS ( SELECT @@ -78,7 +69,7 @@ def makeTransactionAggregates(self): interval ) ) - except sa.exc.ProgrammingError: + except ProgrammingError: view = """ CREATE MATERIALIZED VIEW expenditures_by_{0} AS ( SELECT @@ -135,7 +126,7 @@ def makeLoanBalanceView(self): """, raise_exc=True, ) - except sa.exc.ProgrammingError: + except ProgrammingError: self.executeTransaction( """ CREATE MATERIALIZED VIEW current_loan_status AS ( @@ -154,20 +145,10 @@ def makeLoanBalanceView(self): ) def executeTransaction(self, query, *args, **kwargs): - trans = self.connection.begin() - - raise_exc = kwargs.get("raise_exc", True) - - try: - self.connection.execute("SET local timezone to 'America/Denver'") - if kwargs: - self.connection.execute(query, **kwargs) - else: - self.connection.execute(query, *args) - trans.commit() - except sa.exc.ProgrammingError as e: - # TODO: Make some kind of logger - # logger.error(e, exc_info=True) - trans.rollback() - if raise_exc: - raise e + with connections["default"].cursor() as cursor: + with transaction.atomic(): + cursor.execute("SET local timezone to 'America/Denver'") + if kwargs: + cursor.execute(query, kwargs) + else: + cursor.execute(query, args) diff --git a/camp_fin/tests/test_unit.py b/camp_fin/tests/test_unit.py index 9d13caf..cee7a6a 100644 --- a/camp_fin/tests/test_unit.py +++ b/camp_fin/tests/test_unit.py @@ -176,11 +176,9 @@ def test_race_change_list_loads(self): colname = 'class="column-{field}"'.format(field=field) self.assertIn(colname, html) - table_list = html.split("