Skip to content

Commit

Permalink
Merge pull request #12 from AnswerDotAI/remove-operational-error
Browse files Browse the repository at this point in the history
Remove OperationError
  • Loading branch information
jph00 authored Dec 25, 2024
2 parents fda2a36 + 8b79c1b commit 3d81d51
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 16 deletions.
12 changes: 6 additions & 6 deletions apswutils/db.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# This file is from sqlite-utils and copyright and license is the same as that project
__all__ = ['Database', 'Queryable', 'Table', 'View']

from .utils import chunks, hash_record, OperationalError, suggest_column_types, types_for_column_types, column_affinity, find_spatialite
from .utils import chunks, hash_record, suggest_column_types, types_for_column_types, column_affinity, find_spatialite
from collections import namedtuple
from collections.abc import Mapping
from typing import cast, Any, Callable, Dict, Generator, Iterable, Union, Optional, List, Tuple, Iterator
from functools import cache
import contextlib, datetime, decimal, inspect, itertools, json, os, pathlib, re, secrets, textwrap, binascii, uuid, logging
import apsw.ext, apsw.bestpractice
import apsw, apsw.ext, apsw.bestpractice

logger = logging.getLogger('apsw')
logger.setLevel(logging.ERROR)
Expand Down Expand Up @@ -684,7 +684,7 @@ def cached_counts(self, tables: Optional[Iterable[str]] = None) -> Dict[str, int
sql += " where [table] in ({})".format(", ".join("?" for table in tables))
try:
return {r[0]: r[1] for r in self.execute(sql, tables).fetchall()}
except OperationalError:
except apsw.Error:
return {}

def reset_counts(self):
Expand Down Expand Up @@ -2118,7 +2118,7 @@ def create_index(
try:
self.db.execute(sql)
break
except OperationalError as e:
except apsw.SQLError as e:
# find_unique_name=True - try again if 'index ... already exists'
arg = e.args[0]
if (
Expand Down Expand Up @@ -2769,7 +2769,7 @@ def update(

for row in cursor:
self.result.append(dict(zip(columns, row)))
except OperationalError as e:
except apsw.SQLError as e:
if alter and (" column" in e.args[0]):
# Attempt to add any missing columns, then try again
self.add_missing_columns([updates])
Expand Down Expand Up @@ -2934,7 +2934,7 @@ def insert_chunk(
except apsw.ExecutionCompleteError: continue
for row in cursor:
records.append(dict(zip(columns, row)))
except OperationalError as e:
except apsw.SQLError as e:
if alter and (" column" in e.args[0]):
# Attempt to add any missing columns, then try again
self.add_missing_columns(chunk)
Expand Down
4 changes: 0 additions & 4 deletions apswutils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
from typing import Dict, cast, BinaryIO, Iterable, Optional, Tuple, Type
import apsw

# TODO: Replace use of OperationalError with more explicit apsw errors
# In order to keep this PR minimal, we use OperationalError as a shim for apsw.Error
OperationalError = apsw.Error

SPATIALITE_PATHS = (
"/usr/lib/x86_64-linux-gnu/mod_spatialite.so",
"/usr/lib/aarch64-linux-gnu/mod_spatialite.so",
Expand Down
5 changes: 2 additions & 3 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
DescIndex,
AlterError,
NoObviousTable,
OperationalError,
ForeignKey,
Table,
View,
Expand Down Expand Up @@ -717,7 +716,7 @@ def test_columns_not_in_first_record_should_not_cause_batch_to_be_too_large(fres
fresh_db["too_many_columns"].insert_all(
records, alter=True, batch_size=batch_size
)
except OperationalError:
except apsw.SQLError:
raise


Expand Down Expand Up @@ -817,7 +816,7 @@ def test_create_index_find_unique_name(fresh_db):
table.insert({"id": 1})
table.create_index(["id"])
# Without find_unique_name should error
with pytest.raises(OperationalError, match="index idx_t_id already exists"):
with pytest.raises(apsw.SQLError, match="index idx_t_id already exists"):
table.create_index(["id"])
# With find_unique_name=True it should work
table.create_index(["id"], find_unique_name=True)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_create_view.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from apswutils.utils import OperationalError
import apsw


def test_create_view(fresh_db):
Expand All @@ -10,7 +10,7 @@ def test_create_view(fresh_db):

def test_create_view_error(fresh_db):
fresh_db.create_view("bar", "select 1 + 1")
with pytest.raises(OperationalError):
with pytest.raises(apsw.SQLError):
fresh_db.create_view("bar", "select 1 + 2")


Expand Down
1 change: 0 additions & 1 deletion tests/test_transform.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from apswutils.db import ForeignKey
from apswutils.utils import OperationalError
import pytest
import apsw
from collections.abc import Mapping
Expand Down

0 comments on commit 3d81d51

Please sign in to comment.