Files
warmbly/internal/infrastructure/db/migrations/000039_org_encrypted_keys.down.sql
T
Matthew Meszaros c800081987 feat: store envelope-encryption DEKs per organization
Mailboxes, integration tokens, and message content are organization
assets. Keying their DEK by the connecting user meant offboarding that
user made every ciphertext they created unreadable.

Migration 000039 drops user_encrypted_keys and creates
organization_encrypted_keys keyed by organization_id. The new table has
no FK on purpose: platform secrets live under the zero UUID (no
organizations row), and DEK rows must never cascade-delete because a
lost DEK is unrecoverable.

Pre-production, so there is no data migration; ciphertexts sealed under
the old per-user DEKs are abandoned with the table.
2026-06-10 17:15:47 +02:00

15 lines
538 B
SQL

DROP TABLE IF EXISTS organization_encrypted_keys;
CREATE TABLE user_encrypted_keys (
user_id uuid NOT NULL,
encrypted_data_key text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE ONLY user_encrypted_keys
ADD CONSTRAINT user_encrypted_keys_pkey PRIMARY KEY (user_id);
ALTER TABLE ONLY user_encrypted_keys
ADD CONSTRAINT user_encrypted_keys_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;