2.4 KiB
2.4 KiB
Schema do Banco de Dados
Schema: portal
-- Schema portal
CREATE SCHEMA IF NOT EXISTS portal;
-- Tabela: portal.user_settings
CREATE TABLE portal.user_settings (
user_id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Trigger para auto-update de updated_at
CREATE OR REPLACE FUNCTION portal.update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_user_settings_updated_at
BEFORE UPDATE ON portal.user_settings
FOR EACH ROW
EXECUTE FUNCTION portal.update_updated_at_column();
-- RLS: Usuário só vê próprias configurações
ALTER TABLE portal.user_settings ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can view own settings"
ON portal.user_settings FOR SELECT
USING (auth.uid() = user_id);
CREATE POLICY "Users can update own settings"
ON portal.user_settings FOR UPDATE
USING (auth.uid() = user_id);
-- Tabela: portal.google_calendar_integration
CREATE TABLE portal.google_calendar_integration (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
is_connected BOOLEAN DEFAULT FALSE,
connected_email VARCHAR(255),
connected_at TIMESTAMP WITH TIME ZONE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(user_id)
);
CREATE TRIGGER update_google_calendar_integration_updated_at
BEFORE UPDATE ON portal.google_calendar_integration
FOR EACH ROW
EXECUTE FUNCTION portal.update_updated_at_column();
-- RLS
ALTER TABLE portal.google_calendar_integration ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can view own google calendar integration"
ON portal.google_calendar_integration FOR SELECT
USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own google calendar integration"
ON portal.google_calendar_integration FOR INSERT
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own google calendar integration"
ON portal.google_calendar_integration FOR UPDATE
USING (auth.uid() = user_id);
-- Indexes
CREATE INDEX idx_user_settings_user_id ON portal.user_settings(user_id);
CREATE INDEX idx_google_calendar_integration_user_id ON portal.google_calendar_integration(user_id);
CREATE INDEX idx_google_calendar_integration_is_connected ON portal.google_calendar_integration(is_connected);