From cbd100ba6d4dc52c3c8ad5acd93febaf21aa2d02 Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Mon, 5 Dec 2022 23:38:17 +0100 Subject: [PATCH] Remove the management parameter --- geoalchemy2/__init__.py | 9 +++------ geoalchemy2/alembic_helpers.py | 4 ++-- geoalchemy2/dialects/common.py | 25 +++++++++++++++---------- geoalchemy2/dialects/postgresql.py | 10 +++++++--- geoalchemy2/dialects/sqlite.py | 6 +----- geoalchemy2/types.py | 12 ++++++------ tests/test_functional_postgresql.py | 10 +++++----- 7 files changed, 39 insertions(+), 37 deletions(-) diff --git a/geoalchemy2/__init__.py b/geoalchemy2/__init__.py index 133f0cdc..48d0c501 100644 --- a/geoalchemy2/__init__.py +++ b/geoalchemy2/__init__.py @@ -7,6 +7,7 @@ from geoalchemy2 import functions # noqa from geoalchemy2 import types # noqa +from geoalchemy2.dialects import common from geoalchemy2.dialects import postgresql from geoalchemy2.dialects import sqlite from geoalchemy2.dialects.common import _check_spatial_type @@ -30,9 +31,7 @@ def _select_dialect(dialect_name): try: return known_dialects[dialect_name] except KeyError: - raise ValueError( - f"The dialect '{dialect_name}' is unknown, please choose one of {known_dialects.keys()}" - ) + return common def _setup_ddl_event_listeners(): @@ -69,9 +68,7 @@ def after_parent_attach(column, table): ): raise ArgumentError("Arg Error(use_N_D_index): spatial_index must be True") - if getattr(column.type, "management", True) or not getattr( - column.type, "spatial_index", False - ): + if not getattr(column.type, "spatial_index", False): # If the column is managed, the indexes are created after the table return diff --git a/geoalchemy2/alembic_helpers.py b/geoalchemy2/alembic_helpers.py index c5aade54..93babb8a 100644 --- a/geoalchemy2/alembic_helpers.py +++ b/geoalchemy2/alembic_helpers.py @@ -443,7 +443,7 @@ def render_drop_geo_table(autogen_context, op): def create_geo_table(context, revision, op): """Replace the default CreateTableOp by a geospatial-specific one.""" dialect = context.bind.dialect - gis_cols = _get_gis_cols(op, (Geometry, Geography, Raster), dialect, check_col_management=False) + gis_cols = _get_gis_cols(op, (Geometry, Geography, Raster), dialect) if gis_cols: new_op = CreateGeospatialTableOp( @@ -465,7 +465,7 @@ def drop_geo_table(context, revision, op): dialect = context.bind.dialect table = op.to_table() gis_cols = _get_gis_cols( - table, (Geometry, Geography, Raster), dialect, check_col_management=False + table, (Geometry, Geography, Raster), dialect ) if gis_cols: diff --git a/geoalchemy2/dialects/common.py b/geoalchemy2/dialects/common.py index 15cd4820..a099b783 100644 --- a/geoalchemy2/dialects/common.py +++ b/geoalchemy2/dialects/common.py @@ -21,14 +21,23 @@ def _format_select_args(*args): return args -def _get_gis_cols(table, spatial_types, dialect, check_col_management=False): +def check_management(*args): + """Default function to check management.""" + return True + + +def _get_gis_cols(table, spatial_types, dialect, check_col_management=None): + if check_col_management is not None: + func = check_col_management + else: + func = check_management return [ col for col in table.columns if ( isinstance(col, Column) and _check_spatial_type(col.type, spatial_types, dialect) - and (not check_col_management or check_management(col, dialect.name)) + and func(col, dialect.name) ) ] @@ -40,13 +49,13 @@ def _check_spatial_type(tested_type, spatial_types, dialect=None): ) -def _get_dispatch_info(table, bind): +def _get_dispatch_info(table, bind, check_col_management=None): """Get info required for dispatch events.""" dialect = bind.dialect # Filter Geometry columns from the table with management=True # Note: Geography and PostGIS >= 2.0 don't need this - gis_cols = _get_gis_cols(table, Geometry, dialect, check_col_management=True) + gis_cols = _get_gis_cols(table, Geometry, dialect, check_col_management=check_col_management) # Find all other columns that are not managed Geometries regular_cols = [x for x in table.columns if x not in gis_cols] @@ -67,17 +76,13 @@ def _update_table_for_dispatch(table, regular_cols): table.columns = column_collection -def setup_create_drop(table, bind): +def setup_create_drop(table, bind, check_col_management=None): """Prepare the table for before_create and before_drop events.""" - dialect, gis_cols, regular_cols = _get_dispatch_info(table, bind) + dialect, gis_cols, regular_cols = _get_dispatch_info(table, bind, check_col_management) _update_table_for_dispatch(table, regular_cols) return dialect, gis_cols, regular_cols -def check_management(column, dialect_name): - return getattr(column.type, "management", False) is True or dialect_name == "sqlite" - - def before_create(table, bind, **kw): return diff --git a/geoalchemy2/dialects/postgresql.py b/geoalchemy2/dialects/postgresql.py index 6de583ff..e4b4a750 100644 --- a/geoalchemy2/dialects/postgresql.py +++ b/geoalchemy2/dialects/postgresql.py @@ -7,12 +7,16 @@ from geoalchemy2.dialects.common import _check_spatial_type from geoalchemy2.dialects.common import _format_select_args from geoalchemy2.dialects.common import _spatial_idx_name -from geoalchemy2.dialects.common import check_management from geoalchemy2.dialects.common import setup_create_drop from geoalchemy2.types import Geography from geoalchemy2.types import Geometry +def check_management(column, dialect_name): + """Check if the column should be managed.""" + return getattr(column.type, "use_typmod", None) is False + + def create_spatial_index(bind, table, col): """Create spatial index on the given column.""" # If the index does not exist (which might be the case when @@ -81,7 +85,7 @@ def reflect_geometry_column(inspector, table, column_info): def before_create(table, bind, **kw): """Handle spatial indexes during the before_create event.""" - dialect, gis_cols, regular_cols = setup_create_drop(table, bind) + dialect, gis_cols, regular_cols = setup_create_drop(table, bind, check_management) dialect_name = dialect.name # Remove the spatial indexes from the table metadata because they should not be @@ -152,7 +156,7 @@ def after_create(table, bind, **kw): def before_drop(table, bind, **kw): """Handle spatial indexes during the before_drop event.""" - dialect, gis_cols, regular_cols = setup_create_drop(table, bind) + dialect, gis_cols, regular_cols = setup_create_drop(table, bind, check_management) # Drop the managed Geometry columns for col in gis_cols: diff --git a/geoalchemy2/dialects/sqlite.py b/geoalchemy2/dialects/sqlite.py index 800b3752..0a1cc910 100644 --- a/geoalchemy2/dialects/sqlite.py +++ b/geoalchemy2/dialects/sqlite.py @@ -8,7 +8,6 @@ from geoalchemy2.dialects.common import _check_spatial_type from geoalchemy2.dialects.common import _format_select_args from geoalchemy2.dialects.common import _spatial_idx_name -from geoalchemy2.dialects.common import check_management from geoalchemy2.dialects.common import setup_create_drop from geoalchemy2.types import Geography from geoalchemy2.types import Geometry @@ -151,7 +150,6 @@ def reflect_geometry_column(inspector, table, column_info): def before_create(table, bind, **kw): """Handle spatial indexes during the before_create event.""" dialect, gis_cols, regular_cols = setup_create_drop(table, bind) - dialect_name = dialect.name # Remove the spatial indexes from the table metadata because they should not be # created during the table.create() step since the associated columns do not exist @@ -162,7 +160,6 @@ def before_create(table, bind, **kw): for col in table.info["_saved_columns"]: if ( _check_spatial_type(col.type, Geometry, dialect) - and check_management(col, dialect_name) ) and col in idx.columns.values(): table.indexes.remove(idx) if idx.name != _spatial_idx_name(table.name, col.name) or not getattr( @@ -175,13 +172,12 @@ def before_create(table, bind, **kw): def after_create(table, bind, **kw): """Handle spatial indexes during the after_create event.""" dialect = bind.dialect - dialect_name = dialect.name table.columns = table.info.pop("_saved_columns") for col in table.columns: # Add the managed Geometry columns with AddGeometryColumn() - if _check_spatial_type(col.type, Geometry, dialect) and check_management(col, dialect_name): + if _check_spatial_type(col.type, Geometry, dialect): col.type = col._actual_type del col._actual_type dimension = get_col_dim(col) diff --git a/geoalchemy2/types.py b/geoalchemy2/types.py index 57c5a330..c4dac553 100644 --- a/geoalchemy2/types.py +++ b/geoalchemy2/types.py @@ -127,7 +127,7 @@ def __init__( dimension=2, spatial_index=True, use_N_D_index=False, - management=False, + management=None, use_typmod=None, from_text=None, name=None, @@ -146,13 +146,13 @@ def __init__( self.dimension = dimension self.spatial_index = spatial_index self.use_N_D_index = use_N_D_index - if management: + if management is not None: warnings.warn( - "The 'management' parameter is going to be deprecated and will raise an error in " - "the version 0.14", - PendingDeprecationWarning, + "The 'management' parameter is deprecated and will raise an error in the " + "version 0.14", + DeprecationWarning, ) - self.management = management + self.management = (management is True) self.use_typmod = use_typmod self.extended = self.as_binary == "ST_AsEWKB" self.nullable = nullable diff --git a/tests/test_functional_postgresql.py b/tests/test_functional_postgresql.py index adab702a..b9967682 100644 --- a/tests/test_functional_postgresql.py +++ b/tests/test_functional_postgresql.py @@ -153,14 +153,14 @@ class TableWithIndexes(BaseArgTest): Geometry( geometry_type="POINT", spatial_index=False, - management=True, + # management=True, ) ) geom_managed_index = Column( Geometry( geometry_type="POINT", spatial_index=True, - management=True, + # management=True, ) ) # Test indexes on Geometry columns with ND index. @@ -179,7 +179,7 @@ class TableWithIndexes(BaseArgTest): dimension=3, spatial_index=True, use_N_D_index=True, - management=True, + # management=True, ) ) # Test indexes on Geography columns. @@ -201,14 +201,14 @@ class TableWithIndexes(BaseArgTest): Geography( geometry_type="POINT", spatial_index=False, - management=True, + # management=True, ) ) geog_managed_index = Column( Geography( geometry_type="POINT", spatial_index=True, - management=True, + # management=True, ) ) # Test indexes on Raster columns.