Add slug to cookie categories for stable consent identifiers
The category slug provides a stable, URL-safe key used as the data-cookie-consent attribute value and consent data key, replacing the fragile category name. This prevents breakage when categories are renamed. - Add slug column with unique-per-banner constraint and backfill migration - Add Slug validator (lowercase alphanumeric + hyphens) - Propagate slug through GraphQL schema, service layer, and snapshot - Update console UI with slug field in create/edit forms - Switch cookie-banner widget to use slug as consent data keys Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -50,6 +50,7 @@ type (
|
||||
|
||||
CookieBannerVersionSnapshotCategory struct {
|
||||
Name string `json:"name"`
|
||||
Slug string `json:"slug"`
|
||||
Description string `json:"description"`
|
||||
Kind CookieCategoryKind `json:"kind"`
|
||||
Cookies CookieItems `json:"cookies"`
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
@@ -42,6 +43,7 @@ type (
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
CookieBannerID gid.GID `db:"cookie_banner_id"`
|
||||
Name string `db:"name"`
|
||||
Slug string `db:"slug"`
|
||||
Description string `db:"description"`
|
||||
Kind CookieCategoryKind `db:"kind"`
|
||||
Rank int `db:"rank"`
|
||||
@@ -103,6 +105,7 @@ SELECT
|
||||
organization_id,
|
||||
cookie_banner_id,
|
||||
name,
|
||||
slug,
|
||||
description,
|
||||
kind,
|
||||
rank,
|
||||
@@ -152,6 +155,7 @@ SELECT
|
||||
organization_id,
|
||||
cookie_banner_id,
|
||||
name,
|
||||
slug,
|
||||
description,
|
||||
kind,
|
||||
rank,
|
||||
@@ -229,6 +233,7 @@ SELECT
|
||||
organization_id,
|
||||
cookie_banner_id,
|
||||
name,
|
||||
slug,
|
||||
description,
|
||||
kind,
|
||||
rank,
|
||||
@@ -275,6 +280,7 @@ INSERT INTO cookie_categories (
|
||||
organization_id,
|
||||
cookie_banner_id,
|
||||
name,
|
||||
slug,
|
||||
description,
|
||||
kind,
|
||||
rank,
|
||||
@@ -286,6 +292,7 @@ INSERT INTO cookie_categories (
|
||||
@organization_id,
|
||||
@cookie_banner_id,
|
||||
@name,
|
||||
@slug,
|
||||
@description,
|
||||
@kind,
|
||||
@rank,
|
||||
@@ -300,6 +307,7 @@ INSERT INTO cookie_categories (
|
||||
"organization_id": c.OrganizationID,
|
||||
"cookie_banner_id": c.CookieBannerID,
|
||||
"name": c.Name,
|
||||
"slug": c.Slug,
|
||||
"description": c.Description,
|
||||
"kind": c.Kind,
|
||||
"rank": c.Rank,
|
||||
@@ -309,6 +317,11 @@ INSERT INTO cookie_categories (
|
||||
|
||||
_, err := tx.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
|
||||
if pgErr.Code == "23505" && pgErr.ConstraintName == "idx_cookie_categories_unique_slug_per_banner" {
|
||||
return ErrResourceAlreadyExists
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("cannot insert cookie category: %w", err)
|
||||
}
|
||||
|
||||
@@ -324,6 +337,7 @@ func (c *CookieCategory) Update(
|
||||
UPDATE cookie_categories
|
||||
SET
|
||||
name = @name,
|
||||
slug = @slug,
|
||||
description = @description,
|
||||
updated_at = @updated_at
|
||||
WHERE
|
||||
@@ -336,6 +350,7 @@ WHERE
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": c.ID,
|
||||
"name": c.Name,
|
||||
"slug": c.Slug,
|
||||
"description": c.Description,
|
||||
"updated_at": c.UpdatedAt,
|
||||
}
|
||||
@@ -343,6 +358,11 @@ WHERE
|
||||
|
||||
result, err := tx.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
|
||||
if pgErr.Code == "23505" && pgErr.ConstraintName == "idx_cookie_categories_unique_slug_per_banner" {
|
||||
return ErrResourceAlreadyExists
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("cannot update cookie category: %w", err)
|
||||
}
|
||||
|
||||
@@ -443,6 +463,7 @@ SELECT
|
||||
organization_id,
|
||||
cookie_banner_id,
|
||||
name,
|
||||
slug,
|
||||
description,
|
||||
kind,
|
||||
rank,
|
||||
|
||||
24
pkg/coredata/migrations/20260423T144457Z.sql
Normal file
24
pkg/coredata/migrations/20260423T144457Z.sql
Normal file
@@ -0,0 +1,24 @@
|
||||
-- Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
|
||||
--
|
||||
-- Permission to use, copy, modify, and/or distribute this software for any
|
||||
-- purpose with or without fee is hereby granted, provided that the above
|
||||
-- copyright notice and this permission notice appear in all copies.
|
||||
--
|
||||
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
-- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
-- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
-- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
-- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
-- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
-- PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
ALTER TABLE cookie_categories
|
||||
ADD COLUMN slug TEXT NOT NULL DEFAULT '';
|
||||
|
||||
UPDATE cookie_categories
|
||||
SET slug = LOWER(REGEXP_REPLACE(REGEXP_REPLACE(name, '[^a-zA-Z0-9]+', '-', 'g'), '^-|-$', '', 'g'));
|
||||
|
||||
ALTER TABLE cookie_categories ALTER COLUMN slug DROP DEFAULT;
|
||||
|
||||
CREATE UNIQUE INDEX idx_cookie_categories_unique_slug_per_banner
|
||||
ON cookie_categories (cookie_banner_id, slug);
|
||||
Reference in New Issue
Block a user