Skip to content

Commit

Permalink
Provide alembic_helpers.include_object again
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-berchet committed Jul 20, 2022
1 parent e11ef48 commit bd122fc
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 36 deletions.
15 changes: 15 additions & 0 deletions doc/alembic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ file used by Alembic to auto-generate the migration scripts, like in the followi
# ...
context.configure(
# ...
include_object=alembic_helpers.include_object,
process_revision_directives=alembic_helpers.writer,
render_item=alembic_helpers.render_item,
)
Expand All @@ -131,11 +132,21 @@ file used by Alembic to auto-generate the migration scripts, like in the followi
# ...
context.configure(
# ...
include_object=alembic_helpers.include_object,
process_revision_directives=alembic_helpers.writer,
render_item=alembic_helpers.render_item,
)
# ...
Qs one can see, there are 3 specific functions to pass to the context:

1. :func:`geoalchemy2.alembic_helpers.include_object` ignores the internal tables managed by the
spatial extensions (note that in some cases this function might need some customization, see the
details in the doc of this function).
2. :obj:`geoalchemy2.alembic_helpers.writer` adds specific spatial operations to Alembic.
3. :func:`geoalchemy2.alembic_helpers.render_item` automatically adds `GeoAlchemy2` imports into the
migration scripts.

After running the ``alembic revision --autogenerate -m <msg>`` command, the migration script should
be properly generated and should not need to be manually edited.

Expand Down Expand Up @@ -190,6 +201,7 @@ in ``my_package.custom_types``, you just have to edit the ``env.py`` file like t
# ...
context.configure(
# ...
include_object=alembic_helpers.include_object,
process_revision_directives=alembic_helpers.writer,
render_item=render_item,
)
Expand All @@ -200,6 +212,7 @@ in ``my_package.custom_types``, you just have to edit the ``env.py`` file like t
# ...
context.configure(
# ...
include_object=alembic_helpers.include_object,
process_revision_directives=alembic_helpers.writer,
render_item=render_item,
)
Expand All @@ -226,6 +239,7 @@ thus the ``env.py`` file should look like the following:
# ...
context.configure(
# ...
include_object=alembic_helpers.include_object,
process_revision_directives=alembic_helpers.writer,
render_item=alembic_helpers.render_item,
)
Expand All @@ -242,6 +256,7 @@ thus the ``env.py`` file should look like the following:
# ...
context.configure(
# ...
include_object=alembic_helpers.include_object,
process_revision_directives=alembic_helpers.writer,
render_item=alembic_helpers.render_item,
)
Expand Down
2 changes: 1 addition & 1 deletion doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The current version of this documentation applies to the version |version| of Ge
Requirements
------------

GeoAlchemy 2 requires SQLAlchemy 1.4.
GeoAlchemy 2 requires `SQLAlchemy <http://sqlalchemy.org>`_ >= 1.4.

Installation
------------
Expand Down
8 changes: 5 additions & 3 deletions geoalchemy2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@ def dispatch(current_event, table, bind):
bind.execute(stmt)
bind.execute(
text(
"""DROP TABLE IF EXISTS idx_{}_{};""".format(
table.name,
col.name,
"""DROP TABLE IF EXISTS {};""".format(
_spatial_idx_name(
table.name,
col.name,
)
)
)
)
Expand Down
26 changes: 26 additions & 0 deletions geoalchemy2/alembic_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
from geoalchemy2 import func

writer = rewriter.Rewriter()
"""Rewriter object for Alembic."""

_SPATIAL_TABLES = set()


Expand Down Expand Up @@ -96,6 +98,30 @@ def render_item(obj_type, obj, autogen_context):
return False


def include_object(obj, name, obj_type, reflected, compare_to):
"""Do not include internal tables of spatial extensions.
.. warning::
This function only checks the table names, so it might exclude tables that should not be.
In such case, you should create your own function to handle your specific table names.
"""
if (
obj_type == "table"
and (
name.startswith("geometry_columns")
or name.startswith("spatial_ref_sys")
or name.startswith("spatialite_history")
or name.startswith("sqlite_sequence")
or name.startswith("views_geometry_columns")
or name.startswith("virts_geometry_columns")
or name.startswith("idx_")
)
):
return False
return True


@Operations.register_operation("add_geospatial_column")
@BatchOperations.register_operation(
"add_geospatial_column", "batch_add_geospatial_column"
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
universal = 1

[tox:tox]
envlist = py{37,38,39,310}-sqla{14, latest}, pypy3-sqla{14, latest}, lint, coverage
envlist = py{37,38,39,310}-sqla{14, latest}, pypy3-sqla{14, latest}, lint, coverage, docs
requires=
setuptools>42

[gh-actions]
python =
3.7: py37-sqla{14, latest}, lint
3.8: py38-sqla{14, latest}
3.8: py38-sqla{14, latest}, docs
3.9: py39-sqla{14, latest}
3.10: py310-sqla{14, latest}
pypy-3.8: pypy3-sqla{14, latest}
Expand Down
50 changes: 32 additions & 18 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,32 +105,46 @@ def copy_and_connect_sqlite_db(input_db, tmp_db, engine_echo):


def check_indexes(conn, expected, table_name):
if conn.dialect.name == "postgresql":
# Query to check the indexes
index_query = text(
"""Check that actual indexes are equal to the expected ones."""
index_query = {
"postgresql": text(
"""SELECT indexname, indexdef
FROM pg_indexes
WHERE
tablename = '{}'
ORDER BY indexname;""".format(table_name)
)
indexes = conn.execute(index_query).fetchall()
),
"sqlite": text(
"""SELECT *
FROM geometry_columns
WHERE f_table_name = '{}'
ORDER BY f_table_name, f_geometry_column;""".format(table_name)
),
}

# Query to check the indexes
actual_indexes = conn.execute(index_query[conn.dialect.name]).fetchall()

expected = [
expected_indexes = expected[conn.dialect.name]
if conn.dialect.name == "postgresql":
expected_indexes = [
(i[0], re.sub("\n *", " ", i[1]))
for i in expected["postgresql"]
for i in expected_indexes
]

assert indexes == expected
try:
assert actual_indexes == expected_indexes
except AssertionError as exc:
print("###############################################")

elif conn.dialect.name == "sqlite":
# Query to check the indexes
index_query = text(
"""SELECT *
FROM geometry_columns
WHERE f_table_name = '{}'
ORDER BY f_table_name, f_geometry_column;""".format(table_name)
)
print("Expected indexes:")
for i in expected_indexes:
print(i)

print("Actual indexes:")
for i in actual_indexes:
print(i)

print("###############################################")

indexes = conn.execute(index_query).fetchall()
assert indexes == expected["sqlite"]
raise exc
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def engine(tmpdir, db_url, _engine_echo):

# For other dialects the engine is directly returned
engine = create_engine(db_url, echo=_engine_echo)
engine.update_execution_options(search_path=["gis", "public"])
engine._spatialite_version = None
return engine

Expand Down
13 changes: 1 addition & 12 deletions tests/test_alembic_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,24 +177,13 @@ def alembic_env(
connection = engine.connect()
def include_object(obj, name, obj_type, reflected, compare_to):
# Ignore tables that are already in the DB
if (
obj_type == "table"
and name not in [
"new_spatial_table"
]
):
return False
return True
context.configure(
connection=connection,
target_metadata=target_metadata,
version_table_pk=True,
process_revision_directives=alembic_helpers.writer,
render_item=alembic_helpers.render_item,
include_object=include_object,
include_object=alembic_helpers.include_object,
render_as_batch=True
)
Expand Down

0 comments on commit bd122fc

Please sign in to comment.