forked from Rathore-Rajpal/gmail-sent-automations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmail_credentials_migration.sql
More file actions
43 lines (36 loc) · 1.4 KB
/
gmail_credentials_migration.sql
File metadata and controls
43 lines (36 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-- Create table for Gmail API credentials
CREATE TABLE IF NOT EXISTS gmail_credentials (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
client_id TEXT NOT NULL,
client_secret TEXT NOT NULL,
access_token TEXT,
refresh_token TEXT,
token_expires_at TIMESTAMP WITH TIME ZONE,
is_active BOOLEAN DEFAULT true,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Enable RLS
ALTER TABLE gmail_credentials ENABLE ROW LEVEL SECURITY;
-- Create policies
CREATE POLICY "Users can view their own Gmail credentials" ON gmail_credentials
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert their own Gmail credentials" ON gmail_credentials
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update their own Gmail credentials" ON gmail_credentials
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete their own Gmail credentials" ON gmail_credentials
FOR DELETE USING (auth.uid() = user_id);
-- Create trigger for updated_at
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER update_gmail_credentials_updated_at
BEFORE UPDATE ON gmail_credentials
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();