|
| 1 | +-- Deploy schemas/meta_public/tables/secrets/table to pg |
| 2 | +-- requires: schemas/meta_public/schema |
| 3 | +-- requires: schemas/meta_public/tables/apps/table |
| 4 | +-- requires: schemas/meta_public/tables/secret_providers/table |
| 5 | + |
| 6 | +BEGIN; |
| 7 | + |
| 8 | +CREATE TABLE IF NOT EXISTS meta_public.secrets ( |
| 9 | + id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 10 | + |
| 11 | + -- Ownership / scope |
| 12 | + owner_type text NOT NULL, -- user | org | app | site |
| 13 | + owner_id uuid NOT NULL, |
| 14 | + app_id uuid NOT NULL, |
| 15 | + |
| 16 | + -- Logical key |
| 17 | + key text NOT NULL, |
| 18 | + |
| 19 | + -- Normalized key for uniqueness (lowercased or citext) |
| 20 | + key_normalized text NOT NULL, |
| 21 | + |
| 22 | + -- Provider linkage |
| 23 | + provider_id uuid NOT NULL, |
| 24 | + provider_ref text NOT NULL, |
| 25 | + |
| 26 | + description text, |
| 27 | + |
| 28 | + is_active boolean NOT NULL DEFAULT true, |
| 29 | + |
| 30 | + created_at timestamptz NOT NULL DEFAULT current_timestamp, |
| 31 | + updated_at timestamptz NOT NULL DEFAULT current_timestamp, |
| 32 | + rotated_at timestamptz |
| 33 | +); |
| 34 | + |
| 35 | +COMMENT ON TABLE meta_public.secrets IS |
| 36 | + 'Metadata for user/org/app secrets; values live in external providers.'; |
| 37 | + |
| 38 | +COMMENT ON COLUMN meta_public.secrets.owner_type IS |
| 39 | + 'Owner type for the secret: user, org, app, or site.'; |
| 40 | + |
| 41 | +COMMENT ON COLUMN meta_public.secrets.owner_id IS |
| 42 | + 'ID of the owning user/org/app/site.'; |
| 43 | + |
| 44 | +COMMENT ON COLUMN meta_public.secrets.app_id IS |
| 45 | + 'Logical app/database this secret is associated with.'; |
| 46 | + |
| 47 | +COMMENT ON COLUMN meta_public.secrets.key IS |
| 48 | + 'Logical secret key name (e.g. MAILGUN_API_KEY).'; |
| 49 | + |
| 50 | +COMMENT ON COLUMN meta_public.secrets.key_normalized IS |
| 51 | + 'Normalized form of key used for uniqueness (e.g. lower(key)).'; |
| 52 | + |
| 53 | +COMMENT ON COLUMN meta_public.secrets.provider_id IS |
| 54 | + 'Foreign key to meta_public.secret_providers.'; |
| 55 | + |
| 56 | +COMMENT ON COLUMN meta_public.secrets.provider_ref IS |
| 57 | + 'Opaque provider-specific reference/path (e.g. OpenBao KV path).'; |
| 58 | + |
| 59 | +ALTER TABLE meta_public.secrets |
| 60 | + ADD CONSTRAINT secrets_app_fkey |
| 61 | + FOREIGN KEY (app_id) |
| 62 | + REFERENCES meta_public.apps (id) |
| 63 | + ON DELETE CASCADE; |
| 64 | + |
| 65 | +ALTER TABLE meta_public.secrets |
| 66 | + ADD CONSTRAINT secrets_provider_fkey |
| 67 | + FOREIGN KEY (provider_id) |
| 68 | + REFERENCES meta_public.secret_providers (id) |
| 69 | + ON DELETE RESTRICT; |
| 70 | + |
| 71 | +CREATE UNIQUE INDEX IF NOT EXISTS secrets_owner_app_key_norm_uniq |
| 72 | + ON meta_public.secrets (owner_type, owner_id, app_id, key_normalized); |
| 73 | + |
| 74 | +COMMIT; |
| 75 | + |
0 commit comments