From 23d389ece7a4fb1562fbee491062ffce0b083421 Mon Sep 17 00:00:00 2001 From: Arjan Molenaar Date: Tue, 7 Nov 2023 12:07:08 +0100 Subject: [PATCH] feat: Slugs for tools and versions --- ...8b8d2d_add_slugs_for_tools_and_versions.py | 37 +++++++++++++++++++ backend/capellacollab/tools/crud.py | 3 ++ backend/capellacollab/tools/models.py | 2 + 3 files changed, 42 insertions(+) create mode 100644 backend/capellacollab/alembic/versions/2827788b8d2d_add_slugs_for_tools_and_versions.py diff --git a/backend/capellacollab/alembic/versions/2827788b8d2d_add_slugs_for_tools_and_versions.py b/backend/capellacollab/alembic/versions/2827788b8d2d_add_slugs_for_tools_and_versions.py new file mode 100644 index 0000000000..2e3595de39 --- /dev/null +++ b/backend/capellacollab/alembic/versions/2827788b8d2d_add_slugs_for_tools_and_versions.py @@ -0,0 +1,37 @@ +# SPDX-FileCopyrightText: Copyright DB Netz AG and the capella-collab-manager contributors +# SPDX-License-Identifier: Apache-2.0 + +"""Slugs for tools and versions + +Revision ID: 2827788b8d2d +Revises: 5ca7037ef183 +Create Date: 2023-11-07 11:15:07.753055 + +""" +import sqlalchemy as sa +from alembic import op +from slugify import slugify + +revision = "2827788b8d2d" +down_revision = "5ca7037ef183" +branch_labels = None +depends_on = None + + +def upgrade(): + for table_name in ("tools", "versions"): + op.add_column( + table_name, sa.Column("slug", sa.String(), nullable=True) + ) + + connection = op.get_bind() + rows = connection.execute(sa.text(f"SELECT * from {table_name}")) + + for row in rows: + connection.execute( + sa.text( + f"UPDATE {table_name} SET slug = '{slugify(row.name)}' WHERE id = {row.id};" + ) + ) + + op.alter_column(table_name, "slug", nullable=False) diff --git a/backend/capellacollab/tools/crud.py b/backend/capellacollab/tools/crud.py index 47d49edf5b..a80e5455df 100644 --- a/backend/capellacollab/tools/crud.py +++ b/backend/capellacollab/tools/crud.py @@ -4,6 +4,7 @@ from collections import abc import sqlalchemy as sa +from slugify import slugify from sqlalchemy import exc, orm from capellacollab.core import database @@ -37,6 +38,7 @@ def get_tool_by_name( def create_tool( db: orm.Session, tool: models.DatabaseTool ) -> models.DatabaseTool: + tool.slug = slugify(tool.name) tool.integrations = integrations_models.DatabaseToolIntegrations( pure_variants=False, t4c=False, jupyter=False ) @@ -161,6 +163,7 @@ def create_version( is_recommended=is_recommended, is_deprecated=is_deprecated, tool_id=tool_id, + slug=slugify(name), ) db.add(version) db.commit() diff --git a/backend/capellacollab/tools/models.py b/backend/capellacollab/tools/models.py index d503de71ce..cc23db9197 100644 --- a/backend/capellacollab/tools/models.py +++ b/backend/capellacollab/tools/models.py @@ -23,6 +23,7 @@ class DatabaseTool(database.Base): id: orm.Mapped[int] = orm.mapped_column(primary_key=True) name: orm.Mapped[str] + slug: orm.Mapped[str] docker_image_template: orm.Mapped[str] docker_image_backup_template: orm.Mapped[str | None] readonly_docker_image_template: orm.Mapped[str | None] @@ -46,6 +47,7 @@ class DatabaseVersion(database.Base): id: orm.Mapped[int] = orm.mapped_column(primary_key=True) name: orm.Mapped[str] + slug: orm.Mapped[str] is_recommended: orm.Mapped[bool] is_deprecated: orm.Mapped[bool]