Skip to content

Add organization_id to review_suggestion_snippets #1525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
52 changes: 51 additions & 1 deletion frontend/packages/db/schema/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,23 @@ $$;
ALTER FUNCTION "public"."set_review_feedbacks_organization_id"() OWNER TO "postgres";


CREATE OR REPLACE FUNCTION "public"."set_review_suggestion_snippets_organization_id"() RETURNS "trigger"
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
BEGIN
NEW.organization_id := (
SELECT "organization_id"
FROM "public"."review_feedbacks"
WHERE "id" = NEW.review_feedback_id
);
RETURN NEW;
END;
$$;


ALTER FUNCTION "public"."set_review_suggestion_snippets_organization_id"() OWNER TO "postgres";


CREATE OR REPLACE FUNCTION "public"."set_schema_file_paths_organization_id"() RETURNS "trigger"
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
Expand Down Expand Up @@ -869,7 +886,8 @@ CREATE TABLE IF NOT EXISTS "public"."review_suggestion_snippets" (
"filename" "text" NOT NULL,
"snippet" "text" NOT NULL,
"created_at" timestamp(3) with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
"updated_at" timestamp(3) with time zone NOT NULL
"updated_at" timestamp(3) with time zone NOT NULL,
"organization_id" "uuid" NOT NULL
);


Expand Down Expand Up @@ -1147,6 +1165,10 @@ CREATE OR REPLACE TRIGGER "set_review_feedbacks_organization_id_trigger" BEFORE



CREATE OR REPLACE TRIGGER "set_review_suggestion_snippets_organization_id_trigger" BEFORE INSERT OR UPDATE ON "public"."review_suggestion_snippets" FOR EACH ROW EXECUTE FUNCTION "public"."set_review_suggestion_snippets_organization_id"();



CREATE OR REPLACE TRIGGER "set_schema_file_paths_organization_id_trigger" BEFORE INSERT OR UPDATE ON "public"."schema_file_paths" FOR EACH ROW EXECUTE FUNCTION "public"."set_schema_file_paths_organization_id"();


Expand Down Expand Up @@ -1341,6 +1363,11 @@ ALTER TABLE ONLY "public"."review_suggestion_snippets"



ALTER TABLE ONLY "public"."review_suggestion_snippets"
ADD CONSTRAINT "review_suggestion_snippets_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON UPDATE CASCADE ON DELETE RESTRICT;



ALTER TABLE ONLY "public"."schema_file_paths"
ADD CONSTRAINT "schema_file_path_project_id_fkey" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON UPDATE CASCADE ON DELETE RESTRICT;

Expand Down Expand Up @@ -1633,6 +1660,16 @@ COMMENT ON POLICY "authenticated_users_can_select_org_review_feedbacks" ON "publ



CREATE POLICY "authenticated_users_can_select_org_review_suggestion_snippets" ON "public"."review_suggestion_snippets" FOR SELECT TO "authenticated" USING (("organization_id" IN ( SELECT "organization_members"."organization_id"
FROM "public"."organization_members"
WHERE ("organization_members"."user_id" = "auth"."uid"()))));



COMMENT ON POLICY "authenticated_users_can_select_org_review_suggestion_snippets" ON "public"."review_suggestion_snippets" IS 'Authenticated users can only view review suggestion snippets belonging to organizations they are members of';



CREATE POLICY "authenticated_users_can_select_org_schema_file_paths" ON "public"."schema_file_paths" FOR SELECT TO "authenticated" USING (("organization_id" IN ( SELECT "organization_members"."organization_id"
FROM "public"."organization_members"
WHERE ("organization_members"."user_id" = "auth"."uid"()))));
Expand Down Expand Up @@ -1760,6 +1797,9 @@ ALTER TABLE "public"."review_feedback_knowledge_suggestion_mappings" ENABLE ROW
ALTER TABLE "public"."review_feedbacks" ENABLE ROW LEVEL SECURITY;


ALTER TABLE "public"."review_suggestion_snippets" ENABLE ROW LEVEL SECURITY;


ALTER TABLE "public"."schema_file_paths" ENABLE ROW LEVEL SECURITY;


Expand Down Expand Up @@ -1839,6 +1879,10 @@ CREATE POLICY "service_role_can_insert_all_review_feedbacks" ON "public"."review



CREATE POLICY "service_role_can_insert_all_review_suggestion_snippets" ON "public"."review_suggestion_snippets" FOR INSERT TO "service_role" WITH CHECK (true);



CREATE POLICY "service_role_can_select_all_doc_file_paths" ON "public"."doc_file_paths" FOR SELECT TO "service_role" USING (true);


Expand Down Expand Up @@ -2221,6 +2265,12 @@ GRANT ALL ON FUNCTION "public"."set_review_feedbacks_organization_id"() TO "serv



GRANT ALL ON FUNCTION "public"."set_review_suggestion_snippets_organization_id"() TO "anon";
GRANT ALL ON FUNCTION "public"."set_review_suggestion_snippets_organization_id"() TO "authenticated";
GRANT ALL ON FUNCTION "public"."set_review_suggestion_snippets_organization_id"() TO "service_role";



GRANT ALL ON FUNCTION "public"."set_schema_file_paths_organization_id"() TO "anon";
GRANT ALL ON FUNCTION "public"."set_schema_file_paths_organization_id"() TO "authenticated";
GRANT ALL ON FUNCTION "public"."set_schema_file_paths_organization_id"() TO "service_role";
Expand Down
2 changes: 2 additions & 0 deletions frontend/packages/db/src/types/supabase-overrides/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { OverallReviewsOverride } from './overall_reviews'
import type { ProjectRepositoryMappingsOverride } from './project_repository_mappings'
import type { ReviewFeedbackKnowledgeSuggestionMappingsOverride } from './review_feedback_knowledge_suggestion_mappings'
import type { ReviewFeedbacksOverride } from './review_feedbacks'
import type { ReviewSuggestionSnippetsOverride } from './review_suggestion_snippets'
import type { SchemaFilePathsOverride } from './schema_file_paths'

export type AppDatabaseOverrides = MergeDeep<
Expand All @@ -22,6 +23,7 @@ export type AppDatabaseOverrides = MergeDeep<
OverallReviewKnowledgeSuggestionMappingsOverride &
OverallReviewsOverride &
ReviewFeedbacksOverride &
ReviewSuggestionSnippetsOverride &
GithubPullRequestsOverride &
MigrationPullRequestMappingsOverride &
GithubPullRequestCommentsOverride &
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface ReviewSuggestionSnippetsOverride {
public: {
Tables: {
review_suggestion_snippets: {
Insert: {
organization_id?: string | null
}
Update: {
organization_id?: string | null
}
}
}
}
}
10 changes: 10 additions & 0 deletions frontend/packages/db/supabase/database.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ export type Database = {
created_at: string
filename: string
id: string
organization_id: string
review_feedback_id: string
snippet: string
updated_at: string
Expand All @@ -848,6 +849,7 @@ export type Database = {
created_at?: string
filename: string
id?: string
organization_id: string
review_feedback_id: string
snippet: string
updated_at: string
Expand All @@ -856,6 +858,7 @@ export type Database = {
created_at?: string
filename?: string
id?: string
organization_id?: string
review_feedback_id?: string
snippet?: string
updated_at?: string
Expand All @@ -868,6 +871,13 @@ export type Database = {
referencedRelation: 'review_feedbacks'
referencedColumns: ['id']
},
{
foreignKeyName: 'review_suggestion_snippets_organization_id_fkey'
columns: ['organization_id']
isOneToOne: false
referencedRelation: 'organizations'
referencedColumns: ['id']
},
]
}
schema_file_paths: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
BEGIN;

ALTER TABLE "public"."review_suggestion_snippets" ADD COLUMN "organization_id" UUID;

UPDATE "public"."review_suggestion_snippets" rss
SET "organization_id" = (
SELECT rf."organization_id"
FROM "public"."review_feedbacks" rf
WHERE rf."id" = rss."review_feedback_id"
LIMIT 1
);

ALTER TABLE "public"."review_suggestion_snippets"
ALTER COLUMN "organization_id" SET NOT NULL;

ALTER TABLE "public"."review_suggestion_snippets"
ADD CONSTRAINT "review_suggestion_snippets_organization_id_fkey"
FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id")
ON UPDATE CASCADE ON DELETE RESTRICT;

CREATE OR REPLACE FUNCTION "public"."set_review_suggestion_snippets_organization_id"() RETURNS "trigger"
LANGUAGE "plpgsql" SECURITY DEFINER
AS $$
BEGIN
NEW.organization_id := (
SELECT "organization_id"
FROM "public"."review_feedbacks"
WHERE "id" = NEW.review_feedback_id
);
RETURN NEW;
END;
$$;

CREATE TRIGGER "set_review_suggestion_snippets_organization_id_trigger"
BEFORE INSERT OR UPDATE ON "public"."review_suggestion_snippets"
FOR EACH ROW
EXECUTE FUNCTION "public"."set_review_suggestion_snippets_organization_id"();

ALTER TABLE "public"."review_suggestion_snippets" ENABLE ROW LEVEL SECURITY;

CREATE POLICY "authenticated_users_can_select_org_review_suggestion_snippets"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Authenticated users can only view review suggestion snippets that belong to organizations they are members of.
  • Service role can insert review suggestion snippets without restriction.
    If you're curious where this table is used, try grepping for .from('review_suggestion_snippets') in the codebase.

ON "public"."review_suggestion_snippets"
FOR SELECT TO "authenticated"
USING (("organization_id" IN (
SELECT "organization_members"."organization_id"
FROM "public"."organization_members"
WHERE ("organization_members"."user_id" = "auth"."uid"())
)));

COMMENT ON POLICY "authenticated_users_can_select_org_review_suggestion_snippets"
ON "public"."review_suggestion_snippets"
IS 'Authenticated users can only view review suggestion snippets belonging to organizations they are members of';

CREATE POLICY "service_role_can_insert_all_review_suggestion_snippets"
ON "public"."review_suggestion_snippets"
FOR INSERT TO "service_role"
WITH CHECK (true);

COMMIT;
Loading