numerus/deploy/product.sql

47 lines
1.1 KiB
MySQL
Raw Normal View History

-- Deploy numerus:product to pg
-- requires: schema_numerus
-- requires: company
-- requires: tax
-- requires: tag_name
begin;
set search_path to numerus, public;
create table product (
product_id serial primary key,
company_id integer not null references company,
slug uuid not null default gen_random_uuid(),
name text not null constraint name_not_empty check(length(trim(name)) > 0),
description text not null default '',
price integer not null,
tags tag_name[] not null default '{}',
created_at timestamptz not null default current_timestamp
);
comment on column product.price is
'Price is stored in cents.';
create index on product using gin (tags);
grant select, insert, update, delete on table product to invoicer;
grant select, insert, update, delete on table product to admin;
grant usage on sequence product_product_id_seq to invoicer;
grant usage on sequence product_product_id_seq to admin;
alter table product enable row level security;
create policy company_policy
on product
using (
exists(
select 1
from company_user
join user_profile using (user_id)
where company_user.company_id = product.company_id
)
);
commit;