diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b53faaec..b415a7f42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ - [#868](https://github.com/LayerManager/layman/issues/868) Fill table `map_layer` with relations between maps and [internal layers](doc/models.md#internal-map-layer) (layers published on this Layman instance). Relations to [external layers](doc/models.md#internal-map-layer) (layers of other servers) are not imported into the table. ### Changes - [#868](https://github.com/LayerManager/layman/issues/868) Endpoints [GET Publications](doc/rest.md#get-publications), [GET Layers](doc/rest.md#get-layers), [GET Workspace Layers](doc/rest.md#get-workspace-layers), [GET Maps](doc/rest.md#get-maps), [GET Workspace Maps](doc/rest.md#get-workspace-maps), [GET Workspace Layer](doc/rest.md#get-workspace-layer), [GET Workspace Map](doc/rest.md#get-workspace-map), [POST Workspace Layers](doc/rest.md#post-workspace-layers), [DELETE Workspace Layer](doc/rest.md#delete-workspace-layer), [DELETE Workspace Layers](doc/rest.md#delete-workspace-layers), [DELETE Workspace Map](doc/rest.md#delete-workspace-map), [DELETE Workspace Maps](doc/rest.md#delete-workspace-maps), [POST Workspace Maps](doc/rest.md#post-workspace-maps), [PATCH Workspace Layer](doc/rest.md#patch-workspace-layer), [PATCH Workspace Map](doc/rest.md#patch-workspace-map) and [WMS/WFS endpoints](doc/endpoints.md) respects [HTTP header `X-Forwarded-Prefix`](doc/client-proxy.md#x-forwarded-prefix-http-header) of the request in the response. -- [#868](https://github.com/LayerManager/layman/issues/868) Relations between map and [internal layers](doc/models.md#internal-map-layer) are updated in `map_layer` table when calling [POST Workspace Maps](doc/rest.md#post-workspace-maps), [DELETE Workspace Map](doc/rest.md#delete-workspace-map), and [DELETE Workspace Maps](doc/rest.md#delete-workspace-maps). +- [#868](https://github.com/LayerManager/layman/issues/868) Relations between map and [internal layers](doc/models.md#internal-map-layer) are updated in `map_layer` table when calling [POST Workspace Maps](doc/rest.md#post-workspace-maps), [PATCH Workspace Map](doc/rest.md#patch-workspace-map), [DELETE Workspace Map](doc/rest.md#delete-workspace-map), and [DELETE Workspace Maps](doc/rest.md#delete-workspace-maps). - [#880](https://github.com/LayerManager/layman/issues/880) Use Docker Compose v2 (`docker compose`) in Makefile without `compatibility` flag and remove `Makefile_docker-compose_v1` file. Docker containers are named according to Docker Compose v2 and may have different name after upgrade. - [#765](https://github.com/LayerManager/layman/issues/765) Stop saving OAuth2 claims in filesystem, use prime DB schema only. - [#893](https://github.com/LayerManager/layman/issues/893) It is possible to specify logging level by new environment variable [LAYMAN_LOGLEVEL](doc/env-settings.md#LAYMAN_LOGLEVEL). Default level is `INFO`. diff --git a/src/layman/map/prime_db_schema/tasks.py b/src/layman/map/prime_db_schema/tasks.py index bfefab593..6e05cdc66 100644 --- a/src/layman/map/prime_db_schema/tasks.py +++ b/src/layman/map/prime_db_schema/tasks.py @@ -3,7 +3,7 @@ from layman import celery_app from layman.celery import AbortedException from layman.common import empty_method_returns_true -from .util import insert_internal_layers +from .util import ensure_internal_layers from .. import util, MAP_TYPE from ...common.prime_db_schema.publications import set_bbox @@ -32,7 +32,7 @@ def refresh_file_data( set_bbox(workspace, MAP_TYPE, mapname, native_bbox, crs, ) map_layers = util.get_layers_from_json(mapjson) - insert_internal_layers(workspace, mapname, map_layers) + ensure_internal_layers(workspace, mapname, map_layers) if self.is_aborted(): raise AbortedException diff --git a/src/layman/map/prime_db_schema/util.py b/src/layman/map/prime_db_schema/util.py index bc4f7bbd3..e561391db 100644 --- a/src/layman/map/prime_db_schema/util.py +++ b/src/layman/map/prime_db_schema/util.py @@ -6,14 +6,31 @@ DB_SCHEMA = settings.LAYMAN_PRIME_SCHEMA -def insert_internal_layers(workspace, mapname, layers): - map_id = get_publication_info(workspace, MAP_TYPE, mapname, context={'keys': ['id'], })['id'] - for layer_workspace, layer_name, layer_index in layers: +def ensure_internal_layers(workspace, mapname, layers): + map_info = get_publication_info(workspace, MAP_TYPE, mapname, context={'keys': ['id', 'map_layers'], }) + map_id = map_info['id'] + db_layers = {(layer_dict['workspace'], layer_dict['name'], layer_dict['index'], ) for layer_dict in map_info['_map_layers']} + + to_insert = set(layers) - db_layers + to_delete = db_layers - set(layers) + + for layer_workspace, layer_name, layer_index in to_insert: insert_query = f''' insert into {DB_SCHEMA}.map_layer(id_map, layer_workspace, layer_name, layer_index) values (%s, %s, %s, %s); ''' db_util.run_statement(insert_query, (map_id, layer_workspace, layer_name, layer_index,)) + for layer_workspace, layer_name, layer_index in to_delete: + delete_query = f''' + delete from {DB_SCHEMA}.map_layer + where id_map = %s + and layer_workspace = %s + and layer_name = %s + and layer_index = %s + ; + ''' + db_util.run_statement(delete_query, (map_id, layer_workspace, layer_name, layer_index,)) + def delete_internal_layer_relations(workspace, mapname): map_id = get_publication_info(workspace, MAP_TYPE, mapname, context={'keys': ['id'], })['id'] diff --git a/tests/asserts/final/publication/metadata.py b/tests/asserts/final/publication/metadata.py index 622b1f0b1..1f0f6bd22 100644 --- a/tests/asserts/final/publication/metadata.py +++ b/tests/asserts/final/publication/metadata.py @@ -28,7 +28,7 @@ 'identifier', 'map_endpoint', 'map_file_endpoint', - 'operates_on', + 'operates_on', # When sending map with no items after there were some items, items are not deleted (probably because of bug in Micka) 'organisation_name', 'publication_date', 'reference_system', diff --git a/tests/dynamic_data/publications/map_layer_relation/map_layer_relation_test.py b/tests/dynamic_data/publications/map_layer_relation/map_layer_relation_test.py index 445bd4d6d..081b49d9a 100644 --- a/tests/dynamic_data/publications/map_layer_relation/map_layer_relation_test.py +++ b/tests/dynamic_data/publications/map_layer_relation/map_layer_relation_test.py @@ -81,6 +81,29 @@ 'operates_on': [LAYER_HRANICE_PRIVATE], }, }, + 'patch_map_with_different_layers': { + 'post_before_test_args': { + 'file_paths': [os.path.join(DIRECTORY, 'internal_wms_and_wfs.json')], + }, + 'exp_before_rest_method': { + 'map_layers': { + # layer, layer index, exists? + (LAYER_HRANICE, 1, True), + (LAYER_MISTA_NON_EXISTENT, 2, False), + (LAYER_HRANICE, 3, True), + }, + 'operates_on': [LAYER_HRANICE], + }, + 'rest_method': base_test_classes.RestMethodAll.PATCH, + 'rest_args': { + 'file_paths': [os.path.join(DIRECTORY, 'internal_hranice_private.json')], + 'actor_name': PRIVATE_WORKSPACE, + }, + 'exp_after_rest_method': { + 'map_layers': [(LAYER_HRANICE_PRIVATE, 1, True)], + 'operates_on': [LAYER_HRANICE_PRIVATE], + }, + }, }