Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 15 additions & 107 deletions packages/database/src/dbTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,44 +891,6 @@ export type Database = {
source_local_id: string | null
space_id: number | null
}
Insert: {
arity?: number | null
author_id?: number | null
created?: string | null
description?: string | null
epistemic_status?:
| Database["public"]["Enums"]["EpistemicStatus"]
| null
id?: number | null
is_schema?: boolean | null
last_modified?: string | null
literal_content?: Json | null
name?: string | null
reference_content?: Json | null
refs?: number[] | null
schema_id?: number | null
source_local_id?: string | null
space_id?: number | null
}
Update: {
arity?: number | null
author_id?: number | null
created?: string | null
description?: string | null
epistemic_status?:
| Database["public"]["Enums"]["EpistemicStatus"]
| null
id?: number | null
is_schema?: boolean | null
last_modified?: string | null
literal_content?: Json | null
name?: string | null
reference_content?: Json | null
refs?: number[] | null
schema_id?: number | null
source_local_id?: string | null
space_id?: number | null
}
Relationships: [
{
foreignKeyName: "Concept_author_id_fkey"
Expand Down Expand Up @@ -990,36 +952,6 @@ export type Database = {
text: string | null
variant: Database["public"]["Enums"]["ContentVariant"] | null
}
Insert: {
author_id?: number | null
created?: string | null
creator_id?: number | null
document_id?: number | null
id?: number | null
last_modified?: string | null
metadata?: Json | null
part_of_id?: number | null
scale?: Database["public"]["Enums"]["Scale"] | null
source_local_id?: string | null
space_id?: number | null
text?: string | null
variant?: Database["public"]["Enums"]["ContentVariant"] | null
}
Update: {
author_id?: number | null
created?: string | null
creator_id?: number | null
document_id?: number | null
id?: number | null
last_modified?: string | null
metadata?: Json | null
part_of_id?: number | null
scale?: Database["public"]["Enums"]["Scale"] | null
source_local_id?: string | null
space_id?: number | null
text?: string | null
variant?: Database["public"]["Enums"]["ContentVariant"] | null
}
Relationships: [
{
foreignKeyName: "Content_author_id_fkey"
Expand Down Expand Up @@ -1210,28 +1142,6 @@ export type Database = {
space_id: number | null
url: string | null
}
Insert: {
author_id?: number | null
contents?: unknown
created?: string | null
id?: number | null
last_modified?: string | null
metadata?: Json | null
source_local_id?: string | null
space_id?: number | null
url?: string | null
}
Update: {
author_id?: number | null
contents?: unknown
created?: string | null
id?: number | null
last_modified?: string | null
metadata?: Json | null
source_local_id?: string | null
space_id?: number | null
url?: string | null
}
Relationships: [
{
foreignKeyName: "Document_author_id_fkey"
Expand Down Expand Up @@ -1272,22 +1182,6 @@ export type Database = {
source_local_id: string | null
space_id: number | null
}
Insert: {
created?: string | null
filehash?: string | null
filepath?: string | null
last_modified?: string | null
source_local_id?: string | null
space_id?: number | null
}
Update: {
created?: string | null
filehash?: string | null
filepath?: string | null
last_modified?: string | null
source_local_id?: string | null
space_id?: number | null
}
Relationships: []
}
my_groups: {
Expand Down Expand Up @@ -1441,6 +1335,7 @@ export type Database = {
}
}
can_access_account: { Args: { account_uid: string }; Returns: boolean }
can_view_content: { Args: { content_id: number }; Returns: boolean }
can_view_specific_resource: {
Args: { source_local_id_: string; space_id_: number }
Returns: boolean
Expand Down Expand Up @@ -1660,6 +1555,16 @@ export type Database = {
text_content: string
}[]
}
my_accessible_resources: {
Args: never
Returns: Database["public"]["CompositeTypes"]["accessible_resource"][]
SetofOptions: {
from: "*"
to: "accessible_resource"
isOneToOne: false
isSetofReturn: true
}
}
my_permissions_in_space: {
Args: { space_id_: number }
Returns: Database["public"]["Enums"]["SpaceAccessPermissions"]
Expand Down Expand Up @@ -1796,6 +1701,10 @@ export type Database = {
task_status: "active" | "timeout" | "complete" | "failed"
}
CompositeTypes: {
accessible_resource: {
space_id: number | null
source_local_id: string | null
}
account_local_input: {
name: string | null
account_local_id: string | null
Expand Down Expand Up @@ -2058,4 +1967,3 @@ export const Constants = {
},
},
} as const

Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
CREATE TYPE public.accessible_resource AS (
space_id bigint,
source_local_id character varying
);

CREATE OR REPLACE FUNCTION public.my_accessible_resources() RETURNS SETOF public.accessible_resource
STRICT STABLE
SET search_path = ''
LANGUAGE sql
AS $$
SELECT DISTINCT space_id, source_local_id FROM public."ResourceAccess"
JOIN public.my_user_accounts() ON (account_uid = my_user_accounts);
$$;

CREATE OR REPLACE VIEW public.my_documents AS
SELECT
id,
space_id,
source_local_id,
url,
"created",
metadata,
last_modified,
author_id,
contents
FROM
public."Document"
LEFT OUTER JOIN public.my_accessible_resources() AS ra USING (space_id, source_local_id)
WHERE (
space_id = any(public.my_space_ids('reader'))
OR (space_id = any(public.my_space_ids('partial')) AND ra.space_id IS NOT NULL)
);

CREATE OR REPLACE VIEW public.my_contents AS
SELECT
id,
document_id,
source_local_id,
variant,
author_id,
creator_id,
created,
text,
metadata,
scale,
space_id,
last_modified,
part_of_id
FROM public."Content"
LEFT OUTER JOIN public.my_accessible_resources() AS ra USING (space_id, source_local_id)
WHERE (
space_id = any(public.my_space_ids('reader'))
OR (space_id = any(public.my_space_ids('partial')) AND ra.space_id IS NOT NULL)
);


CREATE OR REPLACE VIEW public.my_concepts AS
SELECT
id,
epistemic_status,
name,
description,
author_id,
created,
last_modified,
space_id,
arity,
schema_id,
literal_content,
reference_content,
refs,
is_schema,
source_local_id
FROM public."Concept"
LEFT OUTER JOIN public.my_accessible_resources() AS ra USING (space_id, source_local_id)
WHERE (
space_id = any(public.my_space_ids('reader'))
OR (space_id = any(public.my_space_ids('partial')) AND ra.space_id IS NOT NULL)
);

CREATE OR REPLACE VIEW public.my_file_references AS
SELECT
source_local_id,
space_id,
filepath,
filehash,
created,
last_modified
FROM public."FileReference"
LEFT OUTER JOIN public.my_accessible_resources() AS ra USING (space_id, source_local_id)
WHERE (
space_id = any(public.my_space_ids('reader'))
OR (space_id = any(public.my_space_ids('partial')) AND ra.space_id IS NOT NULL)
);

CREATE OR REPLACE VIEW public.my_contents_with_embedding_openai_text_embedding_3_small_1536 AS
SELECT
ct.id,
ct.document_id,
ct.source_local_id,
ct.variant,
ct.author_id,
ct.creator_id,
ct.created,
ct.text,
ct.metadata,
ct.scale,
ct.space_id,
ct.last_modified,
ct.part_of_id,
emb.model,
emb.vector
FROM public."Content" AS ct
JOIN public."ContentEmbedding_openai_text_embedding_3_small_1536" AS emb ON (ct.id = emb.target_id)
LEFT OUTER JOIN public.my_accessible_resources() AS ra USING (space_id, source_local_id)
WHERE (
ct.space_id = any(public.my_space_ids('reader'))
OR (ct.space_id = any(public.my_space_ids('partial')) AND ra.space_id IS NOT NULL)
)
AND NOT emb.obsolete;

CREATE OR REPLACE FUNCTION public.can_view_content(content_id BIGINT) RETURNS BOOLEAN
STABLE
SET search_path = ''
LANGUAGE sql
AS $$
SELECT public.can_view_specific_resource(space_id, source_local_id) FROM public."Content" WHERE id=content_id;
$$;

DROP POLICY IF EXISTS embedding_openai_te3s_1536_policy ON public."ContentEmbedding_openai_text_embedding_3_small_1536";
DROP POLICY IF EXISTS embedding_openai_te3s_1536_select_policy ON public."ContentEmbedding_openai_text_embedding_3_small_1536";
CREATE POLICY embedding_openai_te3s_1536_select_policy ON public."ContentEmbedding_openai_text_embedding_3_small_1536"
FOR SELECT USING (public.content_in_space(target_id) OR public.can_view_content(target_id));
DROP POLICY IF EXISTS embedding_openai_te3s_1536_delete_policy ON public."ContentEmbedding_openai_text_embedding_3_small_1536";
CREATE POLICY embedding_openai_te3s_1536_delete_policy ON public."ContentEmbedding_openai_text_embedding_3_small_1536"
FOR DELETE USING (public.content_in_space(target_id));
DROP POLICY IF EXISTS embedding_openai_te3s_1536_insert_policy ON public."ContentEmbedding_openai_text_embedding_3_small_1536";
CREATE POLICY embedding_openai_te3s_1536_insert_policy ON public."ContentEmbedding_openai_text_embedding_3_small_1536"
FOR INSERT WITH CHECK (public.content_in_space(target_id));
DROP POLICY IF EXISTS embedding_openai_te3s_1536_update_policy ON public."ContentEmbedding_openai_text_embedding_3_small_1536";
CREATE POLICY embedding_openai_te3s_1536_update_policy ON public."ContentEmbedding_openai_text_embedding_3_small_1536"
FOR UPDATE USING (public.content_in_space(target_id));
7 changes: 4 additions & 3 deletions packages/database/supabase/schemas/assets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ SELECT
created,
last_modified
FROM public."FileReference"
LEFT OUTER JOIN public.my_accessible_resources() AS ra USING (space_id, source_local_id)
WHERE (
space_id = any(public.my_space_ids('reader'))
OR public.can_view_specific_resource(space_id, source_local_id)
OR (space_id = any(public.my_space_ids('partial')) AND ra.space_id IS NOT NULL)
);

GRANT ALL ON TABLE public."FileReference" TO authenticated;
Expand Down Expand Up @@ -121,7 +122,7 @@ CREATE TRIGGER on_insert_file_reference_trigger AFTER INSERT ON public."FileRefe
INSERT INTO storage.buckets
(id, name, public)
VALUES
('assets', 'assets', false)
('assets', 'assets', FALSE)
ON CONFLICT (id) DO NOTHING;

DROP POLICY IF EXISTS "storage_insert_assets_authenticated" ON storage.objects;
Expand All @@ -140,7 +141,7 @@ DROP POLICY IF EXISTS "storage_delete_assets_noref" ON storage.objects;
CREATE POLICY "storage_delete_assets_noref"
ON storage.objects FOR DELETE TO authenticated USING (
bucket_id = 'assets' AND NOT EXISTS (
SELECT true FROM public."FileReference"
SELECT TRUE FROM public."FileReference"
WHERE filehash = name LIMIT 1
)
);
Expand Down
3 changes: 2 additions & 1 deletion packages/database/supabase/schemas/concept.sql
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ SELECT
is_schema,
source_local_id
FROM public."Concept"
LEFT OUTER JOIN public.my_accessible_resources() AS ra USING (space_id, source_local_id)
WHERE (
space_id = any(public.my_space_ids('reader'))
OR public.can_view_specific_resource(space_id, source_local_id)
OR (space_id = any(public.my_space_ids('partial')) AND ra.space_id IS NOT null)
);

-- following https://docs.postgrest.org/en/v13/references/api/resource_embedding.html#recursive-relationships
Expand Down
Loading