Skip to content

Commit

Permalink
Alter workspace.name column to varchar(59)
Browse files Browse the repository at this point in the history
  • Loading branch information
index-git authored and jirik committed Jan 8, 2024
1 parent 000d615 commit a4386ef
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,7 +37,7 @@
- [GET](doc/rest.md#get-workspace-map)/[PATCH](doc/rest.md#patch-workspace-map) Workspace Map
- GET Workspace [Layers](doc/rest.md#get-workspace-layers)/[Maps](doc/rest.md#get-workspace-maps)
- GET [Layers](doc/rest.md#get-layers)/[Maps](doc/rest.md#get-maps)/[Publications](doc/rest.md#get-publications)
- [#165](https://github.com/LayerManager/layman/issues/165) Name of [users](doc/models.md#username) and [public workspaces](doc/models.md#public-workspace) are from now on restricted to 59 characters.
- [#165](https://github.com/LayerManager/layman/issues/165) Name of [users](doc/models.md#username) and [public workspaces](doc/models.md#public-workspace) are from now on restricted to a maximum length of 59 characters.
- All changes from [v1.22.1](#v1221), [v1.22.2](#v1222) and [v1.22.3](#v1223).
- [#960](https://github.com/LayerManager/layman/issues/960) Handle WMS requests with HTTP error more efficiently in timgen.
- [#962](https://github.com/LayerManager/layman/issues/962) Make values of `layman_metadata.publication_status` and `status` key(s) more consistent in responses of PATCH Workspace [Layer](doc/rest.md#patch-workspace-layer)/[Map](doc/rest.md#patch-workspace-map) and GET Workspace [Layer](doc/rest.md#get-workspace-layer)/[Map](doc/rest.md#get-workspace-map).
Expand Down
1 change: 1 addition & 0 deletions src/layman/upgrade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
23 changes: 23 additions & 0 deletions src/layman/upgrade/upgrade_v1_23.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 workspace name 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)
24 changes: 24 additions & 0 deletions src/layman/upgrade/upgrade_v1_23_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
too_long_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 ('{too_long_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 = '{too_long_workspace_name}'"""
db_util.run_statement(delete_statement)

with app.app_context():
upgrade_v1_23.restrict_workspace_name_length()

0 comments on commit a4386ef

Please sign in to comment.