-- Migration 1042: Per-user visibility preferences for case modules. -- Existing sag_module_prefs remain case-wide for backwards compatibility. -- New per-user preferences are initially used for the Buzzwords module only. CREATE TABLE IF NOT EXISTS user_sag_module_prefs ( id SERIAL PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES users(user_id) ON DELETE CASCADE, sag_id INTEGER NOT NULL REFERENCES sag_sager(id) ON DELETE CASCADE, module_key VARCHAR(50) NOT NULL, is_enabled BOOLEAN NOT NULL DEFAULT TRUE, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UNIQUE (user_id, sag_id, module_key) ); CREATE INDEX IF NOT EXISTS idx_user_sag_module_prefs_user_sag ON user_sag_module_prefs(user_id, sag_id); CREATE OR REPLACE FUNCTION update_user_sag_module_prefs_updated_at() RETURNS TRIGGER AS $$ BEGIN NEW.updated_at = CURRENT_TIMESTAMP; RETURN NEW; END; $$ LANGUAGE plpgsql; DROP TRIGGER IF EXISTS update_user_sag_module_prefs_updated_at ON user_sag_module_prefs; CREATE TRIGGER update_user_sag_module_prefs_updated_at BEFORE UPDATE ON user_sag_module_prefs FOR EACH ROW EXECUTE FUNCTION update_user_sag_module_prefs_updated_at();