Skip to content

Commit

Permalink
Remove the management parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-berchet committed Jan 6, 2023
1 parent b3204da commit cbd100b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 37 deletions.
9 changes: 3 additions & 6 deletions geoalchemy2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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():
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions geoalchemy2/alembic_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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:
Expand Down
25 changes: 15 additions & 10 deletions geoalchemy2/dialects/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
]

Expand All @@ -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]
Expand All @@ -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

Expand Down
10 changes: 7 additions & 3 deletions geoalchemy2/dialects/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 1 addition & 5 deletions geoalchemy2/dialects/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions geoalchemy2/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions tests/test_functional_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit cbd100b

Please sign in to comment.