xtablo-source/supabase/migrations_backup/10_create_tablo_access_table.sql

37 lines
1.4 KiB
MySQL
Raw Permalink Normal View History

2025-07-03 19:42:49 +00:00
-- Create tablo_access table for managing shared access to tablos
CREATE TABLE IF NOT EXISTS tablo_access (
id SERIAL PRIMARY KEY,
tablo_id INTEGER NOT NULL,
user_id UUID NOT NULL,
granted_by UUID NOT NULL,
is_active BOOLEAN DEFAULT TRUE,
is_admin BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
-- Foreign key constraint to tablos table
CONSTRAINT fk_tablo_access_tablo_id
FOREIGN KEY (tablo_id) REFERENCES tablos(id) ON DELETE CASCADE,
2025-07-04 13:01:46 +00:00
-- Foreign key constraint to users table (auth.users)
CONSTRAINT fk_tablo_access_user_id
FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE,
2025-07-06 15:23:44 +00:00
-- Foreign key constraint to profiles table
CONSTRAINT fk_tablo_access_user_id_from_profiles
FOREIGN KEY (user_id) REFERENCES profiles(id)
2025-07-03 19:42:49 +00:00
);
-- Create indexes for performance
CREATE INDEX IF NOT EXISTS idx_tablo_access_tablo_id ON tablo_access(tablo_id);
CREATE INDEX IF NOT EXISTS idx_tablo_access_user_id ON tablo_access(user_id);
-- Enable Row Level Security
ALTER TABLE tablo_access ENABLE ROW LEVEL SECURITY;
-- Policy to allow users to view tablo_access records where they are the user
CREATE POLICY "Users can view their tablo access only if the access is active" ON tablo_access
FOR SELECT USING (
user_id = (SELECT auth.uid())
AND is_active = TRUE
);