From 2494155094691d0ef5344a0455245f2f7a059d32 Mon Sep 17 00:00:00 2001 From: Arthur Belleville Date: Thu, 6 Nov 2025 08:37:52 +0100 Subject: [PATCH] Move migrations to supabase migrations_backup --- sql/01_username_is_not_unique.sql | 5 - sql/03_add_email.sql | 2 - sql/06_sample_data_and_queries.sql | 240 ------------------ .../01_username_is_not_unique.sql | 28 ++ .../02_add_default_values.sql | 0 supabase/migrations_backup/03_add_email.sql | 2 + .../migrations_backup}/04_add_trigger.sql | 0 supabase/migrations_backup/05_add_users.sql | 7 + .../06_sample_data_and_queries.sql | 221 ++++++++++++++++ .../07_create_feedback_table.sql | 0 .../08_create_tablos_table.sql | 0 .../09_create_tablo_invites_table.sql | 0 .../10_create_tablo_access_table.sql | 0 .../11_create_tablo_access_trigger.sql | 0 .../12_update_tablos_id_to_random_string.sql | 0 .../13_create_user_tablos_view.sql | 0 .../14_create_events_table.sql | 0 .../15_create_events_and_tablos_view.sql | 0 .../16_create_calendar_sync_table.sql | 0 .../17_availabilities_table.sql | 0 .../18_event_types_table.sql | 0 .../migrations_backup}/19_standard_name.sql | 0 .../migrations_backup}/20_short_user_id.sql | 0 .../migrations_backup}/21_is_temporary.sql | 0 .../22_add_firstname_lastname.sql | 0 .../23_add_introductions_table.sql | 0 .../24_replace_intro_email_by_json.sql | 0 .../migrations_backup}/25_notes.sql | 0 .../26_create_note_sharing_tables.sql | 0 .../27_add_is_pending_to_invites.sql | 0 .../migrations_backup}/28_modify_trigger.sql | 0 ...29_add_created_at_col_to_tablo_invites.sql | 0 .../30_new_trigger_on_login.sql | 0 .../31_add_rls_for_tablo_invites.sql | 0 ..._add_unique_constraint_to_tablo_access.sql | 0 ...add_unique_constraint_to_tablo_invites.sql | 0 .../33_add_updated_at_column_to_tablos.sql | 0 .../34_modify_firstname_from_email.sql | 0 .../migrations_backup}/35_stripe_wrappers.sql | 0 .../36_fix_stripe_subscription_dates.sql | 0 .../37_secure_active_subscriptions.sql | 0 .../migrations_backup}/38_remove_function.sql | 0 .../39_grant_access_to_schema.sql | 0 .../migrations_backup}/40_debug_trigger.sql | 0 .../cleanup_old_stripe_functions.sql | 0 45 files changed, 258 insertions(+), 247 deletions(-) delete mode 100644 sql/01_username_is_not_unique.sql delete mode 100644 sql/03_add_email.sql delete mode 100644 sql/06_sample_data_and_queries.sql create mode 100644 supabase/migrations_backup/01_username_is_not_unique.sql rename {sql => supabase/migrations_backup}/02_add_default_values.sql (100%) create mode 100644 supabase/migrations_backup/03_add_email.sql rename {sql => supabase/migrations_backup}/04_add_trigger.sql (100%) create mode 100644 supabase/migrations_backup/05_add_users.sql create mode 100644 supabase/migrations_backup/06_sample_data_and_queries.sql rename {sql => supabase/migrations_backup}/07_create_feedback_table.sql (100%) rename {sql => supabase/migrations_backup}/08_create_tablos_table.sql (100%) rename {sql => supabase/migrations_backup}/09_create_tablo_invites_table.sql (100%) rename {sql => supabase/migrations_backup}/10_create_tablo_access_table.sql (100%) rename {sql => supabase/migrations_backup}/11_create_tablo_access_trigger.sql (100%) rename {sql => supabase/migrations_backup}/12_update_tablos_id_to_random_string.sql (100%) rename {sql => supabase/migrations_backup}/13_create_user_tablos_view.sql (100%) rename {sql => supabase/migrations_backup}/14_create_events_table.sql (100%) rename {sql => supabase/migrations_backup}/15_create_events_and_tablos_view.sql (100%) rename {sql => supabase/migrations_backup}/16_create_calendar_sync_table.sql (100%) rename {sql => supabase/migrations_backup}/17_availabilities_table.sql (100%) rename {sql => supabase/migrations_backup}/18_event_types_table.sql (100%) rename {sql => supabase/migrations_backup}/19_standard_name.sql (100%) rename {sql => supabase/migrations_backup}/20_short_user_id.sql (100%) rename {sql => supabase/migrations_backup}/21_is_temporary.sql (100%) rename {sql => supabase/migrations_backup}/22_add_firstname_lastname.sql (100%) rename {sql => supabase/migrations_backup}/23_add_introductions_table.sql (100%) rename {sql => supabase/migrations_backup}/24_replace_intro_email_by_json.sql (100%) rename {sql => supabase/migrations_backup}/25_notes.sql (100%) rename {sql => supabase/migrations_backup}/26_create_note_sharing_tables.sql (100%) rename {sql => supabase/migrations_backup}/27_add_is_pending_to_invites.sql (100%) rename {sql => supabase/migrations_backup}/28_modify_trigger.sql (100%) rename {sql => supabase/migrations_backup}/29_add_created_at_col_to_tablo_invites.sql (100%) rename {sql => supabase/migrations_backup}/30_new_trigger_on_login.sql (100%) rename {sql => supabase/migrations_backup}/31_add_rls_for_tablo_invites.sql (100%) rename {sql => supabase/migrations_backup}/31_add_unique_constraint_to_tablo_access.sql (100%) rename {sql => supabase/migrations_backup}/32_add_unique_constraint_to_tablo_invites.sql (100%) rename {sql => supabase/migrations_backup}/33_add_updated_at_column_to_tablos.sql (100%) rename {sql => supabase/migrations_backup}/34_modify_firstname_from_email.sql (100%) rename {sql => supabase/migrations_backup}/35_stripe_wrappers.sql (100%) rename {sql => supabase/migrations_backup}/36_fix_stripe_subscription_dates.sql (100%) rename {sql => supabase/migrations_backup}/37_secure_active_subscriptions.sql (100%) rename {sql => supabase/migrations_backup}/38_remove_function.sql (100%) rename {sql => supabase/migrations_backup}/39_grant_access_to_schema.sql (100%) rename {sql => supabase/migrations_backup}/40_debug_trigger.sql (100%) rename {sql => supabase/migrations_backup}/cleanup_old_stripe_functions.sql (100%) diff --git a/sql/01_username_is_not_unique.sql b/sql/01_username_is_not_unique.sql deleted file mode 100644 index 93aeade..0000000 --- a/sql/01_username_is_not_unique.sql +++ /dev/null @@ -1,5 +0,0 @@ -ALTER TABLE profiles -DROP CONSTRAINT IF EXISTS profiles_username_key; - --- ALTER TABLE profiles --- ADD CONSTRAINT profiles_username_key UNIQUE (username); diff --git a/sql/03_add_email.sql b/sql/03_add_email.sql deleted file mode 100644 index 83aa586..0000000 --- a/sql/03_add_email.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE profiles -ADD COLUMN email varchar; diff --git a/sql/06_sample_data_and_queries.sql b/sql/06_sample_data_and_queries.sql deleted file mode 100644 index 7f47719..0000000 --- a/sql/06_sample_data_and_queries.sql +++ /dev/null @@ -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; \ No newline at end of file diff --git a/supabase/migrations_backup/01_username_is_not_unique.sql b/supabase/migrations_backup/01_username_is_not_unique.sql new file mode 100644 index 0000000..d20fcb3 --- /dev/null +++ b/supabase/migrations_backup/01_username_is_not_unique.sql @@ -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); diff --git a/sql/02_add_default_values.sql b/supabase/migrations_backup/02_add_default_values.sql similarity index 100% rename from sql/02_add_default_values.sql rename to supabase/migrations_backup/02_add_default_values.sql diff --git a/supabase/migrations_backup/03_add_email.sql b/supabase/migrations_backup/03_add_email.sql new file mode 100644 index 0000000..33d6ddc --- /dev/null +++ b/supabase/migrations_backup/03_add_email.sql @@ -0,0 +1,2 @@ +-- ALTER TABLE profiles +-- ADD COLUMN email varchar; diff --git a/sql/04_add_trigger.sql b/supabase/migrations_backup/04_add_trigger.sql similarity index 100% rename from sql/04_add_trigger.sql rename to supabase/migrations_backup/04_add_trigger.sql diff --git a/supabase/migrations_backup/05_add_users.sql b/supabase/migrations_backup/05_add_users.sql new file mode 100644 index 0000000..6be0d87 --- /dev/null +++ b/supabase/migrations_backup/05_add_users.sql @@ -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"}'); diff --git a/supabase/migrations_backup/06_sample_data_and_queries.sql b/supabase/migrations_backup/06_sample_data_and_queries.sql new file mode 100644 index 0000000..559e21a --- /dev/null +++ b/supabase/migrations_backup/06_sample_data_and_queries.sql @@ -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; \ No newline at end of file diff --git a/sql/07_create_feedback_table.sql b/supabase/migrations_backup/07_create_feedback_table.sql similarity index 100% rename from sql/07_create_feedback_table.sql rename to supabase/migrations_backup/07_create_feedback_table.sql diff --git a/sql/08_create_tablos_table.sql b/supabase/migrations_backup/08_create_tablos_table.sql similarity index 100% rename from sql/08_create_tablos_table.sql rename to supabase/migrations_backup/08_create_tablos_table.sql diff --git a/sql/09_create_tablo_invites_table.sql b/supabase/migrations_backup/09_create_tablo_invites_table.sql similarity index 100% rename from sql/09_create_tablo_invites_table.sql rename to supabase/migrations_backup/09_create_tablo_invites_table.sql diff --git a/sql/10_create_tablo_access_table.sql b/supabase/migrations_backup/10_create_tablo_access_table.sql similarity index 100% rename from sql/10_create_tablo_access_table.sql rename to supabase/migrations_backup/10_create_tablo_access_table.sql diff --git a/sql/11_create_tablo_access_trigger.sql b/supabase/migrations_backup/11_create_tablo_access_trigger.sql similarity index 100% rename from sql/11_create_tablo_access_trigger.sql rename to supabase/migrations_backup/11_create_tablo_access_trigger.sql diff --git a/sql/12_update_tablos_id_to_random_string.sql b/supabase/migrations_backup/12_update_tablos_id_to_random_string.sql similarity index 100% rename from sql/12_update_tablos_id_to_random_string.sql rename to supabase/migrations_backup/12_update_tablos_id_to_random_string.sql diff --git a/sql/13_create_user_tablos_view.sql b/supabase/migrations_backup/13_create_user_tablos_view.sql similarity index 100% rename from sql/13_create_user_tablos_view.sql rename to supabase/migrations_backup/13_create_user_tablos_view.sql diff --git a/sql/14_create_events_table.sql b/supabase/migrations_backup/14_create_events_table.sql similarity index 100% rename from sql/14_create_events_table.sql rename to supabase/migrations_backup/14_create_events_table.sql diff --git a/sql/15_create_events_and_tablos_view.sql b/supabase/migrations_backup/15_create_events_and_tablos_view.sql similarity index 100% rename from sql/15_create_events_and_tablos_view.sql rename to supabase/migrations_backup/15_create_events_and_tablos_view.sql diff --git a/sql/16_create_calendar_sync_table.sql b/supabase/migrations_backup/16_create_calendar_sync_table.sql similarity index 100% rename from sql/16_create_calendar_sync_table.sql rename to supabase/migrations_backup/16_create_calendar_sync_table.sql diff --git a/sql/17_availabilities_table.sql b/supabase/migrations_backup/17_availabilities_table.sql similarity index 100% rename from sql/17_availabilities_table.sql rename to supabase/migrations_backup/17_availabilities_table.sql diff --git a/sql/18_event_types_table.sql b/supabase/migrations_backup/18_event_types_table.sql similarity index 100% rename from sql/18_event_types_table.sql rename to supabase/migrations_backup/18_event_types_table.sql diff --git a/sql/19_standard_name.sql b/supabase/migrations_backup/19_standard_name.sql similarity index 100% rename from sql/19_standard_name.sql rename to supabase/migrations_backup/19_standard_name.sql diff --git a/sql/20_short_user_id.sql b/supabase/migrations_backup/20_short_user_id.sql similarity index 100% rename from sql/20_short_user_id.sql rename to supabase/migrations_backup/20_short_user_id.sql diff --git a/sql/21_is_temporary.sql b/supabase/migrations_backup/21_is_temporary.sql similarity index 100% rename from sql/21_is_temporary.sql rename to supabase/migrations_backup/21_is_temporary.sql diff --git a/sql/22_add_firstname_lastname.sql b/supabase/migrations_backup/22_add_firstname_lastname.sql similarity index 100% rename from sql/22_add_firstname_lastname.sql rename to supabase/migrations_backup/22_add_firstname_lastname.sql diff --git a/sql/23_add_introductions_table.sql b/supabase/migrations_backup/23_add_introductions_table.sql similarity index 100% rename from sql/23_add_introductions_table.sql rename to supabase/migrations_backup/23_add_introductions_table.sql diff --git a/sql/24_replace_intro_email_by_json.sql b/supabase/migrations_backup/24_replace_intro_email_by_json.sql similarity index 100% rename from sql/24_replace_intro_email_by_json.sql rename to supabase/migrations_backup/24_replace_intro_email_by_json.sql diff --git a/sql/25_notes.sql b/supabase/migrations_backup/25_notes.sql similarity index 100% rename from sql/25_notes.sql rename to supabase/migrations_backup/25_notes.sql diff --git a/sql/26_create_note_sharing_tables.sql b/supabase/migrations_backup/26_create_note_sharing_tables.sql similarity index 100% rename from sql/26_create_note_sharing_tables.sql rename to supabase/migrations_backup/26_create_note_sharing_tables.sql diff --git a/sql/27_add_is_pending_to_invites.sql b/supabase/migrations_backup/27_add_is_pending_to_invites.sql similarity index 100% rename from sql/27_add_is_pending_to_invites.sql rename to supabase/migrations_backup/27_add_is_pending_to_invites.sql diff --git a/sql/28_modify_trigger.sql b/supabase/migrations_backup/28_modify_trigger.sql similarity index 100% rename from sql/28_modify_trigger.sql rename to supabase/migrations_backup/28_modify_trigger.sql diff --git a/sql/29_add_created_at_col_to_tablo_invites.sql b/supabase/migrations_backup/29_add_created_at_col_to_tablo_invites.sql similarity index 100% rename from sql/29_add_created_at_col_to_tablo_invites.sql rename to supabase/migrations_backup/29_add_created_at_col_to_tablo_invites.sql diff --git a/sql/30_new_trigger_on_login.sql b/supabase/migrations_backup/30_new_trigger_on_login.sql similarity index 100% rename from sql/30_new_trigger_on_login.sql rename to supabase/migrations_backup/30_new_trigger_on_login.sql diff --git a/sql/31_add_rls_for_tablo_invites.sql b/supabase/migrations_backup/31_add_rls_for_tablo_invites.sql similarity index 100% rename from sql/31_add_rls_for_tablo_invites.sql rename to supabase/migrations_backup/31_add_rls_for_tablo_invites.sql diff --git a/sql/31_add_unique_constraint_to_tablo_access.sql b/supabase/migrations_backup/31_add_unique_constraint_to_tablo_access.sql similarity index 100% rename from sql/31_add_unique_constraint_to_tablo_access.sql rename to supabase/migrations_backup/31_add_unique_constraint_to_tablo_access.sql diff --git a/sql/32_add_unique_constraint_to_tablo_invites.sql b/supabase/migrations_backup/32_add_unique_constraint_to_tablo_invites.sql similarity index 100% rename from sql/32_add_unique_constraint_to_tablo_invites.sql rename to supabase/migrations_backup/32_add_unique_constraint_to_tablo_invites.sql diff --git a/sql/33_add_updated_at_column_to_tablos.sql b/supabase/migrations_backup/33_add_updated_at_column_to_tablos.sql similarity index 100% rename from sql/33_add_updated_at_column_to_tablos.sql rename to supabase/migrations_backup/33_add_updated_at_column_to_tablos.sql diff --git a/sql/34_modify_firstname_from_email.sql b/supabase/migrations_backup/34_modify_firstname_from_email.sql similarity index 100% rename from sql/34_modify_firstname_from_email.sql rename to supabase/migrations_backup/34_modify_firstname_from_email.sql diff --git a/sql/35_stripe_wrappers.sql b/supabase/migrations_backup/35_stripe_wrappers.sql similarity index 100% rename from sql/35_stripe_wrappers.sql rename to supabase/migrations_backup/35_stripe_wrappers.sql diff --git a/sql/36_fix_stripe_subscription_dates.sql b/supabase/migrations_backup/36_fix_stripe_subscription_dates.sql similarity index 100% rename from sql/36_fix_stripe_subscription_dates.sql rename to supabase/migrations_backup/36_fix_stripe_subscription_dates.sql diff --git a/sql/37_secure_active_subscriptions.sql b/supabase/migrations_backup/37_secure_active_subscriptions.sql similarity index 100% rename from sql/37_secure_active_subscriptions.sql rename to supabase/migrations_backup/37_secure_active_subscriptions.sql diff --git a/sql/38_remove_function.sql b/supabase/migrations_backup/38_remove_function.sql similarity index 100% rename from sql/38_remove_function.sql rename to supabase/migrations_backup/38_remove_function.sql diff --git a/sql/39_grant_access_to_schema.sql b/supabase/migrations_backup/39_grant_access_to_schema.sql similarity index 100% rename from sql/39_grant_access_to_schema.sql rename to supabase/migrations_backup/39_grant_access_to_schema.sql diff --git a/sql/40_debug_trigger.sql b/supabase/migrations_backup/40_debug_trigger.sql similarity index 100% rename from sql/40_debug_trigger.sql rename to supabase/migrations_backup/40_debug_trigger.sql diff --git a/sql/cleanup_old_stripe_functions.sql b/supabase/migrations_backup/cleanup_old_stripe_functions.sql similarity index 100% rename from sql/cleanup_old_stripe_functions.sql rename to supabase/migrations_backup/cleanup_old_stripe_functions.sql