Skip to content

Commit 01e0c42

Browse files
committed
Updates to latest schema
1 parent ca74488 commit 01e0c42

File tree

2 files changed

+200
-25
lines changed

2 files changed

+200
-25
lines changed

data/db/schema.sql

Lines changed: 200 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ CREATE EXTENSION IF NOT EXISTS pg_graphql WITH SCHEMA public;
3737
COMMENT ON EXTENSION pg_graphql IS 'GraphQL support';
3838

3939

40+
--
41+
-- Name: graphql_public; Type: SCHEMA; Schema: -; Owner: -
42+
--
43+
44+
CREATE SCHEMA graphql_public;
45+
46+
4047
--
4148
-- Name: pg_net; Type: EXTENSION; Schema: -; Owner: -
4249
--
@@ -300,6 +307,59 @@ $$;
300307
COMMENT ON FUNCTION extensions.grant_pg_cron_access() IS 'Grants access to pg_cron';
301308

302309

310+
--
311+
-- Name: grant_pg_graphql_access(); Type: FUNCTION; Schema: extensions; Owner: -
312+
--
313+
314+
CREATE FUNCTION extensions.grant_pg_graphql_access() RETURNS event_trigger
315+
LANGUAGE plpgsql
316+
AS $_$
317+
DECLARE
318+
func_is_graphql_resolve bool;
319+
BEGIN
320+
func_is_graphql_resolve = (
321+
SELECT n.proname = 'resolve'
322+
FROM pg_event_trigger_ddl_commands() AS ev
323+
LEFT JOIN pg_catalog.pg_proc AS n
324+
ON ev.objid = n.oid
325+
);
326+
327+
IF func_is_graphql_resolve
328+
THEN
329+
grant usage on schema graphql to postgres, anon, authenticated, service_role;
330+
grant all on function graphql.resolve to postgres, anon, authenticated, service_role;
331+
332+
alter default privileges in schema graphql grant all on tables to postgres, anon, authenticated, service_role;
333+
alter default privileges in schema graphql grant all on functions to postgres, anon, authenticated, service_role;
334+
alter default privileges in schema graphql grant all on sequences to postgres, anon, authenticated, service_role;
335+
336+
create or replace function graphql_public.graphql(
337+
"operationName" text default null,
338+
query text default null,
339+
variables jsonb default null,
340+
extensions jsonb default null
341+
)
342+
returns jsonb
343+
language sql
344+
as $$
345+
SELECT graphql.resolve(query, coalesce(variables, '{}'));
346+
$$;
347+
348+
grant select on graphql.field, graphql.type, graphql.enum_value to postgres, anon, authenticated, service_role;
349+
grant execute on function graphql.resolve to postgres, anon, authenticated, service_role;
350+
END IF;
351+
352+
END;
353+
$_$;
354+
355+
356+
--
357+
-- Name: FUNCTION grant_pg_graphql_access(); Type: COMMENT; Schema: extensions; Owner: -
358+
--
359+
360+
COMMENT ON FUNCTION extensions.grant_pg_graphql_access() IS 'Grants access to pg_graphql';
361+
362+
303363
--
304364
-- Name: grant_pg_net_access(); Type: FUNCTION; Schema: extensions; Owner: -
305365
--
@@ -429,6 +489,64 @@ BEGIN
429489
END; $$;
430490

431491

492+
--
493+
-- Name: set_graphql_placeholder(); Type: FUNCTION; Schema: extensions; Owner: -
494+
--
495+
496+
CREATE FUNCTION extensions.set_graphql_placeholder() RETURNS event_trigger
497+
LANGUAGE plpgsql
498+
AS $_$
499+
DECLARE
500+
graphql_is_dropped bool;
501+
BEGIN
502+
graphql_is_dropped = (
503+
SELECT ev.schema_name = 'graphql_public'
504+
FROM pg_event_trigger_dropped_objects() AS ev
505+
WHERE ev.schema_name = 'graphql_public'
506+
);
507+
508+
IF graphql_is_dropped
509+
THEN
510+
create or replace function graphql_public.graphql(
511+
"operationName" text default null,
512+
query text default null,
513+
variables jsonb default null,
514+
extensions jsonb default null
515+
)
516+
returns jsonb
517+
language plpgsql
518+
as $$
519+
DECLARE
520+
server_version float;
521+
BEGIN
522+
server_version = (SELECT (SPLIT_PART((select version()), ' ', 2))::float);
523+
524+
IF server_version >= 14 THEN
525+
RETURN jsonb_build_object(
526+
'data', null::jsonb,
527+
'errors', array['pg_graphql extension is not enabled.']
528+
);
529+
ELSE
530+
RETURN jsonb_build_object(
531+
'data', null::jsonb,
532+
'errors', array['pg_graphql is only available on projects running Postgres 14 onwards.']
533+
);
534+
END IF;
535+
END;
536+
$$;
537+
END IF;
538+
539+
END;
540+
$_$;
541+
542+
543+
--
544+
-- Name: FUNCTION set_graphql_placeholder(); Type: COMMENT; Schema: extensions; Owner: -
545+
--
546+
547+
COMMENT ON FUNCTION extensions.set_graphql_placeholder() IS 'Reintroduces placeholder function for graphql_public.graphql';
548+
549+
432550
--
433551
-- Name: get_auth(text); Type: FUNCTION; Schema: pgbouncer; Owner: -
434552
--
@@ -1068,28 +1186,72 @@ $$;
10681186

10691187

10701188
--
1071-
-- Name: search(text, text, integer, integer, integer); Type: FUNCTION; Schema: storage; Owner: -
1189+
-- Name: search(text, text, integer, integer, integer, text, text, text); Type: FUNCTION; Schema: storage; Owner: -
10721190
--
10731191

1074-
CREATE FUNCTION storage.search(prefix text, bucketname text, limits integer DEFAULT 100, levels integer DEFAULT 1, offsets integer DEFAULT 0) RETURNS TABLE(name text, id uuid, updated_at timestamp with time zone, created_at timestamp with time zone, last_accessed_at timestamp with time zone, metadata jsonb)
1075-
LANGUAGE plpgsql
1076-
AS $$
1077-
BEGIN
1078-
return query
1079-
with files_folders as (
1080-
select path_tokens[levels] as folder
1081-
from storage.objects
1082-
where objects.name ilike prefix || '%'
1083-
and bucket_id = bucketname
1084-
GROUP by folder
1085-
limit limits
1086-
offset offsets
1087-
)
1088-
select files_folders.folder as name, objects.id, objects.updated_at, objects.created_at, objects.last_accessed_at, objects.metadata from files_folders
1089-
left join storage.objects
1090-
on prefix || files_folders.folder = objects.name and objects.bucket_id=bucketname;
1091-
END
1092-
$$;
1192+
CREATE FUNCTION storage.search(prefix text, bucketname text, limits integer DEFAULT 100, levels integer DEFAULT 1, offsets integer DEFAULT 0, search text DEFAULT ''::text, sortcolumn text DEFAULT 'name'::text, sortorder text DEFAULT 'asc'::text) RETURNS TABLE(name text, id uuid, updated_at timestamp with time zone, created_at timestamp with time zone, last_accessed_at timestamp with time zone, metadata jsonb)
1193+
LANGUAGE plpgsql STABLE
1194+
AS $_$
1195+
declare
1196+
v_order_by text;
1197+
v_sort_order text;
1198+
begin
1199+
case
1200+
when sortcolumn = 'name' then
1201+
v_order_by = 'name';
1202+
when sortcolumn = 'updated_at' then
1203+
v_order_by = 'updated_at';
1204+
when sortcolumn = 'created_at' then
1205+
v_order_by = 'created_at';
1206+
when sortcolumn = 'last_accessed_at' then
1207+
v_order_by = 'last_accessed_at';
1208+
else
1209+
v_order_by = 'name';
1210+
end case;
1211+
1212+
case
1213+
when sortorder = 'asc' then
1214+
v_sort_order = 'asc';
1215+
when sortorder = 'desc' then
1216+
v_sort_order = 'desc';
1217+
else
1218+
v_sort_order = 'asc';
1219+
end case;
1220+
1221+
v_order_by = v_order_by || ' ' || v_sort_order;
1222+
1223+
return query execute
1224+
'with folders as (
1225+
select path_tokens[$1] as folder
1226+
from storage.objects
1227+
where objects.name ilike $2 || $3 || ''%''
1228+
and bucket_id = $4
1229+
and array_length(regexp_split_to_array(objects.name, ''/''), 1) <> $1
1230+
group by folder
1231+
order by folder ' || v_sort_order || '
1232+
)
1233+
(select folder as "name",
1234+
null as id,
1235+
null as updated_at,
1236+
null as created_at,
1237+
null as last_accessed_at,
1238+
null as metadata from folders)
1239+
union all
1240+
(select path_tokens[$1] as "name",
1241+
id,
1242+
updated_at,
1243+
created_at,
1244+
last_accessed_at,
1245+
metadata
1246+
from storage.objects
1247+
where objects.name ilike $2 || $3 || ''%''
1248+
and bucket_id = $4
1249+
and array_length(regexp_split_to_array(objects.name, ''/''), 1) = $1
1250+
order by ' || v_order_by || ')
1251+
limit $5
1252+
offset $6' using levels, prefix, search, bucketname, limits, offsets;
1253+
end;
1254+
$_$;
10931255

10941256

10951257
--
@@ -2255,6 +2417,15 @@ CREATE PUBLICATION supabase_realtime WITH (publish = 'insert, update, delete, tr
22552417
ALTER PUBLICATION supabase_realtime ADD TABLE ONLY public."Profile";
22562418

22572419

2420+
--
2421+
-- Name: issue_graphql_placeholder; Type: EVENT TRIGGER; Schema: -; Owner: -
2422+
--
2423+
2424+
CREATE EVENT TRIGGER issue_graphql_placeholder ON sql_drop
2425+
WHEN TAG IN ('DROP EXTENSION')
2426+
EXECUTE FUNCTION extensions.set_graphql_placeholder();
2427+
2428+
22582429
--
22592430
-- Name: issue_pg_cron_access; Type: EVENT TRIGGER; Schema: -; Owner: -
22602431
--
@@ -2264,6 +2435,15 @@ CREATE EVENT TRIGGER issue_pg_cron_access ON ddl_command_end
22642435
EXECUTE FUNCTION extensions.grant_pg_cron_access();
22652436

22662437

2438+
--
2439+
-- Name: issue_pg_graphql_access; Type: EVENT TRIGGER; Schema: -; Owner: -
2440+
--
2441+
2442+
CREATE EVENT TRIGGER issue_pg_graphql_access ON ddl_command_end
2443+
WHEN TAG IN ('CREATE FUNCTION')
2444+
EXECUTE FUNCTION extensions.grant_pg_graphql_access();
2445+
2446+
22672447
--
22682448
-- Name: issue_pg_net_access; Type: EVENT TRIGGER; Schema: -; Owner: -
22692449
--

graphql/schema/schema.graphql

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -688,11 +688,6 @@ type Query {
688688
): VoteConnection
689689
}
690690

691-
type SchemaMigrationsInsertResponse {
692-
"""Count of the records impacted by the mutation"""
693-
affectedCount: Int!
694-
}
695-
696691
"""
697692
Boolean expression comparing fields on type "String"
698693
"""

0 commit comments

Comments
 (0)