Untitled

 avatar
unknown
plain_text
10 months ago
958 B
16
Indexable
create table if not exists checkouts (
  id uuid primary key default gen_random_uuid(),
  user_id uuid references auth.users (id) on delete cascade, -- opsional, kalau user login
  email text not null, -- dari input user
  items jsonb not null, -- contoh: [{ "product_id": "123", "name": "Ebook A", "price": 50000, "quantity": 2 }]
  total_price_cents integer not null,
  status text not null default 'pending' check (status in ('pending','paid','failed','canceled')),
  invoice_url text,
  created_at timestamptz default now(),
  updated_at timestamptz default now()
);

-- auto update updated_at setiap ada update
create or replace function update_updated_at()
returns trigger as $$
begin
  new.updated_at = now();
  return new;
end;
$$ language plpgsql;

create trigger set_updated_at
before update on checkouts
for each row
execute function update_updated_at();

-- aktifkan RLS
alter table checkouts enable row level security;
Editor is loading...
Leave a Comment