macrostrat.database 3.5.0__tar.gz → 3.5.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/PKG-INFO +1 -1
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/__init__.py +1 -1
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/utils.py +21 -4
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/pyproject.toml +1 -1
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/mapper/__init__.py +0 -0
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/mapper/base.py +0 -0
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/mapper/cache.py +0 -0
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/mapper/utils.py +0 -0
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/postgresql.py +0 -0
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/transfer/__init__.py +0 -0
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/transfer/dump_database.py +0 -0
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/transfer/move_tables.py +0 -0
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/transfer/restore_database.py +0 -0
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/transfer/stream_utils.py +0 -0
- {macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/transfer/utils.py +0 -0
|
@@ -128,7 +128,7 @@ class Database(object):
|
|
|
128
128
|
Returns: Iterator of results from the query.
|
|
129
129
|
"""
|
|
130
130
|
params = self._setup_params(params, kwargs)
|
|
131
|
-
return
|
|
131
|
+
return run_sql(self.session, fn, params, **kwargs)
|
|
132
132
|
|
|
133
133
|
def run_query(self, sql, params=None, **kwargs):
|
|
134
134
|
"""Run a single query on the database object, returning the result.
|
|
@@ -5,11 +5,11 @@ from time import sleep
|
|
|
5
5
|
from typing import IO, Union
|
|
6
6
|
from warnings import warn
|
|
7
7
|
|
|
8
|
+
import psycopg2.errors
|
|
8
9
|
from click import echo, secho
|
|
9
10
|
from psycopg2.extensions import set_wait_callback
|
|
10
11
|
from psycopg2.extras import wait_select
|
|
11
12
|
from psycopg2.sql import SQL, Composable, Composed
|
|
12
|
-
import psycopg2.errors
|
|
13
13
|
from rich.console import Console
|
|
14
14
|
from sqlalchemy import MetaData, create_engine, text
|
|
15
15
|
from sqlalchemy.engine import Connection, Engine
|
|
@@ -18,7 +18,7 @@ from sqlalchemy.exc import (
|
|
|
18
18
|
InternalError,
|
|
19
19
|
InvalidRequestError,
|
|
20
20
|
ProgrammingError,
|
|
21
|
-
OperationalError
|
|
21
|
+
OperationalError,
|
|
22
22
|
)
|
|
23
23
|
from sqlalchemy.orm import sessionmaker
|
|
24
24
|
from sqlalchemy.schema import Table
|
|
@@ -232,6 +232,9 @@ def infer_has_server_binds(sql):
|
|
|
232
232
|
return "%s" in sql or search(r"%\(\w+\)s", sql)
|
|
233
233
|
|
|
234
234
|
|
|
235
|
+
_default_statement_filter = lambda sql_text, params: True
|
|
236
|
+
|
|
237
|
+
|
|
235
238
|
def _run_sql(connectable, sql, params=None, **kwargs):
|
|
236
239
|
"""
|
|
237
240
|
Internal function for running a query on a SQLAlchemy connectable,
|
|
@@ -247,6 +250,7 @@ def _run_sql(connectable, sql, params=None, **kwargs):
|
|
|
247
250
|
raise_errors = kwargs.pop("raise_errors", False)
|
|
248
251
|
has_server_binds = kwargs.pop("has_server_binds", None)
|
|
249
252
|
ensure_single_query = kwargs.pop("ensure_single_query", False)
|
|
253
|
+
statement_filter = kwargs.pop("statement_filter", _default_statement_filter)
|
|
250
254
|
|
|
251
255
|
if stop_on_error:
|
|
252
256
|
raise_errors = True
|
|
@@ -288,6 +292,11 @@ def _run_sql(connectable, sql, params=None, **kwargs):
|
|
|
288
292
|
if has_server_binds is None:
|
|
289
293
|
has_server_binds = infer_has_server_binds(sql_text)
|
|
290
294
|
|
|
295
|
+
should_run = statement_filter(sql_text, params)
|
|
296
|
+
if not should_run:
|
|
297
|
+
pretty_print(sql_text, dim=True, strikethrough=True)
|
|
298
|
+
continue
|
|
299
|
+
|
|
291
300
|
# This only does something for postgresql, but it's harmless to run it for other engines
|
|
292
301
|
set_wait_callback(wait_select)
|
|
293
302
|
|
|
@@ -325,7 +334,9 @@ def _run_sql(connectable, sql, params=None, **kwargs):
|
|
|
325
334
|
|
|
326
335
|
def _should_raise_query_error(err):
|
|
327
336
|
"""Determine if an error should be raised for a query or not."""
|
|
328
|
-
if not isinstance(
|
|
337
|
+
if not isinstance(
|
|
338
|
+
err, (ProgrammingError, IntegrityError, InternalError, OperationalError)
|
|
339
|
+
):
|
|
329
340
|
return True
|
|
330
341
|
|
|
331
342
|
orig_err = getattr(err, "orig", None)
|
|
@@ -336,7 +347,10 @@ def _should_raise_query_error(err):
|
|
|
336
347
|
# We might want to change this behavior in the future, or support more graceful handling of errors from other
|
|
337
348
|
# database backends.
|
|
338
349
|
# Ideally we could handle operational errors more gracefully
|
|
339
|
-
if
|
|
350
|
+
if (
|
|
351
|
+
isinstance(orig_err, psycopg2.errors.QueryCanceled)
|
|
352
|
+
or getattr(orig_err, "pgcode", None) == "57014"
|
|
353
|
+
):
|
|
340
354
|
return True
|
|
341
355
|
|
|
342
356
|
return False
|
|
@@ -444,6 +458,9 @@ def run_sql(*args, **kwargs):
|
|
|
444
458
|
returning a list after completion.
|
|
445
459
|
ensure_single_query : bool
|
|
446
460
|
If True, raise an error if multiple queries are passed when only one is expected.
|
|
461
|
+
statement_filter : Callable
|
|
462
|
+
A function that takes a SQL statement and parameters and returns True if the statement
|
|
463
|
+
should be run, and False if it should be skipped.
|
|
447
464
|
"""
|
|
448
465
|
res = _run_sql(*args, **kwargs)
|
|
449
466
|
if kwargs.pop("yield_results", False):
|
|
@@ -3,7 +3,7 @@ authors = ["Daven Quinn <dev@davenquinn.com>"]
|
|
|
3
3
|
description = "A SQLAlchemy-based database toolkit."
|
|
4
4
|
name = "macrostrat.database"
|
|
5
5
|
packages = [{ include = "macrostrat" }]
|
|
6
|
-
version = "3.5.
|
|
6
|
+
version = "3.5.1"
|
|
7
7
|
|
|
8
8
|
[tool.poetry.dependencies]
|
|
9
9
|
GeoAlchemy2 = "^0.15.2"
|
{macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/mapper/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/transfer/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/transfer/move_tables.py
RENAMED
|
File without changes
|
|
File without changes
|
{macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/transfer/stream_utils.py
RENAMED
|
File without changes
|
{macrostrat_database-3.5.0 → macrostrat_database-3.5.1}/macrostrat/database/transfer/utils.py
RENAMED
|
File without changes
|