Skip to content

Commit

Permalink
Migrate description into prime_db
Browse files Browse the repository at this point in the history
  • Loading branch information
index-git committed Dec 20, 2024
1 parent 5b4c314 commit 9dcbb16
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion src/layman/upgrade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -79,6 +79,9 @@
((1, 23, 0), [
upgrade_v1_23.delete_user_roles,
]),
((2, 0, 0), [
upgrade_v2_0.adjust_publications_description,
]),
],
}

Expand Down
32 changes: 31 additions & 1 deletion src/layman/upgrade/upgrade_v2_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
55 changes: 55 additions & 0 deletions src/layman/upgrade/upgrade_v2_0_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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, external_db
from . import upgrade_v2_0


DB_SCHEMA = settings.LAYMAN_PRIME_SCHEMA
INPUT_FILE_PATH = 'sample/layman.layer/small_layer.geojson'
EXTERNAL_DB_TABLE = '_small_LAYER_by_used_servers'
EXTERNAL_DB_SCHEMA = 'public'


@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)

0 comments on commit 9dcbb16

Please sign in to comment.