-- Distribution v1: eingefrorene Übungspakete für Empfängerkanäle. -- Produktiver Abruf bleibt über DISTRIBUTION_ENABLED aus, bis die Testinstanz ihn einschaltet. ALTER TABLE exercises ADD COLUMN IF NOT EXISTS public_id UUID; UPDATE exercises SET public_id = gen_random_uuid() WHERE public_id IS NULL; ALTER TABLE exercises ALTER COLUMN public_id SET DEFAULT gen_random_uuid(); ALTER TABLE exercises ALTER COLUMN public_id SET NOT NULL; CREATE UNIQUE INDEX IF NOT EXISTS uq_exercises_public_id ON exercises (public_id); ALTER TABLE exercise_variants ADD COLUMN IF NOT EXISTS public_id UUID; UPDATE exercise_variants SET public_id = gen_random_uuid() WHERE public_id IS NULL; ALTER TABLE exercise_variants ALTER COLUMN public_id SET DEFAULT gen_random_uuid(); ALTER TABLE exercise_variants ALTER COLUMN public_id SET NOT NULL; CREATE UNIQUE INDEX IF NOT EXISTS uq_exercise_variants_public_id ON exercise_variants (public_id); ALTER TABLE exercise_media ADD COLUMN IF NOT EXISTS public_id UUID; UPDATE exercise_media SET public_id = gen_random_uuid() WHERE public_id IS NULL; ALTER TABLE exercise_media ALTER COLUMN public_id SET DEFAULT gen_random_uuid(); ALTER TABLE exercise_media ALTER COLUMN public_id SET NOT NULL; CREATE UNIQUE INDEX IF NOT EXISTS uq_exercise_media_public_id ON exercise_media (public_id); CREATE TABLE IF NOT EXISTS distribution_publishers ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name TEXT NOT NULL, kind TEXT NOT NULL CHECK (kind IN ('platform', 'own', 'club')), club_id INT REFERENCES clubs (id), status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'suspended')), created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), CONSTRAINT distribution_publishers_kind_club CHECK ( (kind = 'club' AND club_id IS NOT NULL) OR (kind <> 'club' AND club_id IS NULL) ) ); CREATE UNIQUE INDEX IF NOT EXISTS uq_distribution_platform_publisher ON distribution_publishers (kind) WHERE kind = 'platform'; CREATE TABLE IF NOT EXISTS distribution_recipients ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'suspended')), created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE TABLE IF NOT EXISTS distribution_relationships ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), publisher_id UUID NOT NULL REFERENCES distribution_publishers (id), recipient_id UUID NOT NULL REFERENCES distribution_recipients (id), status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'suspended')), UNIQUE (publisher_id, recipient_id) ); CREATE TABLE IF NOT EXISTS distribution_bindings ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), recipient_id UUID NOT NULL REFERENCES distribution_recipients (id), name TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'suspended')), created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE TABLE IF NOT EXISTS distribution_keys ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), binding_id UUID NOT NULL REFERENCES distribution_bindings (id), key_prefix TEXT NOT NULL UNIQUE, secret_hash TEXT NOT NULL, expires_at TIMESTAMPTZ, revoked_at TIMESTAMPTZ, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE TABLE IF NOT EXISTS distribution_releasers ( publisher_id UUID NOT NULL REFERENCES distribution_publishers (id), profile_id INT NOT NULL REFERENCES profiles (id), PRIMARY KEY (publisher_id, profile_id) ); CREATE TABLE IF NOT EXISTS distribution_publisher_exercises ( publisher_id UUID NOT NULL REFERENCES distribution_publishers (id), exercise_id INT NOT NULL REFERENCES exercises (id), PRIMARY KEY (publisher_id, exercise_id) ); CREATE TABLE IF NOT EXISTS distribution_part_identities ( source_id UUID NOT NULL, part_key TEXT NOT NULL, part_id UUID NOT NULL, PRIMARY KEY (source_id, part_key) ); CREATE TABLE IF NOT EXISTS distribution_source_revisions ( source_id UUID NOT NULL, revision INT NOT NULL CHECK (revision > 0), snapshot_hash TEXT NOT NULL, PRIMARY KEY (source_id, revision), UNIQUE (source_id, snapshot_hash) ); CREATE TABLE IF NOT EXISTS distribution_drafts ( id UUID PRIMARY KEY, exercise_id INT NOT NULL REFERENCES exercises (id), publisher_id UUID NOT NULL REFERENCES distribution_publishers (id), recipient_id UUID NOT NULL REFERENCES distribution_recipients (id), prepared_by INT NOT NULL REFERENCES profiles (id), cut JSONB NOT NULL, preview_hash TEXT NOT NULL, release_id UUID, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), confirmed_at TIMESTAMPTZ ); CREATE TABLE IF NOT EXISTS distribution_releases ( release_id UUID PRIMARY KEY, recipient_id UUID NOT NULL REFERENCES distribution_recipients (id), publisher_id UUID NOT NULL REFERENCES distribution_publishers (id), source_id UUID NOT NULL, revision INT NOT NULL, cut_hash TEXT NOT NULL, manifest_text TEXT NOT NULL, manifest_sha256 TEXT NOT NULL, published_at TIMESTAMPTZ NOT NULL, fetch_blocked BOOLEAN NOT NULL DEFAULT FALSE, confirmed_by INT REFERENCES profiles (id) ); CREATE INDEX IF NOT EXISTS idx_distribution_releases_source ON distribution_releases (source_id); CREATE INDEX IF NOT EXISTS idx_distribution_releases_recipient ON distribution_releases (recipient_id); CREATE TABLE IF NOT EXISTS distribution_release_assets ( release_id UUID NOT NULL REFERENCES distribution_releases (release_id), asset_id UUID NOT NULL, sha256 TEXT NOT NULL, size_bytes INT NOT NULL, media_type TEXT NOT NULL, storage_path TEXT NOT NULL, source_media_public_id UUID, source_asset_id INT, PRIMARY KEY (release_id, asset_id) ); CREATE INDEX IF NOT EXISTS idx_distribution_release_assets_source ON distribution_release_assets (source_asset_id); CREATE TABLE IF NOT EXISTS distribution_events ( event_id UUID PRIMARY KEY, recipient_id UUID NOT NULL REFERENCES distribution_recipients (id), seq BIGINT NOT NULL, event_type TEXT NOT NULL, release_id UUID NOT NULL, occurred_at TIMESTAMPTZ NOT NULL, body JSONB NOT NULL, UNIQUE (recipient_id, seq) ); CREATE INDEX IF NOT EXISTS idx_distribution_events_feed ON distribution_events (recipient_id, seq); CREATE TABLE IF NOT EXISTS distribution_evidence ( id TEXT PRIMARY KEY, publisher_id UUID NOT NULL REFERENCES distribution_publishers (id), note TEXT NOT NULL, created_by INT NOT NULL REFERENCES profiles (id), created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE TABLE IF NOT EXISTS distribution_rate_windows ( binding_id UUID PRIMARY KEY REFERENCES distribution_bindings (id), window_start TIMESTAMPTZ NOT NULL, hits INT NOT NULL );