Repository Public
1-- =============================================================================2-- SP BOUCHER | SOURCE FILE3-- =============================================================================4-- Project : KA Maps Engine5-- File : studio/sql/001_init.sql6-- Author : Simon-Pierre Boucher | https://spboucher.ai7-- Contact : support@spboucher.ai8-- Copyright : (c) 2026 Simon-Pierre Boucher9-- License : None specified (no license file in this repository)10-- =============================================================================11-- END OF SP BOUCHER SOURCE HEADER12-- =============================================================================1314-- KA Maps Studio — schéma `studio` (propriétaire : km_studio). Appliqué par `studio migrate`.15CREATE TABLE studio.migration (name text PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT now());1617-- ---------- administrateurs, clés d'accès, sessions18CREATE TABLE studio.admin (19 id uuid PRIMARY KEY DEFAULT gen_random_uuid(),20 name text NOT NULL,21 email text NOT NULL UNIQUE,22 created_at timestamptz NOT NULL DEFAULT now(),23 disabled_at timestamptz24);25CREATE TABLE studio.invite (26 token_hash bytea PRIMARY KEY,27 admin_id uuid NOT NULL REFERENCES studio.admin(id) ON DELETE CASCADE,28 created_at timestamptz NOT NULL DEFAULT now(),29 expires_at timestamptz NOT NULL,30 used_at timestamptz31);32CREATE TABLE studio.credential (33 id text PRIMARY KEY,34 admin_id uuid NOT NULL REFERENCES studio.admin(id) ON DELETE CASCADE,35 public_key bytea NOT NULL,36 counter bigint NOT NULL DEFAULT 0,37 transports text[] NOT NULL DEFAULT '{}',38 device_type text,39 backed_up boolean NOT NULL DEFAULT false,40 label text,41 created_at timestamptz NOT NULL DEFAULT now(),42 last_used_at timestamptz43);44CREATE TABLE studio.challenge (45 id uuid PRIMARY KEY DEFAULT gen_random_uuid(),46 kind text NOT NULL CHECK (kind IN ('register', 'login')),47 challenge text NOT NULL,48 admin_id uuid REFERENCES studio.admin(id) ON DELETE CASCADE,49 expires_at timestamptz NOT NULL50);51CREATE TABLE studio.session (52 id_hash bytea PRIMARY KEY,53 admin_id uuid NOT NULL REFERENCES studio.admin(id) ON DELETE CASCADE,54 csrf text NOT NULL,55 created_at timestamptz NOT NULL DEFAULT now(),56 last_seen timestamptz NOT NULL DEFAULT now(),57 expires_at timestamptz NOT NULL,58 user_agent text59);60CREATE TABLE studio.audit (61 id bigserial PRIMARY KEY,62 at timestamptz NOT NULL DEFAULT now(),63 admin_id uuid,64 admin_name text NOT NULL,65 action text NOT NULL,66 target text,67 detail jsonb NOT NULL DEFAULT '{}',68 ip text69);70CREATE INDEX audit_at ON studio.audit (at DESC);7172-- ---------- thèmes : brouillons, versions immuables, canaux de diffusion73CREATE TABLE studio.theme (74 id text PRIMARY KEY CHECK (id ~ '^[a-z][a-z0-9-]{1,30}$'),75 title text NOT NULL,76 created_at timestamptz NOT NULL DEFAULT now(),77 created_by text NOT NULL,78 archived_at timestamptz79);80CREATE TABLE studio.theme_version (81 theme_id text NOT NULL REFERENCES studio.theme(id),82 version integer NOT NULL CHECK (version >= 1),83 doc jsonb NOT NULL,84 doc_text text NOT NULL, -- octets servis tels quels (le condensé porte sur ce texte)85 sha256 text NOT NULL,86 min_engine text NOT NULL,87 note text NOT NULL DEFAULT '',88 author_id uuid,89 author_name text NOT NULL,90 draft_id bigint,91 created_at timestamptz NOT NULL DEFAULT now(),92 PRIMARY KEY (theme_id, version)93);94CREATE FUNCTION studio.forbid_change() RETURNS trigger LANGUAGE plpgsql AS $$95BEGIN RAISE EXCEPTION 'studio.% est immuable (%)', TG_TABLE_NAME, TG_OP; END $$;96CREATE TRIGGER theme_version_immutable BEFORE UPDATE OR DELETE ON studio.theme_version97 FOR EACH ROW EXECUTE FUNCTION studio.forbid_change();98CREATE TABLE studio.theme_draft (99 id bigserial PRIMARY KEY,100 theme_id text NOT NULL REFERENCES studio.theme(id),101 doc json NOT NULL, -- json (et non jsonb) : garde l'ordre des clés du document102 note text NOT NULL DEFAULT '',103 author_id uuid,104 author_name text NOT NULL,105 base_version integer,106 created_at timestamptz NOT NULL DEFAULT now()107);108CREATE INDEX theme_draft_theme ON studio.theme_draft (theme_id, id DESC);109CREATE TABLE studio.channel (110 theme_id text NOT NULL REFERENCES studio.theme(id),111 app text NOT NULL CHECK (app IN ('louka', 'immoka', 'vraiprix', 'valoplex')),112 platform text NOT NULL CHECK (platform IN ('web', 'ios', 'android')),113 stable_version integer,114 candidate_version integer,115 candidate_percent integer NOT NULL DEFAULT 0 CHECK (candidate_percent BETWEEN 0 AND 100),116 updated_at timestamptz NOT NULL DEFAULT now(),117 updated_by text NOT NULL,118 PRIMARY KEY (theme_id, app, platform),119 FOREIGN KEY (theme_id, stable_version) REFERENCES studio.theme_version (theme_id, version),120 FOREIGN KEY (theme_id, candidate_version) REFERENCES studio.theme_version (theme_id, version),121 CHECK (candidate_version IS NOT NULL OR candidate_percent = 0)122);123CREATE TABLE studio.rollout_event (124 id bigserial PRIMARY KEY,125 at timestamptz NOT NULL DEFAULT now(),126 theme_id text NOT NULL,127 app text NOT NULL,128 platform text NOT NULL,129 action text NOT NULL CHECK (action IN ('publish', 'rollout', 'promote', 'rollback', 'seed', 'cancel')),130 version integer,131 percent integer,132 prev_stable integer,133 prev_candidate integer,134 prev_percent integer,135 author_name text NOT NULL,136 note text NOT NULL DEFAULT ''137);138CREATE INDEX rollout_event_theme ON studio.rollout_event (theme_id, id DESC);139140-- ---------- configuration par application (chaque enregistrement est une révision ; la plus récente est en service)141CREATE TABLE studio.app_config_rev (142 id bigserial PRIMARY KEY,143 app text NOT NULL CHECK (app IN ('louka', 'immoka', 'vraiprix', 'valoplex')),144 config jsonb NOT NULL,145 note text NOT NULL DEFAULT '',146 author_id uuid,147 author_name text NOT NULL,148 created_at timestamptz NOT NULL DEFAULT now()149);150CREATE INDEX app_config_rev_app ON studio.app_config_rev (app, id DESC);151CREATE TRIGGER app_config_rev_immutable BEFORE UPDATE OR DELETE ON studio.app_config_rev152 FOR EACH ROW EXECUTE FUNCTION studio.forbid_change();153154CREATE TABLE studio.icon (155 name text PRIMARY KEY CHECK (name ~ '^[a-z][a-z0-9-]{0,40}$'),156 svg text NOT NULL,157 sha256 text NOT NULL,158 bytes integer NOT NULL,159 updated_at timestamptz NOT NULL DEFAULT now(),160 updated_by text NOT NULL161);162163-- Compteur de révision : incrémenté dans la même transaction que tout changement visible par le164-- service de manifeste, qui recharge son instantané quand il change.165CREATE TABLE studio.state (key text PRIMARY KEY, value bigint NOT NULL);166INSERT INTO studio.state VALUES ('revision', 1);167168-- ---------- exploitation169CREATE TABLE studio.metric_point (170 service text NOT NULL,171 key text NOT NULL,172 ts timestamptz NOT NULL,173 value double precision NOT NULL,174 PRIMARY KEY (service, key, ts)175);176CREATE INDEX metric_point_ts ON studio.metric_point (ts);177CREATE TABLE studio.health_sample (178 service text NOT NULL,179 ts timestamptz NOT NULL,180 ok boolean NOT NULL,181 ms real,182 detail text,183 PRIMARY KEY (service, ts)184);185-- Agrégats seulement : aucun identifiant d'utilisateur, d'appareil, d'adresse ni de position.186CREATE TABLE studio.usage_daily (187 day date NOT NULL,188 app text NOT NULL,189 platform text NOT NULL,190 kind text NOT NULL,191 count bigint NOT NULL DEFAULT 0,192 PRIMARY KEY (day, app, platform, kind)193);194CREATE TABLE studio.tile_probe (195 id bigserial PRIMARY KEY,196 at timestamptz NOT NULL DEFAULT now(),197 author_name text NOT NULL,198 summary jsonb NOT NULL,199 results jsonb NOT NULL200);201CREATE TABLE studio.pipeline_run (202 id bigserial PRIMARY KEY,203 at timestamptz NOT NULL DEFAULT now(),204 author_name text NOT NULL,205 unit text NOT NULL,206 outcome text NOT NULL207);208209-- ---------- droits du service de manifeste (rôle km_maps : lecture de ce qui est publié, écriture des seuls compteurs d'usage)210DO $$211BEGIN212 IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'km_maps') THEN213 GRANT USAGE ON SCHEMA studio TO km_maps;214 GRANT SELECT ON studio.theme, studio.theme_version, studio.channel, studio.app_config_rev, studio.icon, studio.state TO km_maps;215 GRANT SELECT, INSERT, UPDATE ON studio.usage_daily TO km_maps;216 END IF;217END $$;218