Skip to content

Commit

Permalink
Create new DB column publications.db_connection
Browse files Browse the repository at this point in the history
  • Loading branch information
index-git committed Jan 17, 2023
1 parent 53e5b92 commit 2fba276
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*.

Expand Down
5 changes: 4 additions & 1 deletion src/layman/upgrade/__init__.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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: [
Expand Down
19 changes: 19 additions & 0 deletions src/layman/upgrade/upgrade_v1_20.py
Original file line number Diff line number Diff line change
@@ -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, ))
41 changes: 41 additions & 0 deletions src/layman/upgrade/upgrade_v1_20_test.py
Original file line number Diff line number Diff line change
@@ -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}'

0 comments on commit 2fba276

Please sign in to comment.