Files
alva/supabase/migrations/20260128002513_9ff3ec88-8d5a-4590-ab11-a68aebc1a3f9.sql
T
gpt-engineer-app[bot] 3ce5a730d5 Changes
2026-01-28 00:26:48 +00:00

50 lines
1.7 KiB
SQL

-- Create customers table
CREATE TABLE public.customers (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email TEXT NOT NULL UNIQUE,
name TEXT,
phone TEXT,
company TEXT,
total_orders INTEGER DEFAULT 0,
total_spent DECIMAL(10,2) DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
);
-- Create payments table
CREATE TABLE public.payments (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
order_id UUID REFERENCES public.orders(id) ON DELETE CASCADE,
amount DECIMAL(10,2) NOT NULL,
currency TEXT NOT NULL DEFAULT 'SEK',
status TEXT NOT NULL DEFAULT 'pending',
payment_method TEXT,
shopify_payment_id TEXT,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
);
-- Add customer reference to orders
ALTER TABLE public.orders ADD COLUMN customer_id UUID REFERENCES public.customers(id);
-- Enable RLS
ALTER TABLE public.customers ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.payments ENABLE ROW LEVEL SECURITY;
-- RLS policies for customers (only authenticated admins)
CREATE POLICY "Authenticated users can view customers"
ON public.customers FOR SELECT TO authenticated USING (true);
CREATE POLICY "Authenticated users can manage customers"
ON public.customers FOR ALL TO authenticated USING (true) WITH CHECK (true);
-- RLS policies for payments
CREATE POLICY "Authenticated users can view payments"
ON public.payments FOR SELECT TO authenticated USING (true);
CREATE POLICY "Service can insert payments"
ON public.payments FOR INSERT WITH CHECK (true);
-- Triggers for updated_at
CREATE TRIGGER update_customers_updated_at
BEFORE UPDATE ON public.customers
FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();