Skip to content

Commit

Permalink
refactor: remove support for sqlalchemy connections (psycopg2 driver …
Browse files Browse the repository at this point in the history
…only)
  • Loading branch information
spwoodcock committed Oct 23, 2024
1 parent 9be5fbc commit a395074
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 38 deletions.
23 changes: 1 addition & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,9 @@ split_features = split_by_sql(

- The db parameter can be a connection string to start a new connection.
- Or an existing database connection can be reused.
- To do this, either the psycopg2 connection, or a SQLAlchemy Session
- To do this, either the psycopg2 connection, or a DBAPI connection string
must be passed:

SQLAlchemy example:

```python
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from fmtm_splitter.splitter import split_by_sql

# Creates a SQLAlchemy Session object
engine = create_engine("postgresql://postgres:postgres@localhost/postgres")
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
db = SessionLocal()

# Then pass this object as the db param
split_features = split_by_sql(
aoi,
db,
num_buildings=50,
osm_extract=osm_extracts,
)
```

psycopg2 example:

```python
Expand Down
18 changes: 2 additions & 16 deletions fmtm_splitter/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@
from psycopg2.extras import Json, register_uuid
from shapely.geometry import Polygon

try:
import sqlalchemy

_sqlalchemy_import = True
except ImportError:
_sqlalchemy_import = False

log = logging.getLogger(__name__)


Expand All @@ -40,12 +33,10 @@ def create_connection(
"""Get db connection from existing psycopg2 connection, or URL string.
Args:
db (str, psycopg2.extensions.connection, sqlalchemy.orm.session.Session):
db (str, psycopg2.extensions.connection):
string or existing db connection.
If `db` is a string, a new connection is generated.
If `db` is a psycopg2 connection, the connection is re-used.
If `db` is a sqlalchemy.orm.session.Session object, the connection
is also reused.
Returns:
conn: DBAPI connection object to generate cursors from.
Expand All @@ -58,13 +49,8 @@ def create_connection(
conn = db
elif isinstance(db, str):
conn = psycopg2.connect(db)
elif _sqlalchemy_import and isinstance(db, sqlalchemy.orm.session.Session):
conn = db.connection().connection
else:
msg = (
"The `db` variable is not a valid string, psycopg2 connection, "
"or SQLAlchemy Session."
)
msg = "The `db` variable is not a valid string or psycopg2 connection."
log.error(msg)
raise ValueError(msg)

Expand Down

0 comments on commit a395074

Please sign in to comment.