refactor(#29): Improve initial migration

This commit is contained in:
Josh Creek
2024-09-03 19:54:41 +01:00
parent ceba7622a9
commit a29df65cbb
@@ -5,38 +5,26 @@
create table users ( create table users (
-- UUID from auth.users -- UUID from auth.users
id uuid references auth.users not null primary key, id uuid references auth.users not null primary key,
full_name text, name text
avatar_url text,
-- The customer's billing address, stored in JSON format.
billing_address jsonb,
-- Stores your customer's payment instruments.
payment_method jsonb
); );
alter table users alter table users enable row level security;
enable row level security; create policy "Can view own user data." on users for select using (auth.uid() = id);
create policy "Can view own user data." on users create policy "Can update own user data." on users for update using (auth.uid() = id);
for select using ((select auth.uid()) = id);
create policy "Can update own user data." on users
for update using ((select auth.uid()) = id);
/** /**
* This trigger automatically creates a user entry when a new user signs up via Supabase Auth. * This trigger automatically creates a user entry when a new user signs up via Supabase Auth.
*/ */
create function public.handle_new_user() create function public.handle_new_user()
returns trigger as returns trigger as $$
$$ begin
begin insert into public.users (id, name)
insert into public.users (id, full_name, avatar_url) values (new.id, new.raw_user_meta_data->>'name');
values (new.id, new.raw_user_meta_data->>'full_name', new.raw_user_meta_data->>'avatar_url'); return new;
return new; end;
end; $$ language plpgsql security definer;
$$
language plpgsql security definer;
create trigger on_auth_user_created create trigger on_auth_user_created
after insert on auth.users after insert on auth.users
for each row for each row execute procedure public.handle_new_user();
execute procedure public.handle_new_user();
/** /**
* CUSTOMERS * CUSTOMERS
@@ -64,15 +52,17 @@ create table products (
name text, name text,
-- The product's description, meant to be displayable to the customer. Use this field to optionally store a long form explanation of the product being sold for your own rendering purposes. -- The product's description, meant to be displayable to the customer. Use this field to optionally store a long form explanation of the product being sold for your own rendering purposes.
description text, description text,
-- A URL of the product image in Stripe, meant to be displayable to the customer. -- URLs of the product images in Stripe, meant to be displayable to the customer.
image text, images text[],
-- A list of up to 15 features for this product. These are displayed in pricing tables.
features text[],
-- Set of key-value pairs, used to store additional information about the object in a structured format. -- Set of key-value pairs, used to store additional information about the object in a structured format.
metadata jsonb metadata jsonb,
"created_at" timestamp with time zone not null default now(),
"updated_at" timestamp with time zone not null default now()
); );
alter table products alter table products enable row level security;
enable row level security; create policy "Allow public read-only access." on products for select using (true);
create policy "Allow public read-only access." on products
for select using (true);
/** /**
* PRICES * PRICES
@@ -83,8 +73,8 @@ create type pricing_plan_interval as enum ('day', 'week', 'month', 'year');
create table prices ( create table prices (
-- Price ID from Stripe, e.g. price_1234. -- Price ID from Stripe, e.g. price_1234.
id text primary key, id text primary key,
-- The ID of the prduct that this price belongs to. -- The ID of the product that this price belongs to.
product_id text references products, product_id text not null references products,
-- Whether the price can be used for new purchases. -- Whether the price can be used for new purchases.
active boolean, active boolean,
-- A brief description of the price. -- A brief description of the price.
@@ -104,16 +94,14 @@ create table prices (
-- Set of key-value pairs, used to store additional information about the object in a structured format. -- Set of key-value pairs, used to store additional information about the object in a structured format.
metadata jsonb metadata jsonb
); );
alter table prices alter table prices enable row level security;
enable row level security; create policy "Allow public read-only access." on prices for select using (true);
create policy "Allow public read-only access." on prices
for select using (true);
/** /**
* SUBSCRIPTIONS * SUBSCRIPTIONS
* Note: subscriptions are created and managed in Stripe and synced to our DB via Stripe webhooks. * Note: subscriptions are created and managed in Stripe and synced to our DB via Stripe webhooks.
*/ */
create type subscription_status as enum ('trialing', 'active', 'canceled', 'incomplete', 'incomplete_expired', 'past_due', 'unpaid'); create type subscription_status as enum ('trialing', 'active', 'canceled', 'incomplete', 'incomplete_expired', 'past_due', 'unpaid', 'paused');
create table subscriptions ( create table subscriptions (
-- Subscription ID from Stripe, e.g. sub_1234. -- Subscription ID from Stripe, e.g. sub_1234.
id text primary key, id text primary key,
@@ -145,15 +133,5 @@ create table subscriptions (
-- If the subscription has a trial, the end of that trial. -- If the subscription has a trial, the end of that trial.
trial_end timestamp with time zone default timezone('utc'::text, now()) trial_end timestamp with time zone default timezone('utc'::text, now())
); );
alter table subscriptions alter table subscriptions enable row level security;
enable row level security; create policy "Can only view own subs data." on subscriptions for select using (auth.uid() = user_id);
create policy "Can only view own subs data." on subscriptions
for select using ((select auth.uid()) = user_id);
/**
* REALTIME SUBSCRIPTIONS
* Only allow realtime listening on public tables.
*/
drop publication if exists supabase_realtime;
create publication supabase_realtime
for table products, prices;