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 committed Jan 5, 2024
1 parent 6e718fd commit 1bc3f65
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions 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 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 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)
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():
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()

0 comments on commit 1bc3f65

Please sign in to comment.