From 5c02f93ab9754a7cc779fa1f3718f780d4153577 Mon Sep 17 00:00:00 2001 From: index-git Date: Fri, 20 Dec 2024 13:55:06 +0100 Subject: [PATCH] Migrate description into prime_db --- CHANGELOG.md | 1 + src/layman/upgrade/__init__.py | 5 ++- src/layman/upgrade/upgrade_v2_0.py | 32 ++++++++++++++- src/layman/upgrade/upgrade_v2_0_test.py | 52 +++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/layman/upgrade/upgrade_v2_0_test.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c0b05980..c9afffb52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ #### Schema migrations - [#1009](https://github.com/LayerManager/layman/issues/1009) Add column `description` to table `publications` in prime DB schema. #### Data migrations +- [#1009](https://github.com/LayerManager/layman/issues/1009) Fill column `description` in `publications` table in prime DB schema. Value is taken from GeoServer for layers and from filesystem for maps. ### Changes - [#1009](https://github.com/LayerManager/layman/issues/1009) PATCH Workspace [Layer](doc/rest.md#patch-workspace-layer)/[Map](doc/rest.md#patch-workspace-map) returns same response as POST Workspace [Layers](doc/rest.md#post-workspace-layers)/[Maps](doc/rest.md#post-workspace-maps) with only `name`, `uuid`, `url` and for Layer also optional `files_to_upload` keys. - [#1028](https://github.com/LayerManager/layman/issues/1028) Upgrade Node.js of Laymen Test Client from v18 to v22 and dependencies: diff --git a/src/layman/upgrade/__init__.py b/src/layman/upgrade/__init__.py index b79151d15..88a2a9215 100644 --- a/src/layman/upgrade/__init__.py +++ b/src/layman/upgrade/__init__.py @@ -48,7 +48,7 @@ upgrade_v1_23.remove_right_types_table, ]), ((2, 0, 0), [ - upgrade_v2_0.adjust_db_for_image_mosaic, + upgrade_v2_0.adjust_db_for_description, ]), ], consts.MIGRATION_TYPE_DATA: [ @@ -79,6 +79,9 @@ ((1, 23, 0), [ upgrade_v1_23.delete_user_roles, ]), + ((2, 0, 0), [ + upgrade_v2_0.adjust_publications_description, + ]), ], } diff --git a/src/layman/upgrade/upgrade_v2_0.py b/src/layman/upgrade/upgrade_v2_0.py index 5a5a45e7b..1e6430c95 100644 --- a/src/layman/upgrade/upgrade_v2_0.py +++ b/src/layman/upgrade/upgrade_v2_0.py @@ -2,15 +2,45 @@ from db import util as db_util from layman import settings +from layman.layer import LAYER_TYPE +from layman.layer.geoserver import wms as gs_wms +from layman.map import MAP_TYPE +from layman.map.filesystem import input_file logger = logging.getLogger(__name__) DB_SCHEMA = settings.LAYMAN_PRIME_SCHEMA -def adjust_db_for_image_mosaic(): +def adjust_db_for_description(): logger.info(f' Alter DB prime schema for description') statement = f''' ALTER TABLE {DB_SCHEMA}.publications ADD COLUMN IF NOT EXISTS description varchar(1024) default null;''' db_util.run_statement(statement) + + +def adjust_publications_description(): + logger.info(f' Adjust description of publications') + query = f'''select w.name, p.type, p.name + from {DB_SCHEMA}.publications p inner join + {DB_SCHEMA}.workspaces w on w.id = p.id_workspace + ;''' + publications = db_util.run_query(query) + + for workspace, publ_type, publication in publications: + logger.info(f' Adjust description of {publ_type} {workspace}.{publication}') + get_info = {LAYER_TYPE: gs_wms.get_layer_info, + MAP_TYPE: input_file.get_map_info, + }[publ_type] + description = get_info(workspace, publication)['description'] + + query = f'''update {DB_SCHEMA}.publications set + description = %s + where type = %s + and name = %s + and id_workspace = (select w.id from {DB_SCHEMA}.workspaces w where w.name = %s);''' + params = (description, publ_type, publication, workspace,) + db_util.run_statement(query, params) + + logger.info(f' Adjusting publications description DONE') diff --git a/src/layman/upgrade/upgrade_v2_0_test.py b/src/layman/upgrade/upgrade_v2_0_test.py new file mode 100644 index 000000000..0a9eec4cf --- /dev/null +++ b/src/layman/upgrade/upgrade_v2_0_test.py @@ -0,0 +1,52 @@ +import pytest + +from db import util as db_util +from layman import app, settings +from layman.common.prime_db_schema import publications +from test_tools import process_client +from . import upgrade_v2_0 + + +DB_SCHEMA = settings.LAYMAN_PRIME_SCHEMA + + +@pytest.mark.usefixtures('ensure_layman') +@pytest.mark.parametrize('name, publ_type, definition', [ + ('test_vector_layer_sld', process_client.LAYER_TYPE, {}), + ('test_vector_qml_layer', process_client.LAYER_TYPE, {'style_file': 'sample/style/small_layer.qml'}), + ('test_raster_layer', process_client.LAYER_TYPE, {'file_paths': [ + 'sample/layman.layer/sample_tif_tfw_rgba_opaque.tfw', + 'sample/layman.layer/sample_tif_tfw_rgba_opaque.tif', + ]}), + ('test_map', process_client.MAP_TYPE, {}), +]) +def test_table_name_migration(name, publ_type, definition): + workspace = 'test_description_migration' + + description = f'Description of {name}' + process_client.publish_workspace_publication(publ_type, workspace, name, + description=description, + **definition) + + assert process_client.get_workspace_publication(publ_type, workspace, name)['description'] == description + infos = publications.get_publication_infos(workspace) + assert infos[(workspace, publ_type, name)]['description'] == description + + statement = f''' + update {DB_SCHEMA}.publications set + description = null + ;''' + with app.app_context(): + db_util.run_statement(statement) + + infos = publications.get_publication_infos(workspace) + assert infos[(workspace, publ_type, name)]['description'] is None + + with app.app_context(): + upgrade_v2_0.adjust_publications_description() + + assert process_client.get_workspace_publication(publ_type, workspace, name)['description'] == description + infos = publications.get_publication_infos(workspace) + assert infos[(workspace, publ_type, name)]['description'] == description + + process_client.delete_workspace_publication(publ_type, workspace, name)