diff --git a/CHANGELOG.md b/CHANGELOG.md index 782bd95cd..967011cda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,9 @@ make client-build ``` ### Migrations and checks #### Schema migrations +- [#703](https://github.com/LayerManager/layman/issues/703) Create new json column `db_connection` in `publications` table in prime DB schema. #### Data migrations +- [#703](https://github.com/LayerManager/layman/issues/703) Fill column `db_connection` in `publications` table in prime DB schema for all publications. Value is set to `null` for all existing publications. ### Changes - [#703](https://github.com/LayerManager/layman/issues/703) Endpoint [POST Workspace Layers](doc/rest.md#post-workspace-layers) support new body parameter *db_connection*. diff --git a/src/layman/upgrade/__init__.py b/src/layman/upgrade/__init__.py index 8d473fae6..076b3d70d 100644 --- a/src/layman/upgrade/__init__.py +++ b/src/layman/upgrade/__init__.py @@ -1,7 +1,7 @@ import logging from db import util as db_util -from layman.upgrade import upgrade_v1_8, upgrade_v1_9, upgrade_v1_10, upgrade_v1_12, upgrade_v1_16, upgrade_v1_17, upgrade_v1_18 +from layman.upgrade import upgrade_v1_8, upgrade_v1_9, upgrade_v1_10, upgrade_v1_12, upgrade_v1_16, upgrade_v1_17, upgrade_v1_18, upgrade_v1_20 from layman import settings from . import consts @@ -29,6 +29,9 @@ ]), ((1, 18, 0), [ upgrade_v1_18.adjust_db_for_image_mosaic, + ]), + ((1, 20, 0), [ + upgrade_v1_20.adjust_db_for_db_connection, ]) ], consts.MIGRATION_TYPE_DATA: [ diff --git a/src/layman/upgrade/upgrade_v1_20.py b/src/layman/upgrade/upgrade_v1_20.py new file mode 100644 index 000000000..3995fc434 --- /dev/null +++ b/src/layman/upgrade/upgrade_v1_20.py @@ -0,0 +1,19 @@ +import logging + +from db import util as db_util +from layman import settings + +logger = logging.getLogger(__name__) +DB_SCHEMA = settings.LAYMAN_PRIME_SCHEMA + + +def adjust_db_for_db_connection(): + logger.info(f' Alter DB prime schema for db_connection') + + statement = f''' + ALTER TABLE {DB_SCHEMA}.publications ADD COLUMN IF NOT EXISTS db_connection json;''' + db_util.run_statement(statement) + + statement = f'alter table {DB_SCHEMA}.publications add constraint db_connection_with_publ_type_check CHECK ' \ + f'(db_connection IS NULL OR file_type = %s);' + db_util.run_statement(statement, (settings.FILE_TYPE_VECTOR, )) diff --git a/src/layman/upgrade/upgrade_v1_20_test.py b/src/layman/upgrade/upgrade_v1_20_test.py new file mode 100644 index 000000000..0878ef9b1 --- /dev/null +++ b/src/layman/upgrade/upgrade_v1_20_test.py @@ -0,0 +1,41 @@ +import pytest + +from db import util as db_util +from layman import app, settings +from test_tools import process_client +from . import upgrade_v1_20 + +DB_SCHEMA = settings.LAYMAN_PRIME_SCHEMA + + +@pytest.mark.usefixtures('ensure_layman') +def test_db_connection(): + main_workspace = 'test_db_connection_migration_workspace' + + layer_vector_def = (main_workspace, process_client.LAYER_TYPE, 'test_layer_vector', dict()) + layer_raster_def = (main_workspace, process_client.LAYER_TYPE, 'test_layer_raster', {'file_paths': ['sample/layman.layer/sample_jp2_rgb.jp2', ]}) + map_def = (main_workspace, process_client.MAP_TYPE, 'test_map', dict()) + + publication_defs = [layer_vector_def, layer_raster_def, map_def, ] + + for workspace, publication_type, publication, rest_args in publication_defs: + process_client.publish_workspace_publication(publication_type, workspace, publication, **rest_args) + + statement = f''' + ALTER TABLE {DB_SCHEMA}.publications DROP COLUMN db_connection;''' + with app.app_context(): + db_util.run_statement(statement) + + upgrade_v1_20.adjust_db_for_db_connection() + + query = f'''select p.db_connection, p.uuid + from {DB_SCHEMA}.publications p left join + {DB_SCHEMA}.workspaces w on w.id = p.id_workspace +where w.name = %s + and p.name = %s + and p.type = %s +;''' + for workspace, publication_type, publication, _ in publication_defs: + with app.app_context(): + db_conn = db_util.run_query(query, (workspace, publication, publication_type))[0][0] + assert not db_conn, f'publication={publication_type}:{workspace}.{publication}, db_conn={db_conn}'