begin;
alter table public.projects drop constraint if exists projects_symbol_check;
alter table public.projects add constraint projects_symbol_check check(symbol ~ '^[A-Za-z0-9]{2,16}$');
alter table public.projects add column if not exists token_icon_url text not null default '';
alter table public.projects add column if not exists price_24h_ago_rial bigint;
alter table public.projects add column if not exists price_history_24h jsonb not null default '[]';
alter table public.projects add column if not exists market_updated_at timestamptz;
alter table public.projects drop constraint if exists projects_token_icon_url_check;
alter table public.projects add constraint projects_token_icon_url_check check(length(token_icon_url)<2048 and (token_icon_url='' or token_icon_url ~ '^https://' or token_icon_url ~ '^/images/[a-zA-Z0-9_.-]+$'));
alter table public.projects drop constraint if exists projects_previous_price_check;
alter table public.projects add constraint projects_previous_price_check check(price_24h_ago_rial is null or (price_24h_ago_rial>0 and price_24h_ago_rial<=900000000000000 and market_updated_at is not null));
alter table public.projects drop constraint if exists projects_price_history_check;
alter table public.projects add constraint projects_price_history_check check(
 jsonb_typeof(price_history_24h)='array' and jsonb_array_length(price_history_24h)<=96 and
 not jsonb_path_exists(price_history_24h,'$[*] ? (@.type() != "number" || @ <= 0 || @ > 900000000000000 || @ != @.floor())') and
 (jsonb_array_length(price_history_24h)=0 or coalesce((jsonb_array_length(price_history_24h)>=2 and
  (price_history_24h->>0)::numeric=price_24h_ago_rial and (price_history_24h->>-1)::numeric=unit_price_rial),false))
);
notify pgrst,'reload schema';
commit;
