Spaces:
Runtime error
Runtime error
Download supabase_schema.sql from lasource87/bot: direct link, hf CLI and curl.
- Browser
- Download file 13.3 kB
-
https://huggingface.co/spaces/lasource87/bot/resolve/main/supabase_schema.sql
- Command line
-
hf download hf://spaces/lasource87/bot/supabase_schema.sql
-
curl -L -o supabase_schema.sql https://huggingface.co/spaces/lasource87/bot/resolve/main/supabase_schema.sql
13.3 kB
| -- Enable UUID extension | |
| CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | |
| -- Create categories table | |
| CREATE TABLE IF NOT EXISTS public.categories ( | |
| id uuid NOT NULL DEFAULT uuid_generate_v4(), | |
| name text NOT NULL, | |
| type text DEFAULT 'delivery'::text, | |
| created_at timestamp with time zone DEFAULT now(), | |
| display_order integer DEFAULT 0, | |
| CONSTRAINT categories_pkey PRIMARY KEY (id), | |
| UNIQUE(name, type) | |
| ); | |
| -- Create products table | |
| CREATE TABLE IF NOT EXISTS public.products ( | |
| id uuid NOT NULL DEFAULT uuid_generate_v4(), | |
| category_id uuid, | |
| name text NOT NULL, | |
| description text, | |
| image_url text, | |
| is_in_stock boolean DEFAULT true, | |
| type text DEFAULT 'delivery'::text, | |
| created_at timestamp with time zone DEFAULT now(), | |
| display_order integer DEFAULT 0, | |
| fictitious_likes integer DEFAULT 0, | |
| real_likes_count integer DEFAULT 0, | |
| CONSTRAINT products_pkey PRIMARY KEY (id), | |
| CONSTRAINT products_category_id_fkey FOREIGN KEY (category_id) REFERENCES public.categories(id) ON DELETE CASCADE | |
| ); | |
| -- Create product_likes table to track real likes by user | |
| CREATE TABLE IF NOT EXISTS public.product_likes ( | |
| id uuid NOT NULL DEFAULT uuid_generate_v4(), | |
| product_id uuid, | |
| user_telegram_id text NOT NULL, | |
| created_at timestamp with time zone DEFAULT now(), | |
| CONSTRAINT product_likes_pkey PRIMARY KEY (id), | |
| CONSTRAINT product_likes_product_id_fkey FOREIGN KEY (product_id) REFERENCES public.products(id) ON DELETE CASCADE, | |
| UNIQUE(product_id, user_telegram_id) | |
| ); | |
| -- Enable RLS on product_likes | |
| ALTER TABLE public.product_likes ENABLE ROW LEVEL SECURITY; | |
| -- Policies for product_likes | |
| DROP POLICY IF EXISTS "Allow public read on product_likes" ON public.product_likes; | |
| CREATE POLICY "Allow public read on product_likes" ON public.product_likes FOR SELECT USING (true); | |
| DROP POLICY IF EXISTS "Allow all for admin on product_likes" ON public.product_likes; | |
| CREATE POLICY "Allow all for admin on product_likes" ON public.product_likes FOR ALL USING (true) WITH CHECK (true); | |
| -- Function to update real_likes_count on products table | |
| CREATE OR REPLACE FUNCTION public.update_product_likes_count() | |
| RETURNS TRIGGER AS $$ | |
| BEGIN | |
| IF (TG_OP = 'INSERT') THEN | |
| UPDATE public.products SET real_likes_count = real_likes_count + 1 WHERE id = NEW.product_id; | |
| ELSIF (TG_OP = 'DELETE') THEN | |
| UPDATE public.products SET real_likes_count = real_likes_count - 1 WHERE id = OLD.product_id; | |
| END IF; | |
| RETURN NULL; | |
| END; | |
| $$ LANGUAGE plpgsql SECURITY DEFINER SET search_path = public; | |
| -- Trigger to call the function on insert or delete | |
| DROP TRIGGER IF EXISTS on_product_like ON public.product_likes; | |
| CREATE TRIGGER on_product_like | |
| AFTER INSERT OR DELETE ON public.product_likes | |
| FOR EACH ROW EXECUTE FUNCTION public.update_product_likes_count(); | |
| -- Create pricing table | |
| CREATE TABLE IF NOT EXISTS public.product_prices ( | |
| id uuid NOT NULL DEFAULT uuid_generate_v4(), | |
| product_id uuid, | |
| quantity text NOT NULL, | |
| price numeric NOT NULL, | |
| created_at timestamp with time zone DEFAULT now(), | |
| CONSTRAINT product_prices_pkey PRIMARY KEY (id), | |
| CONSTRAINT product_prices_product_id_fkey FOREIGN KEY (product_id) REFERENCES public.products(id) ON DELETE CASCADE | |
| ); | |
| -- Create shop settings table | |
| CREATE TABLE IF NOT EXISTS public.shop_settings ( | |
| id integer NOT NULL, | |
| name text DEFAULT 'Ma Boutique'::text, | |
| description text DEFAULT ''::text, | |
| logo_url text DEFAULT ''::text, | |
| banner_url text DEFAULT ''::text, | |
| order_manager text DEFAULT ''::text, | |
| channel_url text DEFAULT 'https://t.me/hftfrance'::text, | |
| manager_chat_id text DEFAULT ''::text, | |
| is_open boolean DEFAULT true, | |
| is_meetup_enabled boolean DEFAULT false, | |
| is_verification_enabled boolean DEFAULT false, | |
| is_auto_broadcast_enabled boolean DEFAULT true, | |
| order_dispatch_mode text DEFAULT 'private'::text, | |
| delivery_group_id text DEFAULT ''::text, | |
| meetup_group_id text DEFAULT ''::text, | |
| last_opened_at timestamp with time zone DEFAULT now(), | |
| welcome_message text DEFAULT ''::text, | |
| info_text text DEFAULT ''::text, | |
| accent_color text DEFAULT '#10B981'::text, | |
| admin_id text DEFAULT ''::text, | |
| CONSTRAINT shop_settings_pkey PRIMARY KEY (id) | |
| ); | |
| -- Insert default settings if not exists | |
| INSERT INTO public.shop_settings (id, name, accent_color) | |
| VALUES (1, 'Ma Boutique', '#10B981') | |
| ON CONFLICT (id) DO NOTHING; | |
| -- Create bot_users table | |
| CREATE TABLE IF NOT EXISTS public.bot_users ( | |
| id bigint GENERATED ALWAYS AS IDENTITY NOT NULL, | |
| telegram_id text NOT NULL UNIQUE, | |
| username text, | |
| first_name text, | |
| phone text, | |
| is_verified boolean DEFAULT false, | |
| verification_status text DEFAULT 'none'::text, | |
| referral_info text, | |
| last_seen timestamp with time zone DEFAULT now(), | |
| created_at timestamp with time zone DEFAULT now(), | |
| CONSTRAINT bot_users_pkey PRIMARY KEY (id) | |
| ); | |
| -- Create drivers table | |
| CREATE TABLE IF NOT EXISTS public.drivers ( | |
| id uuid NOT NULL DEFAULT uuid_generate_v4(), | |
| telegram_id text NOT NULL UNIQUE, | |
| username text, | |
| first_name text, | |
| created_at timestamp with time zone DEFAULT now(), | |
| CONSTRAINT drivers_pkey PRIMARY KEY (id) | |
| ); | |
| -- Create meetup_workers table | |
| CREATE TABLE IF NOT EXISTS public.meetup_workers ( | |
| id uuid NOT NULL DEFAULT uuid_generate_v4(), | |
| telegram_id text NOT NULL UNIQUE, | |
| username text, | |
| first_name text, | |
| created_at timestamp with time zone DEFAULT now(), | |
| CONSTRAINT meetup_workers_pkey PRIMARY KEY (id) | |
| ); | |
| -- Create orders table | |
| CREATE TABLE IF NOT EXISTS public.orders ( | |
| id uuid NOT NULL DEFAULT uuid_generate_v4(), | |
| client_telegram_id text, | |
| client_name text, | |
| total numeric, | |
| address text, | |
| items jsonb, | |
| status text DEFAULT 'pending'::text, | |
| type text DEFAULT 'delivery'::text, | |
| driver_id text, | |
| completed_by_name text, | |
| collected_amount numeric, | |
| created_at timestamp with time zone DEFAULT now(), | |
| CONSTRAINT orders_pkey PRIMARY KEY (id) | |
| ); | |
| -- Create order_notifications table | |
| CREATE TABLE IF NOT EXISTS public.order_notifications ( | |
| id uuid NOT NULL DEFAULT uuid_generate_v4(), | |
| order_id uuid, | |
| driver_id text, | |
| message_id integer, | |
| created_at timestamp with time zone DEFAULT now(), | |
| CONSTRAINT order_notifications_pkey PRIMARY KEY (id), | |
| CONSTRAINT order_notifications_order_id_fkey FOREIGN KEY (order_id) REFERENCES public.orders(id) ON DELETE CASCADE | |
| ); | |
| -- Create integrated_chats table | |
| CREATE TABLE IF NOT EXISTS public.integrated_chats ( | |
| id uuid NOT NULL DEFAULT uuid_generate_v4(), | |
| chat_id text NOT NULL UNIQUE, | |
| chat_title text, | |
| chat_type text, | |
| added_at timestamp with time zone DEFAULT now(), | |
| CONSTRAINT integrated_chats_pkey PRIMARY KEY (id) | |
| ); | |
| -- ========================================== | |
| -- SECURITY CONFIGURATION (RLS & POLICIES) | |
| -- ========================================== | |
| -- 1. ENABLE ROW LEVEL SECURITY ON ALL TABLES | |
| ALTER TABLE IF EXISTS public.categories ENABLE ROW LEVEL SECURITY; | |
| ALTER TABLE IF EXISTS public.products ENABLE ROW LEVEL SECURITY; | |
| ALTER TABLE IF EXISTS public.product_prices ENABLE ROW LEVEL SECURITY; | |
| ALTER TABLE IF EXISTS public.shop_settings ENABLE ROW LEVEL SECURITY; | |
| ALTER TABLE IF EXISTS public.bot_users ENABLE ROW LEVEL SECURITY; | |
| ALTER TABLE IF EXISTS public.drivers ENABLE ROW LEVEL SECURITY; | |
| ALTER TABLE IF EXISTS public.meetup_workers ENABLE ROW LEVEL SECURITY; | |
| ALTER TABLE IF EXISTS public.orders ENABLE ROW LEVEL SECURITY; | |
| ALTER TABLE IF EXISTS public.order_notifications ENABLE ROW LEVEL SECURITY; | |
| ALTER TABLE IF EXISTS public.integrated_chats ENABLE ROW LEVEL SECURITY; | |
| ALTER TABLE IF EXISTS public.product_likes ENABLE ROW LEVEL SECURITY; | |
| -- 2. RADICAL CLEANUP: Remove all existing policies to start fresh | |
| DO $$ | |
| DECLARE | |
| pol RECORD; | |
| BEGIN | |
| FOR pol IN ( | |
| SELECT policyname, tablename | |
| FROM pg_policies | |
| WHERE schemaname = 'public' | |
| AND tablename IN ( | |
| 'bot_users', 'orders', 'product_likes', 'shop_settings', | |
| 'categories', 'products', 'product_prices', 'integrated_chats', | |
| 'order_notifications', 'drivers', 'meetup_workers' | |
| ) | |
| ) LOOP | |
| EXECUTE format('DROP POLICY IF EXISTS %I ON public.%I', pol.policyname, pol.tablename); | |
| END LOOP; | |
| END $$; | |
| -- 3. CREATE SECURE POLICIES (Strict Security: No public SELECT on sensitive data) | |
| -- Public Read Access (Non-sensitive data only) | |
| CREATE POLICY "webapp_select_categories" ON public.categories FOR SELECT TO anon, authenticated, service_role USING (id IS NOT NULL); | |
| CREATE POLICY "webapp_select_products" ON public.products FOR SELECT TO anon, authenticated, service_role USING (id IS NOT NULL); | |
| CREATE POLICY "webapp_select_prices" ON public.product_prices FOR SELECT TO anon, authenticated, service_role USING (id IS NOT NULL); | |
| CREATE POLICY "webapp_select_settings" ON public.shop_settings FOR SELECT TO anon, authenticated, service_role USING (id IS NOT NULL); | |
| -- Shop Settings Update (Allow Mini App for Admin/Manager identification) | |
| CREATE POLICY "webapp_update_settings" ON public.shop_settings FOR UPDATE TO anon, authenticated, service_role USING (id IS NOT NULL); | |
| -- Sensitive Tables: NO SELECT for anon (Public) | |
| -- Only service_role can SELECT. anon can only INSERT/UPDATE where necessary. | |
| -- Bot Users: anon can only register or update their own last_seen (via checkAuth) | |
| CREATE POLICY "webapp_insert_users" ON public.bot_users FOR INSERT TO anon, authenticated, service_role WITH CHECK (id IS NOT NULL); | |
| CREATE POLICY "webapp_update_users" ON public.bot_users FOR UPDATE TO anon, authenticated, service_role USING (id IS NOT NULL); | |
| -- No SELECT policy for anon on bot_users -> Dump impossible. | |
| -- Orders: anon can only create orders | |
| CREATE POLICY "webapp_insert_orders" ON public.orders FOR INSERT TO anon, authenticated, service_role WITH CHECK (id IS NOT NULL); | |
| -- No SELECT policy for anon on orders -> Dump impossible. | |
| -- Product Likes Management (Publicly visible counts, but restricted management) | |
| CREATE POLICY "webapp_select_likes" ON public.product_likes FOR SELECT TO anon, authenticated, service_role USING (id IS NOT NULL); | |
| CREATE POLICY "webapp_insert_likes" ON public.product_likes FOR INSERT TO anon, authenticated, service_role WITH CHECK (id IS NOT NULL); | |
| CREATE POLICY "webapp_delete_likes" ON public.product_likes FOR DELETE TO anon, authenticated, service_role USING (id IS NOT NULL); | |
| -- Service Tables: Strictly locked to service_role (Resolves "RLS Enabled No Policy" suggestions) | |
| -- No policies for anon -> Total protection. | |
| CREATE POLICY "service_role_only_drivers" ON public.drivers FOR ALL TO service_role USING (true); | |
| CREATE POLICY "service_role_only_workers" ON public.meetup_workers FOR ALL TO service_role USING (true); | |
| CREATE POLICY "service_role_only_chats" ON public.integrated_chats FOR ALL TO service_role USING (true); | |
| CREATE POLICY "service_role_only_notifs" ON public.order_notifications FOR ALL TO service_role USING (true); | |
| -- 4. GRANT PERMISSIONS | |
| -- service_role (The Bot) bypasses RLS but needs explicit grants for schema usage | |
| GRANT USAGE ON SCHEMA public TO service_role; | |
| GRANT ALL ON ALL TABLES IN SCHEMA public TO service_role; | |
| GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO service_role; | |
| GRANT ALL ON ALL FUNCTIONS IN SCHEMA public TO service_role; | |
| -- authenticated role | |
| GRANT USAGE ON SCHEMA public TO authenticated; | |
| GRANT ALL ON ALL TABLES IN SCHEMA public TO authenticated; | |
| GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO authenticated; | |
| -- anon role (Mini App) - Strictly limited to functional needs | |
| GRANT USAGE ON SCHEMA public TO anon; | |
| GRANT INSERT, UPDATE ON public.bot_users TO anon; -- No SELECT | |
| GRANT SELECT ON public.categories TO anon; | |
| GRANT SELECT ON public.products TO anon; | |
| GRANT SELECT ON public.product_prices TO anon; | |
| GRANT SELECT, UPDATE ON public.shop_settings TO anon; | |
| GRANT INSERT ON public.orders TO anon; -- No SELECT | |
| GRANT SELECT, INSERT, DELETE ON public.product_likes TO anon; | |
| -- Ensure future tables also get these permissions for service_role | |
| ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO service_role; | |
| ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO service_role; | |
| ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON FUNCTIONS TO service_role; | |
| -- ========================================== | |
| -- STORAGE CONFIGURATION (Buckets & Policies) | |
| -- ========================================== | |
| -- Create the bucket for images if it doesn't exist | |
| INSERT INTO storage.buckets (id, name, public) | |
| VALUES ('product-images', 'product-images', true) | |
| ON CONFLICT (id) DO NOTHING; | |
| -- Storage Policies | |
| DROP POLICY IF EXISTS "Public Read Access" ON storage.objects; | |
| CREATE POLICY "Public Read Access" ON storage.objects FOR SELECT TO anon, authenticated, service_role USING (bucket_id = 'product-images'); | |
| DROP POLICY IF EXISTS "Public Upload Access" ON storage.objects; | |
| CREATE POLICY "Public Upload Access" ON storage.objects FOR INSERT TO anon, authenticated, service_role WITH CHECK (bucket_id = 'product-images'); | |
| DROP POLICY IF EXISTS "Public Update Access" ON storage.objects; | |
| CREATE POLICY "Public Update Access" ON storage.objects FOR UPDATE TO anon, authenticated, service_role USING (bucket_id = 'product-images'); | |
| DROP POLICY IF EXISTS "Public Delete Access" ON storage.objects; | |
| CREATE POLICY "Public Delete Access" ON storage.objects FOR DELETE TO anon, authenticated, service_role USING (bucket_id = 'product-images'); | |