Skip to content

Commit 7255081

Browse files
authored
ENG-430 Add access-token table (#196)
* Add access-token table schema to database types and update config for schema path * Refactor access-token to access_token in database schema and types for consistency. Update permissions and indexes in SQL migration files. * add platform_account_id to access_token and updating related types * rm prettier save on types.gen * Refactor access_token schema and types: replace stace with request_id, remove id, and update constraints.
1 parent 1f504eb commit 7255081

4 files changed

Lines changed: 160 additions & 0 deletions

File tree

packages/database/supabase/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ schema_paths = [
5959
'./schemas/contributor.sql',
6060
'./schemas/sync.sql',
6161
'./schemas/upload_temp.sql',
62+
'./schemas/access_token.sql',
6263
]
6364

6465
[db.seed]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
create table "public"."access_token" (
2+
"request_id" character varying not null,
3+
"access_token" character varying not null,
4+
"code" character varying,
5+
"platform_account_id" bigint,
6+
"created_date" timestamp with time zone not null default timezone('utc'::text, now())
7+
);
8+
9+
10+
CREATE UNIQUE INDEX access_token_access_token_idx ON public.access_token USING btree (access_token);
11+
12+
CREATE INDEX access_token_code_idx ON public.access_token USING btree (code);
13+
14+
CREATE UNIQUE INDEX access_token_pkey ON public.access_token USING btree (request_id);
15+
16+
CREATE INDEX access_token_platform_account_id_idx ON public.access_token USING btree (platform_account_id);
17+
18+
alter table "public"."access_token" add constraint "access_token_pkey" PRIMARY KEY using index "access_token_pkey";
19+
20+
alter table "public"."access_token" add constraint "access_token_code_check" CHECK ((code IS NOT NULL)) not valid;
21+
22+
alter table "public"."access_token" validate constraint "access_token_code_check";
23+
24+
alter table "public"."access_token" add constraint "access_token_platform_account_id_fkey" FOREIGN KEY (platform_account_id) REFERENCES "PlatformAccount"(id) ON UPDATE CASCADE ON DELETE SET NULL not valid;
25+
26+
alter table "public"."access_token" validate constraint "access_token_platform_account_id_fkey";
27+
28+
grant insert on table "public"."access_token" to "anon";
29+
30+
grant select on table "public"."access_token" to "anon";
31+
32+
grant delete on table "public"."access_token" to "authenticated";
33+
34+
grant insert on table "public"."access_token" to "authenticated";
35+
36+
grant references on table "public"."access_token" to "authenticated";
37+
38+
grant select on table "public"."access_token" to "authenticated";
39+
40+
grant trigger on table "public"."access_token" to "authenticated";
41+
42+
grant truncate on table "public"."access_token" to "authenticated";
43+
44+
grant update on table "public"."access_token" to "authenticated";
45+
46+
grant delete on table "public"."access_token" to "service_role";
47+
48+
grant insert on table "public"."access_token" to "service_role";
49+
50+
grant references on table "public"."access_token" to "service_role";
51+
52+
grant select on table "public"."access_token" to "service_role";
53+
54+
grant trigger on table "public"."access_token" to "service_role";
55+
56+
grant truncate on table "public"."access_token" to "service_role";
57+
58+
grant update on table "public"."access_token" to "service_role";
59+
60+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
create table "access_token" (
2+
request_id varchar primary key,
3+
-- TODO encrypt this (look into supabase vault)
4+
access_token varchar not null,
5+
code varchar,
6+
platform_account_id bigint,
7+
created_date timestamp with time zone default timezone('utc'::text, now()) not null,
8+
constraint access_token_code_check check (
9+
code is not null
10+
),
11+
constraint access_token_platform_account_id_fkey foreign key (platform_account_id)
12+
references public."PlatformAccount" (id) on update cascade on delete set null
13+
);
14+
15+
create unique index access_token_access_token_idx on "access_token" ("access_token");
16+
create index access_token_code_idx on "access_token" (code);
17+
create index access_token_platform_account_id_idx on "access_token" (platform_account_id);
18+
19+
-- Revoke dangerous permissions from anon role
20+
revoke delete on table "public"."access_token" from "anon";
21+
revoke truncate on table "public"."access_token" from "anon";
22+
revoke update on table "public"."access_token" from "anon";
23+
revoke references on table "public"."access_token" from "anon";
24+
revoke trigger on table "public"."access_token" from "anon";
25+
26+
-- Ensure only necessary permissions remain for anon role
27+
grant select on table "public"."access_token" to "anon";
28+
grant insert on table "public"."access_token" to "anon";

packages/database/types.gen.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,38 @@ export type Json =
99
export type Database = {
1010
public: {
1111
Tables: {
12+
access_token: {
13+
Row: {
14+
access_token: string
15+
code: string | null
16+
created_date: string
17+
platform_account_id: number | null
18+
request_id: string
19+
}
20+
Insert: {
21+
access_token: string
22+
code?: string | null
23+
created_date?: string
24+
platform_account_id?: number | null
25+
request_id: string
26+
}
27+
Update: {
28+
access_token?: string
29+
code?: string | null
30+
created_date?: string
31+
platform_account_id?: number | null
32+
request_id?: string
33+
}
34+
Relationships: [
35+
{
36+
foreignKeyName: "access_token_platform_account_id_fkey"
37+
columns: ["platform_account_id"]
38+
isOneToOne: false
39+
referencedRelation: "PlatformAccount"
40+
referencedColumns: ["id"]
41+
},
42+
]
43+
}
1244
AgentIdentifier: {
1345
Row: {
1446
account_id: number
@@ -467,6 +499,25 @@ export type Database = {
467499
[_ in never]: never
468500
}
469501
Functions: {
502+
alpha_delete_by_source_local_ids: {
503+
Args: { p_space_name: string; p_source_local_ids: string[] }
504+
Returns: string
505+
}
506+
alpha_get_last_update_time: {
507+
Args: { p_space_name: string }
508+
Returns: {
509+
last_update_time: string
510+
}[]
511+
}
512+
alpha_upsert_discourse_nodes: {
513+
Args: {
514+
p_space_name: string
515+
p_user_email: string
516+
p_user_name: string
517+
p_nodes: Json
518+
}
519+
Returns: string
520+
}
470521
end_sync_task: {
471522
Args: {
472523
s_target: number
@@ -515,6 +566,26 @@ export type Database = {
515566
}
516567
Returns: unknown
517568
}
569+
upsert_discourse_nodes: {
570+
Args: {
571+
p_space_name: string
572+
p_user_email: string
573+
p_user_name: string
574+
p_nodes: Json
575+
p_platform_name?: string
576+
p_platform_url?: string
577+
p_space_url?: string
578+
p_agent_type?: string
579+
p_content_scale?: string
580+
p_embedding_model?: string
581+
p_document_source_id?: string
582+
}
583+
Returns: {
584+
content_id: number
585+
embedding_created: boolean
586+
action: string
587+
}[]
588+
}
518589
}
519590
Enums: {
520591
AgentIdentifierType: "email" | "orcid"

0 commit comments

Comments
 (0)