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

@@ -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);