-- Deploy numerus:quote_product to pg
-- requires: roles
-- requires: schema_numerus
-- requires: quote
-- requires: discount_rate

begin;

set search_path to numerus, public;

create table quote_product (
	quote_product_id serial primary key,
	quote_id integer not null references quote,
	name text not null constraint name_not_empty check(length(trim(name)) > 0),
	description text not null default '',
	price integer not null,
	quantity integer not null default 1,
	discount_rate discount_rate not null default 0.0
);

grant select, insert, update, delete on table quote_product to invoicer;
grant select, insert, update, delete on table quote_product to admin;

grant usage on sequence quote_product_quote_product_id_seq to invoicer;
grant usage on sequence quote_product_quote_product_id_seq to admin;

alter table quote_product enable row level security;

create policy company_policy
on quote_product
using (
	exists(
		select 1
		from quote
		where quote.quote_id = quote_product.quote_id
	)
);

commit;
