fix(ddl): Add storage triggers and fix whatsapp RLS pattern

- Add 06-triggers.sql with trg_files_updated_at and trg_usage_updated_at
  for storage.files and storage.usage tables
- Fix RLS policies in whatsapp schema to use current_setting with true
  parameter to avoid errors when app.current_tenant_id is not set

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Adrian Flores Cortes 2026-02-03 17:18:07 -06:00
parent 0d3e0228f4
commit c1c5892364
3 changed files with 23 additions and 2 deletions

View File

@ -0,0 +1,21 @@
-- =============================================================================
-- TRIGGERS PARA SCHEMA STORAGE
-- =============================================================================
-- Actualiza automaticamente la columna updated_at en las tablas del schema storage
-- Usa la funcion publica set_updated_at() definida en 03-functions.sql
-- Trigger para storage.files
CREATE TRIGGER trg_files_updated_at
BEFORE UPDATE ON storage.files
FOR EACH ROW
EXECUTE FUNCTION public.set_updated_at();
-- Trigger para storage.usage
CREATE TRIGGER trg_usage_updated_at
BEFORE UPDATE ON storage.usage
FOR EACH ROW
EXECUTE FUNCTION public.set_updated_at();
-- Comments
COMMENT ON TRIGGER trg_files_updated_at ON storage.files IS 'Auto-update updated_at timestamp on file record changes';
COMMENT ON TRIGGER trg_usage_updated_at ON storage.usage IS 'Auto-update updated_at timestamp on usage record changes';

View File

@ -47,7 +47,7 @@ CREATE INDEX idx_whatsapp_configs_active ON whatsapp.configs(is_active) WHERE is
ALTER TABLE whatsapp.configs ENABLE ROW LEVEL SECURITY;
CREATE POLICY whatsapp_configs_tenant_isolation ON whatsapp.configs
USING (tenant_id = current_setting('app.current_tenant_id')::uuid);
USING (tenant_id = current_setting('app.current_tenant_id', true)::uuid);
-- Trigger for updated_at
CREATE TRIGGER update_whatsapp_configs_updated_at

View File

@ -59,7 +59,7 @@ CREATE INDEX idx_whatsapp_messages_direction ON whatsapp.messages(direction);
ALTER TABLE whatsapp.messages ENABLE ROW LEVEL SECURITY;
CREATE POLICY whatsapp_messages_tenant_isolation ON whatsapp.messages
USING (tenant_id = current_setting('app.current_tenant_id')::uuid);
USING (tenant_id = current_setting('app.current_tenant_id', true)::uuid);
-- Trigger for updated_at
CREATE TRIGGER update_whatsapp_messages_updated_at