diff --git a/CHANGELOG.md b/CHANGELOG.md index b3ec51573..c34da8918 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ #### Schema migrations - [#165](https://github.com/LayerManager/layman/issues/165) Add column `role_name` to table `rights` in prime DB schema. Add constraint that exactly one of columns `role_name` and `id_user` is not null. - [#165](https://github.com/LayerManager/layman/issues/165) Create DB schema `_role_service` that can be used as [role service](doc/security.md#role-service). +- [#165](https://github.com/LayerManager/layman/issues/165) Column `name` in table `workspace` in prime DB schema length is changed to 59 characters. #### Data migrations - [#165](https://github.com/LayerManager/layman/issues/165) Delete technical roles and user-role relations in GeoServer `default` role service, which is now replaced by JDBC role service. ### Changes diff --git a/src/layman/upgrade/__init__.py b/src/layman/upgrade/__init__.py index 6f9df0eac..5854f1282 100644 --- a/src/layman/upgrade/__init__.py +++ b/src/layman/upgrade/__init__.py @@ -44,6 +44,7 @@ ((1, 23, 0), [ upgrade_v1_23.adjust_db_for_roles, upgrade_v1_23.create_role_service_schema, + upgrade_v1_23.restrict_workspace_name_length, ]), ], consts.MIGRATION_TYPE_DATA: [ diff --git a/src/layman/upgrade/upgrade_v1_23.py b/src/layman/upgrade/upgrade_v1_23.py index 9e084add4..327ba47f1 100644 --- a/src/layman/upgrade/upgrade_v1_23.py +++ b/src/layman/upgrade/upgrade_v1_23.py @@ -168,3 +168,26 @@ def delete_user_roles(): role_not_exists = response.status_code == 404 if not role_not_exists: response.raise_for_status() + + +def restrict_workspace_name_length(): + logger.info(f' Restrict username length') + + select_too_long = f""" +select name +from {settings.LAYMAN_PRIME_SCHEMA}.workspaces +where length(name) > 59 +;""" + too_long_workspace_name = db_util.run_query(select_too_long) + if len(too_long_workspace_name) > 0: + raise NotImplementedError(f"Too long workspace names: {[name[0] for name in too_long_workspace_name]}") + + # For direct ALTER TABLE raises "ERROR: cannot alter type of a column used by a view or rule" + # For details see https://web.archive.org/web/20111007112138/http://sniptools.com/databases/resize-a-column-in-a-postgresql-table-without-changing-data + alter_column = f""" +UPDATE pg_attribute SET + atttypmod = 59+4 +WHERE attrelid = '{settings.LAYMAN_PRIME_SCHEMA}.workspaces'::regclass +AND attname = 'name' +;""" + db_util.run_statement(alter_column) diff --git a/src/layman/upgrade/upgrade_v1_23_test.py b/src/layman/upgrade/upgrade_v1_23_test.py index 7451434b6..2aaf3e2ab 100644 --- a/src/layman/upgrade/upgrade_v1_23_test.py +++ b/src/layman/upgrade/upgrade_v1_23_test.py @@ -143,3 +143,27 @@ def test_create_role_service_schema(): assert result == 1 result = db_util.run_query(table_existence_query, ('group_roles',))[0][0] assert result == 1 + + +def test_restrict_username_length(): + workspace_name = 'test_restrict_username_length'.ljust(60, 'x') + alter_column = f""" + UPDATE pg_attribute SET + atttypmod = 1043 + WHERE attrelid = '{settings.LAYMAN_PRIME_SCHEMA}.workspaces'::regclass + AND attname = 'name' + ;""" + db_util.run_statement(alter_column) + + insert_statement = f"""INSERT INTO {settings.LAYMAN_PRIME_SCHEMA}.workspaces (name) values ('{workspace_name}')""" + db_util.run_statement(insert_statement) + + with app.app_context(): + with pytest.raises(NotImplementedError): + upgrade_v1_23.restrict_workspace_name_length() + + delete_statement = f"""DELETE FROM {settings.LAYMAN_PRIME_SCHEMA}.workspaces WHERE name = '{workspace_name}'""" + db_util.run_statement(delete_statement) + + with app.app_context(): + upgrade_v1_23.restrict_workspace_name_length()