Add cookie banner versioning with JSONB snapshots

Introduce append-only cookie_banner_versions table with a JSONB
snapshot of consent-relevant configuration (privacy policy URL,
consent mode, expiry, categories and their cookies). Each version
has its own state (DRAFT/PUBLISHED) separate from the banner
lifecycle.

Replace the banner state enum (DRAFT/PUBLISHED/DISABLED) with a
simpler ACTIVE/INACTIVE toggle. Link consent records to the
specific published version the visitor accepted.

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-04-13 12:42:35 +04:00
parent a153427a08
commit 88315f51e1
10 changed files with 790 additions and 63 deletions

View File

@@ -118,7 +118,7 @@ LIMIT 1;
return nil
}
func (b *CookieBanner) LoadPublishedByID(
func (b *CookieBanner) LoadActiveByID(
ctx context.Context,
conn pg.Querier,
bannerID gid.GID,
@@ -139,7 +139,7 @@ FROM
cookie_banners
WHERE
id = @banner_id
AND state = 'PUBLISHED'
AND state = 'ACTIVE'
LIMIT 1;
`

View File

@@ -22,16 +22,14 @@ import (
type CookieBannerState string
const (
CookieBannerStateDraft CookieBannerState = "DRAFT"
CookieBannerStatePublished CookieBannerState = "PUBLISHED"
CookieBannerStateDisabled CookieBannerState = "DISABLED"
CookieBannerStateActive CookieBannerState = "ACTIVE"
CookieBannerStateInactive CookieBannerState = "INACTIVE"
)
func CookieBannerStates() []CookieBannerState {
return []CookieBannerState{
CookieBannerStateDraft,
CookieBannerStatePublished,
CookieBannerStateDisabled,
CookieBannerStateActive,
CookieBannerStateInactive,
}
}
@@ -51,12 +49,10 @@ func (s *CookieBannerState) Scan(value any) error {
}
switch CookieBannerState(v) {
case CookieBannerStateDraft:
*s = CookieBannerStateDraft
case CookieBannerStatePublished:
*s = CookieBannerStatePublished
case CookieBannerStateDisabled:
*s = CookieBannerStateDisabled
case CookieBannerStateActive:
*s = CookieBannerStateActive
case CookieBannerStateInactive:
*s = CookieBannerStateInactive
default:
return fmt.Errorf("invalid CookieBannerState value: %q", v)
}
@@ -65,9 +61,8 @@ func (s *CookieBannerState) Scan(value any) error {
func (s CookieBannerState) Value() (driver.Value, error) {
switch s {
case CookieBannerStateDraft,
CookieBannerStatePublished,
CookieBannerStateDisabled:
case CookieBannerStateActive,
CookieBannerStateInactive:
return string(s), nil
default:
return nil, fmt.Errorf("invalid CookieBannerState: %s", s)

View File

@@ -0,0 +1,391 @@
// 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.
package coredata
import (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"time"
"github.com/jackc/pgx/v5"
"go.gearno.de/kit/pg"
"go.probo.inc/probo/pkg/gid"
"go.probo.inc/probo/pkg/page"
)
type (
CookieBannerVersionSnapshot struct {
PrivacyPolicyURL string `json:"privacy_policy_url"`
ConsentExpiryDays int `json:"consent_expiry_days"`
ConsentMode string `json:"consent_mode"`
Categories []CookieBannerVersionSnapshotCategory `json:"categories"`
}
CookieBannerVersionSnapshotCategory struct {
Name string `json:"name"`
Description string `json:"description"`
Required bool `json:"required"`
Cookies CookieItems `json:"cookies"`
}
CookieBannerVersion struct {
ID gid.GID `db:"id"`
CookieBannerID gid.GID `db:"cookie_banner_id"`
Version int `db:"version"`
State CookieBannerVersionState `db:"state"`
Snapshot json.RawMessage `db:"snapshot"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
CookieBannerVersions []*CookieBannerVersion
)
func (v *CookieBannerVersion) CursorKey(field CookieBannerVersionOrderField) page.CursorKey {
switch field {
case CookieBannerVersionOrderFieldCreatedAt:
return page.NewCursorKey(v.ID, v.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", field))
}
func (v *CookieBannerVersion) AuthorizationAttributes(ctx context.Context, conn pg.Querier) (map[string]string, error) {
q := `
SELECT cb.organization_id
FROM cookie_banner_versions cbv
JOIN cookie_banners cb ON cbv.cookie_banner_id = cb.id
WHERE cbv.id = $1
LIMIT 1;
`
var organizationID gid.GID
if err := conn.QueryRow(ctx, q, v.ID).Scan(&organizationID); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrResourceNotFound
}
return nil, fmt.Errorf("cannot query cookie banner version authorization attributes: %w", err)
}
return map[string]string{"organization_id": organizationID.String()}, nil
}
func (v *CookieBannerVersion) GetSnapshot() (CookieBannerVersionSnapshot, error) {
var snapshot CookieBannerVersionSnapshot
if err := json.Unmarshal(v.Snapshot, &snapshot); err != nil {
return snapshot, fmt.Errorf("cannot unmarshal cookie banner version snapshot: %w", err)
}
return snapshot, nil
}
func (v *CookieBannerVersion) SetSnapshot(snapshot CookieBannerVersionSnapshot) error {
data, err := json.Marshal(snapshot)
if err != nil {
return fmt.Errorf("cannot marshal cookie banner version snapshot: %w", err)
}
v.Snapshot = data
return nil
}
func (v *CookieBannerVersion) LoadByID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
versionID gid.GID,
) error {
q := `
SELECT
id,
cookie_banner_id,
version,
state,
snapshot,
created_at,
updated_at
FROM
cookie_banner_versions
WHERE
%s
AND id = @version_id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"version_id": versionID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query cookie banner versions: %w", err)
}
version, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[CookieBannerVersion])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrResourceNotFound
}
return fmt.Errorf("cannot collect cookie banner version: %w", err)
}
*v = version
return nil
}
func (v *CookieBannerVersions) LoadByCookieBannerID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieBannerID gid.GID,
cursor *page.Cursor[CookieBannerVersionOrderField],
) error {
q := `
SELECT
id,
cookie_banner_id,
version,
state,
snapshot,
created_at,
updated_at
FROM
cookie_banner_versions
WHERE
%s
AND cookie_banner_id = @cookie_banner_id
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_banner_id": cookieBannerID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, cursor.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query cookie banner versions: %w", err)
}
versions, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[CookieBannerVersion])
if err != nil {
return fmt.Errorf("cannot collect cookie banner versions: %w", err)
}
*v = versions
return nil
}
func (v *CookieBannerVersions) CountByCookieBannerID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieBannerID gid.GID,
) (int, error) {
q := `
SELECT
COUNT(id)
FROM
cookie_banner_versions
WHERE
%s
AND cookie_banner_id = @cookie_banner_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_banner_id": cookieBannerID}
maps.Copy(args, scope.SQLArguments())
row := conn.QueryRow(ctx, q, args)
var count int
if err := row.Scan(&count); err != nil {
return 0, fmt.Errorf("cannot scan count: %w", err)
}
return count, nil
}
func (v *CookieBannerVersion) LoadByCookieBannerIDAndVersion(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieBannerID gid.GID,
version int,
) error {
q := `
SELECT
id,
cookie_banner_id,
version,
state,
snapshot,
created_at,
updated_at
FROM
cookie_banner_versions
WHERE
%s
AND cookie_banner_id = @cookie_banner_id
AND version = @version
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"cookie_banner_id": cookieBannerID,
"version": version,
}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query cookie banner versions: %w", err)
}
ver, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[CookieBannerVersion])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrResourceNotFound
}
return fmt.Errorf("cannot collect cookie banner version: %w", err)
}
*v = ver
return nil
}
func (v *CookieBannerVersion) LoadNextVersion(
ctx context.Context,
conn pg.Querier,
scope Scoper,
cookieBannerID gid.GID,
) (int, error) {
q := `
SELECT
COALESCE(MAX(version), 0) + 1
FROM
cookie_banner_versions
WHERE
%s
AND cookie_banner_id = @cookie_banner_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"cookie_banner_id": cookieBannerID}
maps.Copy(args, scope.SQLArguments())
row := conn.QueryRow(ctx, q, args)
var nextVersion int
if err := row.Scan(&nextVersion); err != nil {
return 0, fmt.Errorf("cannot scan next version: %w", err)
}
return nextVersion, nil
}
func (v *CookieBannerVersion) Insert(
ctx context.Context,
tx pg.Tx,
scope Scoper,
) error {
q := `
INSERT INTO cookie_banner_versions (
id,
tenant_id,
cookie_banner_id,
version,
state,
snapshot,
created_at,
updated_at
) VALUES (
@id,
@tenant_id,
@cookie_banner_id,
@version,
@state,
@snapshot,
@created_at,
@updated_at
)
`
args := pgx.StrictNamedArgs{
"id": v.ID,
"tenant_id": scope.GetTenantID(),
"cookie_banner_id": v.CookieBannerID,
"version": v.Version,
"state": v.State,
"snapshot": v.Snapshot,
"created_at": v.CreatedAt,
"updated_at": v.UpdatedAt,
}
_, err := tx.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot insert cookie banner version: %w", err)
}
return nil
}
func (v *CookieBannerVersion) Update(
ctx context.Context,
tx pg.Tx,
scope Scoper,
) error {
q := `
UPDATE cookie_banner_versions
SET
state = @state,
snapshot = @snapshot,
updated_at = @updated_at
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"id": v.ID,
"state": v.State,
"snapshot": v.Snapshot,
"updated_at": v.UpdatedAt,
}
maps.Copy(args, scope.SQLArguments())
result, err := tx.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot update cookie banner version: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}

View File

@@ -0,0 +1,55 @@
// 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.
package coredata
import "fmt"
type CookieBannerVersionOrderField string
const (
CookieBannerVersionOrderFieldCreatedAt CookieBannerVersionOrderField = "CREATED_AT"
)
func (p CookieBannerVersionOrderField) Column() string {
switch p {
case CookieBannerVersionOrderFieldCreatedAt:
return "created_at"
}
panic(fmt.Sprintf("unsupported order by: %s", p))
}
func (p CookieBannerVersionOrderField) IsValid() bool {
switch p {
case CookieBannerVersionOrderFieldCreatedAt:
return true
}
return false
}
func (p CookieBannerVersionOrderField) String() string {
return string(p)
}
func (p *CookieBannerVersionOrderField) UnmarshalText(text []byte) error {
*p = CookieBannerVersionOrderField(text)
if !p.IsValid() {
return fmt.Errorf("%s is not a valid CookieBannerVersionOrderField", string(text))
}
return nil
}
func (p CookieBannerVersionOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}

View File

@@ -0,0 +1,70 @@
// 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.
package coredata
import (
"database/sql/driver"
"fmt"
)
type CookieBannerVersionState string
const (
CookieBannerVersionStateDraft CookieBannerVersionState = "DRAFT"
CookieBannerVersionStatePublished CookieBannerVersionState = "PUBLISHED"
)
func CookieBannerVersionStates() []CookieBannerVersionState {
return []CookieBannerVersionState{
CookieBannerVersionStateDraft,
CookieBannerVersionStatePublished,
}
}
func (s CookieBannerVersionState) String() string {
return string(s)
}
func (s *CookieBannerVersionState) Scan(value any) error {
var v string
switch val := value.(type) {
case string:
v = val
case []byte:
v = string(val)
default:
return fmt.Errorf("unsupported type for CookieBannerVersionState: %T", value)
}
switch CookieBannerVersionState(v) {
case CookieBannerVersionStateDraft:
*s = CookieBannerVersionStateDraft
case CookieBannerVersionStatePublished:
*s = CookieBannerVersionStatePublished
default:
return fmt.Errorf("invalid CookieBannerVersionState value: %q", v)
}
return nil
}
func (s CookieBannerVersionState) Value() (driver.Value, error) {
switch s {
case CookieBannerVersionStateDraft,
CookieBannerVersionStatePublished:
return string(s), nil
default:
return nil, fmt.Errorf("invalid CookieBannerVersionState: %s", s)
}
}

View File

@@ -30,14 +30,15 @@ import (
type (
CookieConsentRecord struct {
ID gid.GID `db:"id"`
CookieBannerID gid.GID `db:"cookie_banner_id"`
VisitorID string `db:"visitor_id"`
IPAddress *string `db:"ip_address"`
UserAgent *string `db:"user_agent"`
ConsentData json.RawMessage `db:"consent_data"`
Action CookieConsentAction `db:"action"`
CreatedAt time.Time `db:"created_at"`
ID gid.GID `db:"id"`
CookieBannerID gid.GID `db:"cookie_banner_id"`
CookieBannerVersionID gid.GID `db:"cookie_banner_version_id"`
VisitorID string `db:"visitor_id"`
IPAddress *string `db:"ip_address"`
UserAgent *string `db:"user_agent"`
ConsentData json.RawMessage `db:"consent_data"`
Action CookieConsentAction `db:"action"`
CreatedAt time.Time `db:"created_at"`
}
CookieConsentRecords []*CookieConsentRecord
@@ -84,6 +85,7 @@ func (r *CookieConsentRecords) LoadByCookieBannerID(
SELECT
id,
cookie_banner_id,
cookie_banner_version_id,
visitor_id,
ip_address,
user_agent,
@@ -165,6 +167,7 @@ INSERT INTO cookie_consent_records (
id,
tenant_id,
cookie_banner_id,
cookie_banner_version_id,
visitor_id,
ip_address,
user_agent,
@@ -175,6 +178,7 @@ INSERT INTO cookie_consent_records (
@id,
@tenant_id,
@cookie_banner_id,
@cookie_banner_version_id,
@visitor_id,
@ip_address,
@user_agent,
@@ -185,15 +189,16 @@ INSERT INTO cookie_consent_records (
`
args := pgx.StrictNamedArgs{
"id": r.ID,
"tenant_id": scope.GetTenantID(),
"cookie_banner_id": r.CookieBannerID,
"visitor_id": r.VisitorID,
"ip_address": r.IPAddress,
"user_agent": r.UserAgent,
"consent_data": r.ConsentData,
"action": r.Action,
"created_at": r.CreatedAt,
"id": r.ID,
"tenant_id": scope.GetTenantID(),
"cookie_banner_id": r.CookieBannerID,
"cookie_banner_version_id": r.CookieBannerVersionID,
"visitor_id": r.VisitorID,
"ip_address": r.IPAddress,
"user_agent": r.UserAgent,
"consent_data": r.ConsentData,
"action": r.Action,
"created_at": r.CreatedAt,
}
_, err := tx.Exec(ctx, q, args)

View File

@@ -101,6 +101,7 @@ const (
CookieBannerEntityType uint16 = 75
CookieCategoryEntityType uint16 = 76
CookieConsentRecordEntityType uint16 = 77
CookieBannerVersionEntityType uint16 = 78
)
func NewEntityFromID(id gid.GID) (any, bool) {
@@ -253,6 +254,8 @@ func NewEntityFromID(id gid.GID) (any, bool) {
return &CookieCategory{ID: id}, true
case CookieConsentRecordEntityType:
return &CookieConsentRecord{ID: id}, true
case CookieBannerVersionEntityType:
return &CookieBannerVersion{ID: id}, true
default:
return nil, false
}

View File

@@ -0,0 +1,47 @@
-- 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.
-- Replace cookie_banner_state enum: DRAFT/PUBLISHED/DISABLED → ACTIVE/INACTIVE
CREATE TYPE cookie_banner_state_new AS ENUM ('ACTIVE', 'INACTIVE');
ALTER TABLE cookie_banners
ALTER COLUMN state TYPE cookie_banner_state_new
USING CASE
WHEN state::text = 'DISABLED' THEN 'INACTIVE'::cookie_banner_state_new
ELSE 'ACTIVE'::cookie_banner_state_new
END;
DROP TYPE cookie_banner_state;
ALTER TYPE cookie_banner_state_new RENAME TO cookie_banner_state;
-- Version state for cookie banner configuration snapshots
CREATE TYPE cookie_banner_version_state AS ENUM ('DRAFT', 'PUBLISHED');
CREATE TABLE cookie_banner_versions (
id TEXT PRIMARY KEY,
tenant_id TEXT NOT NULL,
cookie_banner_id TEXT NOT NULL REFERENCES cookie_banners(id) ON DELETE CASCADE,
version INTEGER NOT NULL,
state cookie_banner_version_state NOT NULL,
snapshot JSONB NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT cookie_banner_versions_banner_version_key
UNIQUE (cookie_banner_id, version)
);
-- Add version reference to consent records
ALTER TABLE cookie_consent_records
ADD COLUMN cookie_banner_version_id TEXT NOT NULL REFERENCES cookie_banner_versions(id);