--
--


SELECT pg_catalog.set_config('search_path', '', false);



--
-- Name: users; Type: TABLE; Schema: public; Owner: timo
--

CREATE TABLE public.users (
    id integer NOT NULL,
    display_name character varying,
    is_admin boolean
);


ALTER TABLE public.users OWNER TO timo;

--
-- Name: my_account(); Type: FUNCTION; Schema: public; Owner: timo
--

CREATE FUNCTION public.my_account() RETURNS public.users
    LANGUAGE sql STABLE SECURITY DEFINER
    AS $$
  select *
  from users
  where cast(id as varchar) = current_setting('jwt.claims.sub', true)
  -- The application will set the current user id at the begin of each transaction.
  -- (See https://postgrest.org or https://graphile.org for example use cases.)
$$;


ALTER FUNCTION public.my_account() OWNER TO timo;

--
-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: timo
--

CREATE SEQUENCE public.users_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.users_id_seq OWNER TO timo;

--
-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: timo
--

ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;


--
-- Name: users id; Type: DEFAULT; Schema: public; Owner: timo
--

ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass);


--
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: timo
--

COPY public.users (id, display_name, is_admin) FROM stdin;
\.


--
-- Name: users_id_seq; Type: SEQUENCE SET; Schema: public; Owner: timo
--

SELECT pg_catalog.setval('public.users_id_seq', 1, false);


--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: timo
--

ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_pkey PRIMARY KEY (id);


--
-- Name: users administrate_accounts; Type: POLICY; Schema: public; Owner: timo
--

CREATE POLICY administrate_accounts ON public.users USING ((EXISTS ( SELECT
   FROM public.my_account() my_account(id, first_name, last_name, display_name, is_admin)
  WHERE my_account.is_admin)));

-- BUG DESCRIPTION: Restoring this policy fails
-- --------------------------------------------
-- Original error message:
--    table "my_account" has 3 columns available but 5 columns specified
--
-- In line 110, pg_dump writes:
--    FROM public.my_account() my_account(id, first_name, last_name, display_name, is_admin)
--
-- Instead of this, I would expect an un-aliased function call, like this:
--    FROM public.my_account()
--
-- I exprect an un-aliased expression, because this would reproduce the code,
-- that I have actually written. The five-column alias resembles the table layout
-- at the time the policy was ADDED, because then the table had five columns.
--
-- However, before the policy was DUMPED, two of those columns had already been dropped.
-- So, if you still prefer the aliased version, a working table alias could look like:
--    FROM public.my_account() my_account(id, display_name, is_admin)
--

--
-- Name: users manage_my_account; Type: POLICY; Schema: public; Owner: timo
--

CREATE POLICY manage_my_account ON public.users USING ((id IN ( SELECT my_account.id
   FROM public.my_account() my_account(id, first_name, last_name, display_name, is_admin))));


--
-- Name: users; Type: ROW SECURITY; Schema: public; Owner: timo
--

ALTER TABLE public.users ENABLE ROW LEVEL SECURITY;

--
--

