macrostrat.database 3.3.3__tar.gz → 3.4.0__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.3.3
3
+ Version: 3.4.0
4
4
  Summary: A SQLAlchemy-based database toolkit.
5
5
  Author: Daven Quinn
6
6
  Author-email: dev@davenquinn.com
@@ -1,19 +1,17 @@
1
1
  import warnings
2
2
  from contextlib import contextmanager
3
- from enum import Enum
4
3
  from pathlib import Path
5
4
  from typing import Optional, Union
6
5
 
7
6
  from psycopg2.errors import InvalidSavepointSpecification
8
7
  from psycopg2.sql import Identifier
9
- from sqlalchemy import URL, MetaData, create_engine, inspect, text
8
+ from sqlalchemy import URL, MetaData, create_engine, inspect, Engine
10
9
  from sqlalchemy.exc import IntegrityError, InternalError
11
10
  from sqlalchemy.ext.compiler import compiles
12
11
  from sqlalchemy.orm import Session, scoped_session, sessionmaker
13
12
  from sqlalchemy.sql.expression import Insert
14
13
 
15
14
  from macrostrat.utils import get_logger
16
-
17
15
  from .mapper import DatabaseMapper
18
16
  from .postgresql import on_conflict, prefix_inserts # noqa
19
17
  from .utils import ( # noqa
@@ -41,14 +39,14 @@ class Database(object):
41
39
 
42
40
  __inspector__ = None
43
41
 
44
- def __init__(self, db_conn: Union[str, URL], *, echo_sql=False, **kwargs):
42
+ def __init__(self, db_conn: Union[str, URL, Engine], *, echo_sql=False, **kwargs):
45
43
  """
46
44
  Wrapper for interacting with a database using SQLAlchemy.
47
45
  Optimized for use with PostgreSQL, but usable with SQLite
48
46
  as well.
49
47
 
50
48
  Args:
51
- db_conn (str): Connection string for the database.
49
+ db_conn (str | URL | Engine): Connection string or engine for the database.
52
50
 
53
51
  Keyword Args:
54
52
  echo_sql (bool): If True, will echo SQL commands to the
@@ -61,8 +59,12 @@ class Database(object):
61
59
 
62
60
  self.instance_params = kwargs.pop("instance_params", {})
63
61
 
64
- log.info(f"Setting up database connection '{db_conn}'")
65
- self.engine = create_engine(db_conn, echo=echo_sql, **kwargs)
62
+ if isinstance(db_conn, Engine):
63
+ log.info(f"Set up database connection with engine {db_conn.url}")
64
+ self.engine = db_conn
65
+ else:
66
+ log.info(f"Setting up database connection with URL '{db_conn}'")
67
+ self.engine = create_engine(db_conn, echo=echo_sql, **kwargs)
66
68
  self.metadata = kwargs.get("metadata", metadata)
67
69
 
68
70
  # Scoped session for database
@@ -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.3.3"
6
+ version = "3.4.0"
7
7
 
8
8
  [tool.poetry.dependencies]
9
9
  GeoAlchemy2 = "^0.14.0"