Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new DB column publications.db_connection #745

Merged
merged 2 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 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 Expand Up @@ -118,7 +120,7 @@ make client-build
```
### Migrations and checks
#### Schema migrations
- [#576](https://github.com/LayerManager/layman/issues/576) Create new column `file_type` in `publication` table.
- [#576](https://github.com/LayerManager/layman/issues/576) Create new column `file_type` in `publications` table.
- [#541](https://github.com/LayerManager/layman/issues/541) Rename vector data DB tables to `layer_<uuid>` format.
#### Data migrations
- [#576](https://github.com/LayerManager/layman/issues/576) Fill column `file_type` in `publications` table in prime DB schema for all publications. Value of each map will be `NULL`. Value of each layer will be same as value of `file.file_type` in [GET Workspace Layer](doc/rest.md#get-workspace-layer) response (i.e. `vector`, `raster`, or `unknown`).
Expand Down Expand Up @@ -199,7 +201,7 @@ make client-build
```
### Migrations and checks
#### Schema migrations
- [#64](https://github.com/LayerManager/layman/issues/64) Create new column `srid` in `publication` table.
- [#64](https://github.com/LayerManager/layman/issues/64) Create new column `srid` in `publications` table.
#### Data migrations
- [#64](https://github.com/LayerManager/layman/issues/64) Native CRS of previously uploaded layers is set to `EPSG:3857`.
- [#64](https://github.com/LayerManager/layman/issues/64) Native CRS of previously uploaded maps is set according their composition file (either `EPSG:3857` or `EPSG:4326`) and their composition file is upgraded to [version 2.0.0](https://raw.githubusercontent.com/hslayers/map-compositions/2.0.0/schema.json).
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}'