Skip to content

Commit

Permalink
Fix compatibility with SQLAlchemy>=2 (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-berchet authored Feb 3, 2023
1 parent 6a5b66b commit 308ac48
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test_and_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ jobs:
# Add PostGIS extension to "gis" database
psql -h localhost -p 5432 -U gis -d gis -c 'CREATE EXTENSION IF NOT EXISTS postgis;'
psql -h localhost -p 5432 -U gis -d gis -c 'DROP EXTENSION IF EXISTS postgis_tiger_geocoder CASCADE;'
# Check python version
- name: Display Python version
run: python -c "import sys; print(sys.version)"
Expand Down
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
default_language_version:
python: python3.7
python: python3.8
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.4.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
Expand All @@ -13,24 +13,24 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pycqa/isort
rev: 5.10.1
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 22.6.0
rev: 23.1.0
hooks:
- id: black
- repo: https://github.com/codespell-project/codespell
rev: v2.1.0
rev: v2.2.2
hooks:
- id: codespell
- repo: https://github.com/PyCQA/pydocstyle
rev: 6.1.1
rev: 6.3.0
hooks:
- id: pydocstyle
additional_dependencies: ["toml"]
exclude: "tests"
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
rev: 6.0.0
hooks:
- id: flake8
6 changes: 2 additions & 4 deletions geoalchemy2/dialects/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ def after_create(table, bind, **kw):
col.type = col._actual_type
del col._actual_type
dimension = get_col_dim(col)
args = [table.schema] if table.schema else []
args.extend([table.name, col.name, col.type.srid, col.type.geometry_type, dimension])
args = [table.name, col.name, col.type.srid, col.type.geometry_type, dimension]

stmt = select(*_format_select_args(func.RecoverGeometryColumn(*args)))
stmt = stmt.execution_options(autocommit=True)
Expand All @@ -211,8 +210,7 @@ def before_drop(table, bind, **kw):
# Disable spatial indexes if present
disable_spatial_index(bind, table, col)

args = [table.schema] if table.schema else []
args.extend([table.name, col.name])
args = [table.name, col.name]

stmt = select(*_format_select_args(func.DiscardGeometryColumn(*args)))
stmt = stmt.execution_options(autocommit=True)
Expand Down
6 changes: 5 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ def copy_and_connect_sqlite_db(input_db, tmp_db, engine_echo):
db_url = f"sqlite:///{tmp_db}"
engine = create_engine(db_url, echo=engine_echo)
listen(engine, "connect", load_spatialite)
print(f"""SPATIALITE VERSION: {engine.execute("SELECT spatialite_version();").fetchone()[0]}""")
with engine.begin() as connection:
print(
"SPATIALITE VERSION:",
connection.execute(text("SELECT spatialite_version();")).fetchone()[0],
)

if input_db.endswith("spatialite_lt_4.sqlite"):
engine._spatialite_version = 3
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy import MetaData
from sqlalchemy import create_engine
from sqlalchemy import text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmaker

from . import copy_and_connect_sqlite_db
Expand Down
2 changes: 1 addition & 1 deletion tests/gallery/test_decipher_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base

from geoalchemy2 import Raster
from geoalchemy2 import WKTElement
Expand Down
2 changes: 1 addition & 1 deletion tests/gallery/test_disable_wrapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base

from geoalchemy2 import Geometry

Expand Down
2 changes: 1 addition & 1 deletion tests/gallery/test_raster_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from sqlalchemy import MetaData
from sqlalchemy import Table
from sqlalchemy import func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Query
from sqlalchemy.orm import declarative_base

from geoalchemy2 import Geometry
from geoalchemy2 import Raster
Expand Down
2 changes: 1 addition & 1 deletion tests/gallery/test_specific_compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from sqlalchemy import MetaData
from sqlalchemy import func
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from sqlalchemy.sql.expression import BindParameter

from geoalchemy2 import WKTElement
Expand Down
2 changes: 1 addition & 1 deletion tests/gallery/test_summarystatsagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import __version__ as SA_VERSION
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base

from geoalchemy2 import Raster
from geoalchemy2 import WKTElement
Expand Down
2 changes: 1 addition & 1 deletion tests/gallery/test_type_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from sqlalchemy import MetaData
from sqlalchemy import func
from sqlalchemy import text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from sqlalchemy.types import TypeDecorator

from geoalchemy2 import Geometry
Expand Down
2 changes: 1 addition & 1 deletion tests/schema_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from sqlalchemy.sql import func
from sqlalchemy.types import TypeDecorator

Expand Down
14 changes: 9 additions & 5 deletions tests/test_alembic_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import Table
from sqlalchemy import text

from geoalchemy2 import Geometry
from geoalchemy2 import alembic_helpers
Expand Down Expand Up @@ -141,7 +142,8 @@ def test_script_path(alembic_dir):
@pytest.fixture
def alembic_env(engine, alembic_dir, alembic_config_path, alembic_env_path, test_script_path):
cfg_tmp = Config(alembic_config_path)
engine.execute("DROP TABLE IF EXISTS alembic_version;")
with engine.begin() as connection:
connection.execute(text("DROP TABLE IF EXISTS alembic_version;"))
command.init(cfg_tmp, str(alembic_dir), template="generic")
with alembic_env_path.open(mode="w", encoding="utf8") as f:
f.write(
Expand Down Expand Up @@ -243,7 +245,7 @@ class = StreamHandler
datefmt = %%H:%%M:%%S
""".format(
alembic_dir, engine.url
alembic_dir, str(engine.url).replace("***", engine.url.password or "")
)
)
return cfg
Expand Down Expand Up @@ -353,7 +355,8 @@ def test_migration_revision(conn, metadata, alembic_config, alembic_env_path, te
# Insert data in new table to check that everything works when Alembic copies the tables
from_text = "GeomFromEWKT" if conn.dialect.name == "sqlite" else "ST_GeomFromEWKT"
conn.execute(
"""INSERT INTO new_spatial_table (
text(
"""INSERT INTO new_spatial_table (
geom_with_idx,
geom_without_idx,
geom_without_idx_2
Expand All @@ -363,10 +366,11 @@ def test_migration_revision(conn, metadata, alembic_config, alembic_env_path, te
{from_text}('SRID=4326;LINESTRING(0 0, 1 1)')
)
""".format(
from_text=from_text
from_text=from_text
)
)
)
conn.execute("COMMIT")
conn.execute(text("COMMIT"))

# Remove spatial columns and add new ones
with test_script_path.open(mode="w", encoding="utf8") as f:
Expand Down
11 changes: 7 additions & 4 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,15 +818,18 @@ def test_reflection(self, conn, setup_reflection_tables):
def test_raster_reflection(self, conn, Ocean, setup_tables):
skip_pg12_sa1217(conn)
skip_postgis1(conn)
with pytest.warns(SAWarning):
if SQLA_LT_2:
with pytest.warns(SAWarning):
t = Table("ocean", MetaData(), autoload_with=conn)
else:
t = Table("ocean", MetaData(), autoload_with=conn)
type_ = t.c.rast.type
assert isinstance(type_, Raster)

@test_only_with_dialects("sqlite")
def test_sqlite_reflection_with_discarded_col(self, conn, Lake, setup_tables):
"""Test that a discarded geometry column is not properly reflected with SQLite."""
conn.execute("""DELETE FROM "geometry_columns" WHERE f_table_name = 'lake';""")
conn.execute(text("""DELETE FROM "geometry_columns" WHERE f_table_name = 'lake';"""))
t = Table(
"lake",
MetaData(),
Expand All @@ -844,9 +847,9 @@ def test_sqlite_reflection_with_discarded_col(self, conn, Lake, setup_tables):

@pytest.fixture
def ocean_view(self, conn, Ocean):
conn.execute("CREATE VIEW test_view AS SELECT * FROM ocean;")
conn.execute(text("CREATE VIEW test_view AS SELECT * FROM ocean;"))
yield Ocean
conn.execute("DROP VIEW test_view;")
conn.execute(text("DROP VIEW test_view;"))

def test_view_reflection(self, conn, Ocean, setup_tables, ocean_view):
"""Test reflection of a view.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_functional_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from sqlalchemy import text
from sqlalchemy.dialects.postgresql.psycopg2 import PGDialect_psycopg2
from sqlalchemy.exc import InternalError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from sqlalchemy.sql import func
from sqlalchemy.sql.expression import type_coerce

Expand Down
2 changes: 1 addition & 1 deletion tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base

from geoalchemy2 import Geometry
from geoalchemy2 import WKBElement
Expand Down
9 changes: 5 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ requires=

[gh-actions]
python =
3.7: py37-sqla{14, latest}, lint
3.8: py38-sqla{14, latest}, docs
3.9: py39-sqla{14, latest}
3.7: py37-sqla{14, latest}
3.8: py38-sqla{14, latest}, lint
3.9: py39-sqla{14, latest}, docs
3.10: py310-sqla{14, latest}
pypy-3.8: pypy3-sqla{14, latest}

Expand All @@ -32,6 +32,7 @@ deps=
commands=
pip freeze --all
pytest -v \
--basetemp={envtmpdir} \
--cov=geoalchemy2 \
--cov-branch \
--cov-report term-missing \
Expand Down Expand Up @@ -71,7 +72,7 @@ commands =
pre-commit run --all-files

[testenv:docs]
basepython = python3.8
basepython = python3.9
changedir = doc
allowlist_externals = make
deps =
Expand Down

0 comments on commit 308ac48

Please sign in to comment.