macrostrat.database 3.1.0__tar.gz → 3.1.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: macrostrat.database
3
- Version: 3.1.0
3
+ Version: 3.1.2
4
4
  Summary: A SQLAlchemy-based database toolkit.
5
5
  Author: Daven Quinn
6
6
  Author-email: dev@davenquinn.com
@@ -60,20 +60,6 @@ def prefix_inserts(insert, compiler, **kw):
60
60
  return compiler.visit_insert(insert, **kw)
61
61
 
62
62
 
63
- _psycopg2_setup_was_run = ContextVar("psycopg2-setup-was-run", default=False)
64
-
65
-
66
- def _setup_psycopg2_wait_callback():
67
- """Set up the wait callback for PostgreSQL connections. This allows for query cancellation with Ctrl-C."""
68
- # TODO: we might want to do this only once on engine creation
69
- # https://github.com/psycopg/psycopg2/issues/333
70
- val = _psycopg2_setup_was_run.get()
71
- if val:
72
- return
73
- psycopg2.extensions.set_wait_callback(psycopg2.extras.wait_select)
74
- _psycopg2_setup_was_run.set(True)
75
-
76
-
77
63
  def table_exists(db: Database, table_name: str, schema: str = "public") -> bool:
78
64
  """Check if a table exists in a PostgreSQL database."""
79
65
  sql = """SELECT EXISTS (
@@ -6,6 +6,8 @@ from typing import IO, Union
6
6
  from warnings import warn
7
7
 
8
8
  from click import echo, secho
9
+ from psycopg2.extensions import set_wait_callback
10
+ from psycopg2.extras import wait_select
9
11
  from psycopg2.sql import SQL, Composable, Composed
10
12
  from sqlalchemy import MetaData, create_engine, text
11
13
  from sqlalchemy.engine import Connection, Engine
@@ -23,8 +25,6 @@ from sqlparse import format, split
23
25
 
24
26
  from macrostrat.utils import cmd, get_logger
25
27
 
26
- from .postgresql import _setup_psycopg2_wait_callback
27
-
28
28
  log = get_logger(__name__)
29
29
 
30
30
 
@@ -150,7 +150,7 @@ def _get_queries(sql, interpret_as_file=None):
150
150
  if isinstance(sql, Path):
151
151
  sql = sql.read_text()
152
152
 
153
- return split(sql)
153
+ return split(format(sql, strip_comments=True))
154
154
 
155
155
 
156
156
  def _is_prebind_param(param):
@@ -239,8 +239,6 @@ def _run_sql(connectable, sql, params=None, **kwargs):
239
239
  yield from _run_sql(conn, sql, params, **kwargs)
240
240
  return
241
241
 
242
- _setup_psycopg2_wait_callback()
243
-
244
242
  stop_on_error = kwargs.pop("stop_on_error", False)
245
243
  raise_errors = kwargs.pop("raise_errors", False)
246
244
  has_server_binds = kwargs.pop("has_server_binds", None)
@@ -266,6 +264,9 @@ def _run_sql(connectable, sql, params=None, **kwargs):
266
264
 
267
265
  for query, params in zip(queries, params):
268
266
  trans = None
267
+ # This only does something for postgresql, but it's harmless to run it for other engines
268
+ set_wait_callback(wait_select)
269
+
269
270
  try:
270
271
  trans = connectable.begin()
271
272
  except InvalidRequestError:
@@ -320,6 +321,8 @@ def _run_sql(connectable, sql, params=None, **kwargs):
320
321
  log.error(err)
321
322
  if raise_errors:
322
323
  raise err
324
+ finally:
325
+ set_wait_callback(None)
323
326
 
324
327
 
325
328
  def run_sql_file(connectable, filename, params=None, **kwargs):
@@ -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.1.0"
6
+ version = "3.1.2"
7
7
 
8
8
  [tool.poetry.dependencies]
9
9
  GeoAlchemy2 = "^0.14.0"