Move migrations to supabase migrations_backup
This commit is contained in:
parent
da6e2ab5f8
commit
2494155094
45 changed files with 258 additions and 247 deletions
|
|
@ -1,5 +0,0 @@
|
|||
ALTER TABLE profiles
|
||||
DROP CONSTRAINT IF EXISTS profiles_username_key;
|
||||
|
||||
-- ALTER TABLE profiles
|
||||
-- ADD CONSTRAINT profiles_username_key UNIQUE (username);
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
ALTER TABLE profiles
|
||||
ADD COLUMN email varchar;
|
||||
|
|
@ -1,240 +0,0 @@
|
|||
-- =====================================================
|
||||
-- SAMPLE DATA FOR TABLOS SYSTEM
|
||||
-- =====================================================
|
||||
|
||||
-- Sample tablos data
|
||||
INSERT INTO tablos (id, name, description, color, owner_id, is_public) VALUES
|
||||
('A1B2C3D4E5F6G7H8I9J0K1L2', 'Projet Alpha', 'Développement de la nouvelle application mobile', 'bg-blue-500', auth.uid(), false),
|
||||
('M3N4O5P6Q7R8S9T0U1V2W3X4', 'Marketing Q4', 'Campagnes marketing pour le quatrième trimestre 2024', 'bg-green-500', auth.uid(), true),
|
||||
('Y5Z6A7B8C9D0E1F2G3H4I5J6', 'Équipe Dev', 'Coordination et suivi de l''équipe de développement', 'bg-purple-500', auth.uid(), false),
|
||||
('K7L8M9N0O1P2Q3R4S5T6U7V8', 'Budget 2024', 'Planification et suivi budgétaire pour l''année 2024', 'bg-red-500', auth.uid(), false),
|
||||
('W9X0Y1Z2A3B4C5D6E7F8G9H0', 'Roadmap Produit', 'Feuille de route et évolution du produit', 'bg-yellow-500', auth.uid(), true),
|
||||
('I1J2K3L4M5N6O7P8Q9R0S1T2', 'Support Client', 'Gestion et suivi du support client', 'bg-indigo-500', auth.uid(), false);
|
||||
|
||||
-- Sample boards for each tablo
|
||||
INSERT INTO tablo_boards (tablo_id, name, type, description, position, created_by) VALUES
|
||||
-- Projet Alpha boards
|
||||
('A1B2C3D4E5F6G7H8I9J0K1L2', 'Développement', 'kanban', 'Suivi des tâches de développement', 0, auth.uid()),
|
||||
('A1B2C3D4E5F6G7H8I9J0K1L2', 'Planning', 'calendar', 'Calendrier du projet', 1, auth.uid()),
|
||||
('A1B2C3D4E5F6G7H8I9J0K1L2', 'Discussion', 'chat', 'Chat de l''équipe projet', 2, auth.uid()),
|
||||
|
||||
-- Marketing Q4 boards
|
||||
('M3N4O5P6Q7R8S9T0U1V2W3X4', 'Campagnes', 'kanban', 'Suivi des campagnes marketing', 0, auth.uid()),
|
||||
('M3N4O5P6Q7R8S9T0U1V2W3X4', 'Calendrier Editorial', 'calendar', 'Planning des publications', 1, auth.uid()),
|
||||
|
||||
-- Équipe Dev boards
|
||||
('Y5Z6A7B8C9D0E1F2G3H4I5J6', 'Sprint Board', 'kanban', 'Tableau de bord du sprint actuel', 0, auth.uid()),
|
||||
('Y5Z6A7B8C9D0E1F2G3H4I5J6', 'Backlog', 'table', 'Backlog produit', 1, auth.uid());
|
||||
|
||||
-- Sample lists for Kanban boards
|
||||
INSERT INTO tablo_lists (board_id, name, position, color) VALUES
|
||||
-- For Projet Alpha - Développement board
|
||||
((SELECT id FROM tablo_boards WHERE name = 'Développement' AND tablo_id = 'A1B2C3D4E5F6G7H8I9J0K1L2'), 'À faire', 0, 'bg-gray-200'),
|
||||
((SELECT id FROM tablo_boards WHERE name = 'Développement' AND tablo_id = 'A1B2C3D4E5F6G7H8I9J0K1L2'), 'En cours', 1, 'bg-blue-200'),
|
||||
((SELECT id FROM tablo_boards WHERE name = 'Développement' AND tablo_id = 'A1B2C3D4E5F6G7H8I9J0K1L2'), 'En test', 2, 'bg-yellow-200'),
|
||||
((SELECT id FROM tablo_boards WHERE name = 'Développement' AND tablo_id = 'A1B2C3D4E5F6G7H8I9J0K1L2'), 'Terminé', 3, 'bg-green-200'),
|
||||
|
||||
-- For Marketing Q4 - Campagnes board
|
||||
((SELECT id FROM tablo_boards WHERE name = 'Campagnes' AND tablo_id = 'M3N4O5P6Q7R8S9T0U1V2W3X4'), 'Idées', 0, 'bg-purple-200'),
|
||||
((SELECT id FROM tablo_boards WHERE name = 'Campagnes' AND tablo_id = 'M3N4O5P6Q7R8S9T0U1V2W3X4'), 'En préparation', 1, 'bg-orange-200'),
|
||||
((SELECT id FROM tablo_boards WHERE name = 'Campagnes' AND tablo_id = 'M3N4O5P6Q7R8S9T0U1V2W3X4'), 'En cours', 2, 'bg-blue-200'),
|
||||
((SELECT id FROM tablo_boards WHERE name = 'Campagnes' AND tablo_id = 'M3N4O5P6Q7R8S9T0U1V2W3X4'), 'Terminées', 3, 'bg-green-200');
|
||||
|
||||
-- Sample cards
|
||||
INSERT INTO tablo_cards (list_id, title, description, position, priority, due_date, created_by) VALUES
|
||||
-- Cards for "À faire" list
|
||||
((SELECT id FROM tablo_lists WHERE name = 'À faire' LIMIT 1), 'Créer l''interface utilisateur', 'Développer les écrans principaux de l''application mobile', 0, 'high', NOW() + INTERVAL '1 week', auth.uid()),
|
||||
((SELECT id FROM tablo_lists WHERE name = 'À faire' LIMIT 1), 'Intégration API backend', 'Connecter l''application aux services backend', 1, 'medium', NOW() + INTERVAL '2 weeks', auth.uid()),
|
||||
((SELECT id FROM tablo_lists WHERE name = 'À faire' LIMIT 1), 'Tests unitaires', 'Écrire les tests pour les composants critiques', 2, 'medium', NOW() + INTERVAL '3 weeks', auth.uid()),
|
||||
|
||||
-- Cards for "En cours" list
|
||||
((SELECT id FROM tablo_lists WHERE name = 'En cours' LIMIT 1), 'Configuration base de données', 'Mise en place de la structure de données', 0, 'high', NOW() + INTERVAL '3 days', auth.uid()),
|
||||
((SELECT id FROM tablo_lists WHERE name = 'En cours' LIMIT 1), 'Authentification utilisateur', 'Système de login/logout', 1, 'high', NOW() + INTERVAL '5 days', auth.uid());
|
||||
|
||||
-- Sample chat channels
|
||||
INSERT INTO tablo_chat_channels (tablo_id, name, type, description, created_by) VALUES
|
||||
('A1B2C3D4E5F6G7H8I9J0K1L2', 'général', 'public', 'Discussion générale du projet Alpha', auth.uid()),
|
||||
('A1B2C3D4E5F6G7H8I9J0K1L2', 'dev-team', 'private', 'Canal privé pour l''équipe de développement', auth.uid()),
|
||||
('M3N4O5P6Q7R8S9T0U1V2W3X4', 'marketing-general', 'public', 'Discussion générale marketing', auth.uid()),
|
||||
('Y5Z6A7B8C9D0E1F2G3H4I5J6', 'daily-standup', 'public', 'Daily standup de l''équipe dev', auth.uid());
|
||||
|
||||
-- Sample chat messages
|
||||
INSERT INTO tablo_chat_messages (channel_id, user_id, content, message_type) VALUES
|
||||
((SELECT id FROM tablo_chat_channels WHERE name = 'général' LIMIT 1), auth.uid(), 'Bonjour l''équipe ! Prêts pour le sprint ?', 'text'),
|
||||
((SELECT id FROM tablo_chat_channels WHERE name = 'général' LIMIT 1), auth.uid(), 'Oui, j''ai terminé la configuration de l''environnement', 'text'),
|
||||
((SELECT id FROM tablo_chat_channels WHERE name = 'dev-team' LIMIT 1), auth.uid(), 'Le build est cassé sur la branche develop', 'text'),
|
||||
((SELECT id FROM tablo_chat_channels WHERE name = 'marketing-general' LIMIT 1), auth.uid(), 'Nouvelle campagne lancée ce matin !', 'text');
|
||||
|
||||
-- =====================================================
|
||||
-- USEFUL QUERIES FOR TABLOS SYSTEM
|
||||
-- =====================================================
|
||||
|
||||
-- 1. Get all tablos for a user (owned or member of)
|
||||
/*
|
||||
SELECT DISTINCT t.*, tm.role, tm.permissions
|
||||
FROM tablos t
|
||||
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id AND tm.user_id = auth.uid()
|
||||
WHERE t.owner_id = auth.uid()
|
||||
OR tm.user_id = auth.uid()
|
||||
OR t.is_public = true
|
||||
ORDER BY t.updated_at DESC;
|
||||
*/
|
||||
|
||||
-- 2. Get tablo with all its boards and lists
|
||||
/*
|
||||
SELECT
|
||||
t.name as tablo_name,
|
||||
t.description as tablo_description,
|
||||
b.name as board_name,
|
||||
b.type as board_type,
|
||||
l.name as list_name,
|
||||
l.position as list_position
|
||||
FROM tablos t
|
||||
LEFT JOIN tablo_boards b ON t.id = b.tablo_id
|
||||
LEFT JOIN tablo_lists l ON b.id = l.board_id
|
||||
WHERE t.id = 'your-tablo-id'
|
||||
ORDER BY b.position, l.position;
|
||||
*/
|
||||
|
||||
-- 3. Get cards with assignees for a specific board
|
||||
/*
|
||||
SELECT
|
||||
c.title,
|
||||
c.description,
|
||||
c.priority,
|
||||
c.due_date,
|
||||
l.name as list_name,
|
||||
c.assignees,
|
||||
c.labels
|
||||
FROM tablo_cards c
|
||||
JOIN tablo_lists l ON c.list_id = l.id
|
||||
JOIN tablo_boards b ON l.board_id = b.id
|
||||
WHERE b.id = 'your-board-id'
|
||||
ORDER BY l.position, c.position;
|
||||
*/
|
||||
|
||||
-- 4. Get recent activity for a tablo
|
||||
/*
|
||||
SELECT
|
||||
ta.action,
|
||||
ta.entity_type,
|
||||
ta.details,
|
||||
ta.created_at,
|
||||
p.full_name as user_name
|
||||
FROM tablo_activities ta
|
||||
JOIN profiles p ON ta.user_id = p.id
|
||||
WHERE ta.tablo_id = 'your-tablo-id'
|
||||
ORDER BY ta.created_at DESC
|
||||
LIMIT 20;
|
||||
*/
|
||||
|
||||
-- 5. Get chat messages for a channel with user info
|
||||
/*
|
||||
SELECT
|
||||
tcm.content,
|
||||
tcm.message_type,
|
||||
tcm.created_at,
|
||||
p.full_name as sender_name,
|
||||
p.avatar_url
|
||||
FROM tablo_chat_messages tcm
|
||||
JOIN profiles p ON tcm.user_id = p.id
|
||||
WHERE tcm.channel_id = 'your-channel-id'
|
||||
ORDER BY tcm.created_at ASC;
|
||||
*/
|
||||
|
||||
-- 6. Get overdue cards across all user's tablos
|
||||
/*
|
||||
SELECT
|
||||
c.title,
|
||||
c.due_date,
|
||||
c.priority,
|
||||
t.name as tablo_name,
|
||||
b.name as board_name,
|
||||
l.name as list_name
|
||||
FROM tablo_cards c
|
||||
JOIN tablo_lists l ON c.list_id = l.id
|
||||
JOIN tablo_boards b ON l.board_id = b.id
|
||||
JOIN tablos t ON b.tablo_id = t.id
|
||||
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id
|
||||
WHERE (t.owner_id = auth.uid() OR tm.user_id = auth.uid())
|
||||
AND c.due_date < NOW()
|
||||
AND c.due_date IS NOT NULL
|
||||
ORDER BY c.due_date ASC;
|
||||
*/
|
||||
|
||||
-- 7. Get member statistics for a tablo
|
||||
/*
|
||||
SELECT
|
||||
COUNT(*) as total_members,
|
||||
COUNT(CASE WHEN tm.role = 'owner' THEN 1 END) as owners,
|
||||
COUNT(CASE WHEN tm.role = 'admin' THEN 1 END) as admins,
|
||||
COUNT(CASE WHEN tm.role = 'member' THEN 1 END) as members,
|
||||
COUNT(CASE WHEN tm.role = 'viewer' THEN 1 END) as viewers
|
||||
FROM tablo_members tm
|
||||
WHERE tm.tablo_id = 'your-tablo-id';
|
||||
*/
|
||||
|
||||
-- 8. Search cards by content
|
||||
/*
|
||||
SELECT
|
||||
c.title,
|
||||
c.description,
|
||||
t.name as tablo_name,
|
||||
b.name as board_name,
|
||||
l.name as list_name,
|
||||
ts_rank(to_tsvector('french', c.title || ' ' || COALESCE(c.description, '')),
|
||||
plainto_tsquery('french', 'search-term')) as rank
|
||||
FROM tablo_cards c
|
||||
JOIN tablo_lists l ON c.list_id = l.id
|
||||
JOIN tablo_boards b ON l.board_id = b.id
|
||||
JOIN tablos t ON b.tablo_id = t.id
|
||||
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id
|
||||
WHERE (t.owner_id = auth.uid() OR tm.user_id = auth.uid())
|
||||
AND to_tsvector('french', c.title || ' ' || COALESCE(c.description, ''))
|
||||
@@ plainto_tsquery('french', 'search-term')
|
||||
ORDER BY rank DESC;
|
||||
*/
|
||||
|
||||
-- =====================================================
|
||||
-- VIEWS FOR COMMON QUERIES
|
||||
-- =====================================================
|
||||
|
||||
-- View for user's tablos with member info
|
||||
CREATE VIEW user_tablos AS
|
||||
SELECT DISTINCT
|
||||
t.*,
|
||||
COALESCE(tm.role, 'owner') as user_role,
|
||||
COALESCE(tm.permissions, '{"read": true, "write": true, "admin": true}'::jsonb) as user_permissions,
|
||||
(SELECT COUNT(*) FROM tablo_members WHERE tablo_id = t.id) as member_count
|
||||
FROM tablos t
|
||||
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id AND tm.user_id = auth.uid()
|
||||
WHERE t.owner_id = auth.uid()
|
||||
OR tm.user_id = auth.uid()
|
||||
OR t.is_public = true;
|
||||
|
||||
-- View for tablo structure (boards, lists, cards count)
|
||||
CREATE VIEW tablo_structure AS
|
||||
SELECT
|
||||
t.id as tablo_id,
|
||||
t.name as tablo_name,
|
||||
COUNT(DISTINCT b.id) as boards_count,
|
||||
COUNT(DISTINCT l.id) as lists_count,
|
||||
COUNT(DISTINCT c.id) as cards_count
|
||||
FROM tablos t
|
||||
LEFT JOIN tablo_boards b ON t.id = b.tablo_id
|
||||
LEFT JOIN tablo_lists l ON b.id = l.board_id
|
||||
LEFT JOIN tablo_cards c ON l.id = c.list_id
|
||||
GROUP BY t.id, t.name;
|
||||
|
||||
-- View for recent activities across all user tablos
|
||||
CREATE VIEW user_recent_activities AS
|
||||
SELECT
|
||||
ta.*,
|
||||
t.name as tablo_name,
|
||||
p.full_name as user_name
|
||||
FROM tablo_activities ta
|
||||
JOIN tablos t ON ta.tablo_id = t.id
|
||||
JOIN profiles p ON ta.user_id = p.id
|
||||
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id AND tm.user_id = auth.uid()
|
||||
WHERE t.owner_id = auth.uid() OR tm.user_id = auth.uid()
|
||||
ORDER BY ta.created_at DESC;
|
||||
28
supabase/migrations_backup/01_username_is_not_unique.sql
Normal file
28
supabase/migrations_backup/01_username_is_not_unique.sql
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
-- Create profiles table
|
||||
CREATE TABLE IF NOT EXISTS profiles (
|
||||
id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
|
||||
full_name TEXT,
|
||||
email TEXT,
|
||||
avatar_url TEXT,
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE profiles ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policies
|
||||
CREATE POLICY "Users can view their own profile" ON profiles
|
||||
FOR SELECT USING (auth.uid() = id);
|
||||
|
||||
CREATE POLICY "Users can update their own profile" ON profiles
|
||||
FOR UPDATE USING (auth.uid() = id);
|
||||
|
||||
CREATE POLICY "Users can insert their own profile" ON profiles
|
||||
FOR INSERT WITH CHECK (auth.uid() = id);
|
||||
|
||||
|
||||
ALTER TABLE profiles
|
||||
DROP CONSTRAINT IF EXISTS profiles_username_key;
|
||||
|
||||
-- ALTER TABLE profiles
|
||||
-- ADD CONSTRAINT profiles_username_key UNIQUE (username);
|
||||
2
supabase/migrations_backup/03_add_email.sql
Normal file
2
supabase/migrations_backup/03_add_email.sql
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
-- ALTER TABLE profiles
|
||||
-- ADD COLUMN email varchar;
|
||||
7
supabase/migrations_backup/05_add_users.sql
Normal file
7
supabase/migrations_backup/05_add_users.sql
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
-- Insert sample users into auth.users table
|
||||
INSERT INTO auth.users (id, email, encrypted_password, email_confirmed_at, created_at, updated_at, raw_user_meta_data) VALUES
|
||||
('00000000-0000-0000-0000-000000000001', 'alice.johnson@example.com', crypt('password123', gen_salt('bf')), NOW(), NOW(), NOW(), '{"name": "Alice Johnson", "avatar_url": "https://images.unsplash.com/photo-1494790108755-2616b612b786?w=150"}'),
|
||||
('00000000-0000-0000-0000-000000000002', 'bob.smith@example.com', crypt('password123', gen_salt('bf')), NOW(), NOW(), NOW(), '{"name": "Bob Smith", "avatar_url": "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=150"}'),
|
||||
('00000000-0000-0000-0000-000000000003', 'carol.davis@example.com', crypt('password123', gen_salt('bf')), NOW(), NOW(), NOW(), '{"name": "Carol Davis", "avatar_url": "https://images.unsplash.com/photo-1438761681033-6461ffad8d80?w=150"}'),
|
||||
('00000000-0000-0000-0000-000000000004', 'david.wilson@example.com', crypt('password123', gen_salt('bf')), NOW(), NOW(), NOW(), '{"name": "David Wilson", "avatar_url": "https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=150"}'),
|
||||
('00000000-0000-0000-0000-000000000005', 'emma.brown@example.com', crypt('password123', gen_salt('bf')), NOW(), NOW(), NOW(), '{"name": "Emma Brown", "avatar_url": "https://images.unsplash.com/photo-1544005313-94ddf0286df2?w=150"}');
|
||||
221
supabase/migrations_backup/06_sample_data_and_queries.sql
Normal file
221
supabase/migrations_backup/06_sample_data_and_queries.sql
Normal file
|
|
@ -0,0 +1,221 @@
|
|||
-- =====================================================
|
||||
-- SAMPLE DATA FOR TABLOS SYSTEM
|
||||
-- =====================================================
|
||||
|
||||
-- Create tablos table
|
||||
CREATE TABLE IF NOT EXISTS tablos (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
color VARCHAR(50) DEFAULT 'bg-blue-500',
|
||||
owner_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
|
||||
is_public BOOLEAN DEFAULT false,
|
||||
position INTEGER DEFAULT 0,
|
||||
status VARCHAR(20) DEFAULT 'active',
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP WITH TIME ZONE
|
||||
);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE tablos ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policies for tablos
|
||||
CREATE POLICY "Users can view their own tablos" ON tablos
|
||||
FOR SELECT USING (auth.uid() = owner_id);
|
||||
|
||||
CREATE POLICY "Users can view public tablos" ON tablos
|
||||
FOR SELECT USING (is_public = true);
|
||||
|
||||
CREATE POLICY "Users can insert their own tablos" ON tablos
|
||||
FOR INSERT WITH CHECK (auth.uid() = owner_id);
|
||||
|
||||
CREATE POLICY "Users can update their own tablos" ON tablos
|
||||
FOR UPDATE USING (auth.uid() = owner_id);
|
||||
|
||||
CREATE POLICY "Users can delete their own tablos" ON tablos
|
||||
FOR DELETE USING (auth.uid() = owner_id);
|
||||
|
||||
|
||||
-- Sample tablos data
|
||||
INSERT INTO tablos (name, description, color, owner_id, is_public) VALUES
|
||||
('Projet Alpha', 'Développement de la nouvelle application mobile', 'bg-blue-500', auth.uid(), false),
|
||||
('Marketing Q4', 'Campagnes marketing pour le quatrième trimestre 2024', 'bg-green-500', auth.uid(), true),
|
||||
('Équipe Dev', 'Coordination et suivi de l''équipe de développement', 'bg-purple-500', auth.uid(), false),
|
||||
('Budget 2024', 'Planification et suivi budgétaire pour l''année 2024', 'bg-red-500', auth.uid(), false),
|
||||
('Roadmap Produit', 'Feuille de route et évolution du produit', 'bg-yellow-500', auth.uid(), true),
|
||||
('Support Client', 'Gestion et suivi du support client', 'bg-indigo-500', auth.uid(), false);
|
||||
|
||||
-- =====================================================
|
||||
-- USEFUL QUERIES FOR TABLOS SYSTEM
|
||||
-- =====================================================
|
||||
|
||||
-- 1. Get all tablos for a user (owned or member of)
|
||||
/*
|
||||
SELECT DISTINCT t.*, tm.role, tm.permissions
|
||||
FROM tablos t
|
||||
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id AND tm.user_id = auth.uid()
|
||||
WHERE t.owner_id = auth.uid()
|
||||
OR tm.user_id = auth.uid()
|
||||
OR t.is_public = true
|
||||
ORDER BY t.updated_at DESC;
|
||||
*/
|
||||
|
||||
-- 2. Get tablo with all its boards and lists
|
||||
/*
|
||||
SELECT
|
||||
t.name as tablo_name,
|
||||
t.description as tablo_description,
|
||||
b.name as board_name,
|
||||
b.type as board_type,
|
||||
l.name as list_name,
|
||||
l.position as list_position
|
||||
FROM tablos t
|
||||
LEFT JOIN tablo_boards b ON t.id = b.tablo_id
|
||||
LEFT JOIN tablo_lists l ON b.id = l.board_id
|
||||
WHERE t.id = 'your-tablo-id'
|
||||
ORDER BY b.position, l.position;
|
||||
*/
|
||||
|
||||
-- 3. Get cards with assignees for a specific board
|
||||
/*
|
||||
SELECT
|
||||
c.title,
|
||||
c.description,
|
||||
c.priority,
|
||||
c.due_date,
|
||||
l.name as list_name,
|
||||
c.assignees,
|
||||
c.labels
|
||||
FROM tablo_cards c
|
||||
JOIN tablo_lists l ON c.list_id = l.id
|
||||
JOIN tablo_boards b ON l.board_id = b.id
|
||||
WHERE b.id = 'your-board-id'
|
||||
ORDER BY l.position, c.position;
|
||||
*/
|
||||
|
||||
-- 4. Get recent activity for a tablo
|
||||
/*
|
||||
SELECT
|
||||
ta.action,
|
||||
ta.entity_type,
|
||||
ta.details,
|
||||
ta.created_at,
|
||||
p.full_name as user_name
|
||||
FROM tablo_activities ta
|
||||
JOIN profiles p ON ta.user_id = p.id
|
||||
WHERE ta.tablo_id = 'your-tablo-id'
|
||||
ORDER BY ta.created_at DESC
|
||||
LIMIT 20;
|
||||
*/
|
||||
|
||||
-- 5. Get chat messages for a channel with user info
|
||||
/*
|
||||
SELECT
|
||||
tcm.content,
|
||||
tcm.message_type,
|
||||
tcm.created_at,
|
||||
p.full_name as sender_name,
|
||||
p.avatar_url
|
||||
FROM tablo_chat_messages tcm
|
||||
JOIN profiles p ON tcm.user_id = p.id
|
||||
WHERE tcm.channel_id = 'your-channel-id'
|
||||
ORDER BY tcm.created_at ASC;
|
||||
*/
|
||||
|
||||
-- 6. Get overdue cards across all user's tablos
|
||||
/*
|
||||
SELECT
|
||||
c.title,
|
||||
c.due_date,
|
||||
c.priority,
|
||||
t.name as tablo_name,
|
||||
b.name as board_name,
|
||||
l.name as list_name
|
||||
FROM tablo_cards c
|
||||
JOIN tablo_lists l ON c.list_id = l.id
|
||||
JOIN tablo_boards b ON l.board_id = b.id
|
||||
JOIN tablos t ON b.tablo_id = t.id
|
||||
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id
|
||||
WHERE (t.owner_id = auth.uid() OR tm.user_id = auth.uid())
|
||||
AND c.due_date < NOW()
|
||||
AND c.due_date IS NOT NULL
|
||||
ORDER BY c.due_date ASC;
|
||||
*/
|
||||
|
||||
-- 7. Get member statistics for a tablo
|
||||
/*
|
||||
SELECT
|
||||
COUNT(*) as total_members,
|
||||
COUNT(CASE WHEN tm.role = 'owner' THEN 1 END) as owners,
|
||||
COUNT(CASE WHEN tm.role = 'admin' THEN 1 END) as admins,
|
||||
COUNT(CASE WHEN tm.role = 'member' THEN 1 END) as members,
|
||||
COUNT(CASE WHEN tm.role = 'viewer' THEN 1 END) as viewers
|
||||
FROM tablo_members tm
|
||||
WHERE tm.tablo_id = 'your-tablo-id';
|
||||
*/
|
||||
|
||||
-- 8. Search cards by content
|
||||
/*
|
||||
SELECT
|
||||
c.title,
|
||||
c.description,
|
||||
t.name as tablo_name,
|
||||
b.name as board_name,
|
||||
l.name as list_name,
|
||||
ts_rank(to_tsvector('french', c.title || ' ' || COALESCE(c.description, '')),
|
||||
plainto_tsquery('french', 'search-term')) as rank
|
||||
FROM tablo_cards c
|
||||
JOIN tablo_lists l ON c.list_id = l.id
|
||||
JOIN tablo_boards b ON l.board_id = b.id
|
||||
JOIN tablos t ON b.tablo_id = t.id
|
||||
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id
|
||||
WHERE (t.owner_id = auth.uid() OR tm.user_id = auth.uid())
|
||||
AND to_tsvector('french', c.title || ' ' || COALESCE(c.description, ''))
|
||||
@@ plainto_tsquery('french', 'search-term')
|
||||
ORDER BY rank DESC;
|
||||
*/
|
||||
|
||||
-- =====================================================
|
||||
-- VIEWS FOR COMMON QUERIES
|
||||
-- =====================================================
|
||||
|
||||
-- View for user's tablos with member info
|
||||
CREATE VIEW user_tablos AS
|
||||
SELECT DISTINCT
|
||||
t.*,
|
||||
COALESCE(tm.role, 'owner') as user_role,
|
||||
COALESCE(tm.permissions, '{"read": true, "write": true, "admin": true}'::jsonb) as user_permissions,
|
||||
(SELECT COUNT(*) FROM tablo_members WHERE tablo_id = t.id) as member_count
|
||||
FROM tablos t
|
||||
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id AND tm.user_id = auth.uid()
|
||||
WHERE t.owner_id = auth.uid()
|
||||
OR tm.user_id = auth.uid()
|
||||
OR t.is_public = true;
|
||||
|
||||
-- View for tablo structure (boards, lists, cards count)
|
||||
CREATE VIEW tablo_structure AS
|
||||
SELECT
|
||||
t.id as tablo_id,
|
||||
t.name as tablo_name,
|
||||
COUNT(DISTINCT b.id) as boards_count,
|
||||
COUNT(DISTINCT l.id) as lists_count,
|
||||
COUNT(DISTINCT c.id) as cards_count
|
||||
FROM tablos t
|
||||
LEFT JOIN tablo_boards b ON t.id = b.tablo_id
|
||||
LEFT JOIN tablo_lists l ON b.id = l.board_id
|
||||
LEFT JOIN tablo_cards c ON l.id = c.list_id
|
||||
GROUP BY t.id, t.name;
|
||||
|
||||
-- View for recent activities across all user tablos
|
||||
CREATE VIEW user_recent_activities AS
|
||||
SELECT
|
||||
ta.*,
|
||||
t.name as tablo_name,
|
||||
p.full_name as user_name
|
||||
FROM tablo_activities ta
|
||||
JOIN tablos t ON ta.tablo_id = t.id
|
||||
JOIN profiles p ON ta.user_id = p.id
|
||||
LEFT JOIN tablo_members tm ON t.id = tm.tablo_id AND tm.user_id = auth.uid()
|
||||
WHERE t.owner_id = auth.uid() OR tm.user_id = auth.uid()
|
||||
ORDER BY ta.created_at DESC;
|
||||
Loading…
Reference in a new issue