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

Conversation

devin-ai-integration[bot]
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot commented Apr 25, 2025

Issue

  • resolve: Add organization_id to review_suggestion_snippets table
スクリーンショット_2025-04-28_18_01_34

Why is this change needed?

This PR adds the organization_id column to the review_suggestion_snippets table and implements RLS policies to ensure data is properly isolated by organization. This is part of the ongoing effort to add organization-based row-level security to all tables.

What would you like reviewers to focus on?

  • Correctness of the migration file
  • Proper implementation of RLS policies
  • Type override implementation

Testing Verification

Changes were verified by reviewing the migration file and type overrides against similar implementations in other tables.

Note: Type generation failed due to Docker rate limits, but type overrides were created following the established pattern.

What was done

🤖 Generated by PR Agent at 197a638

  • Add organization_id column to review_suggestion_snippets table
    • Populate existing rows with correct organization ID
    • Enforce NOT NULL and foreign key constraints
  • Implement trigger to auto-set organization_id from related feedback
  • Enable row-level security (RLS) and add organization-based policies
  • Update TypeScript types and overrides for new column

Detailed Changes

Relevant files
Enhancement
schema.sql
Add organization_id, triggers, and RLS to review_suggestion_snippets

frontend/packages/db/schema/schema.sql

  • Add organization_id column to review_suggestion_snippets
  • Create trigger and function to auto-set organization ID
  • Add RLS policies and enable RLS for the table
  • Add foreign key constraint and update grants
  • +51/-1   
    database.types.ts
    Update types for review_suggestion_snippets organization_id

    frontend/packages/db/supabase/database.types.ts

  • Add organization_id to Row, Insert, Update types for
    review_suggestion_snippets
  • Add new foreign key relationship for organization_id
  • +10/-0   
    20250426000000_add_organization_id_to_review_suggestion_snippets.sql
    Migration for organization_id and RLS on review_suggestion_snippets

    frontend/packages/db/supabase/migrations/20250426000000_add_organization_id_to_review_suggestion_snippets.sql

  • Migration to add organization_id column and populate data
  • Add NOT NULL, foreign key, trigger, and RLS policies
  • Add organization-based select and insert policies
  • +59/-0   
    index.ts
    Add review_suggestion_snippets override to database types

    frontend/packages/db/src/types/supabase-overrides/index.ts

    • Add ReviewSuggestionSnippetsOverride to AppDatabaseOverrides type
    +2/-0     
    review_suggestion_snippets.ts
    Add type override for review_suggestion_snippets organization_id

    frontend/packages/db/src/types/supabase-overrides/review_suggestion_snippets.ts

    • Add type override for organization_id in Insert and Update
    +14/-0   

    Additional Notes


    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Co-Authored-By: noritaka.ikeda@route06.co.jp <noritaka.ikeda@route06.co.jp>
    Copy link

    changeset-bot bot commented Apr 25, 2025

    ⚠️ No Changeset found

    Latest commit: c511a74

    Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

    This PR includes no changesets

    When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

    Click here to learn what changesets are, and how to add one.

    Click here if you're a maintainer who wants to add a changeset to this PR

    Copy link

    vercel bot commented Apr 25, 2025

    The latest updates on your projects. Learn more about Vercel for Git ↗︎

    Name Status Preview Comments Updated (UTC)
    liam-app ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 1, 2025 11:06am
    liam-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 1, 2025 11:06am
    liam-erd-sample ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 1, 2025 11:06am

    Copy link
    Contributor Author

    🤖 Devin AI Engineer

    I'll be helping with this pull request! Here's what you should know:

    ✅ I will automatically:

    • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
    • Look at CI failures and help fix them

    Note: I can only respond to comments from users who have write access to this repository.

    ⚙️ Control Options:

    • Disable automatic comment and CI monitoring

    Copy link
    Contributor

    CI Feedback 🧐

    A test triggered by this PR failed. Here is an AI-generated analysis of the failure:

    Action: frontend-ci

    Failed stage: Run pnpm --filter @liam-hq/db supabase:start [❌]

    Failure summary:

    The action failed due to a SQL syntax error in a database migration script. Specifically, in the
    migration file "20250425122828_add_organization_id_to_overall_reviews.sql", there is a syntax error
    at line 587 where "or" is incorrectly used in an UPDATE statement:

    UPDATE
    "public"."overall_reviews" or

    The SQL parser encountered this error (SQLSTATE 42601) when trying to
    apply the migration during the pnpm supabase:start command.

    Relevant error logs:
    1:  ##[group]Operating System
    2:  Ubuntu
    ...
    
    158:  ##[group]Run pnpm install --frozen-lockfile --prefer-offline
    159:  �[36;1mpnpm install --frozen-lockfile --prefer-offline�[0m
    160:  shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
    161:  env:
    162:  PNPM_HOME: /home/runner/setup-pnpm/node_modules/.bin
    163:  ##[endgroup]
    164:  Scope: all 17 workspace projects
    165:  Lockfile is up to date, resolution step is skipped
    166:  Progress: resolved 1, reused 0, downloaded 0, added 0
    167:  Packages: +1566
    168:  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    169:  Progress: resolved 1566, reused 1240, downloaded 0, added 0
    170:  Progress: resolved 1566, reused 1561, downloaded 0, added 539
    171:  Progress: resolved 1566, reused 1561, downloaded 0, added 1415
    172:  Progress: resolved 1566, reused 1561, downloaded 0, added 1566, done
    173:  WARN  Failed to create bin at /home/runner/work/liam/liam/frontend/apps/erd-sample/node_modules/.bin/liam. ENOENT: no such file or directory, open '/home/runner/work/liam/liam/frontend/packages/cli/dist-cli/bin/cli.js'
    174:  devDependencies:
    ...
    
    187:  │                                                                              │
    188:  │   Ignored build scripts: @biomejs/biome, @bundled-es-modules/glob,           │
    189:  │   @depot/cli, @prisma/client, @prisma/engines, @sentry/cli, core-js-pure,    │
    190:  │   esbuild, protobufjs, sharp, style-dictionary.                              │
    191:  │   Run "pnpm approve-builds" to pick which dependencies should be allowed     │
    192:  │   to run scripts.                                                            │
    193:  │                                                                              │
    194:  ╰──────────────────────────────────────────────────────────────────────────────╯
    195:  frontend/apps/docs postinstall$ fumadocs-mdx
    196:  frontend/packages/jobs postinstall$ cp ../db-structure/node_modules/@ruby/prism/src/prism.wasm prism.wasm
    197:  frontend/packages/jobs postinstall: Done
    198:  frontend/apps/docs postinstall: [MDX] types generated
    199:  frontend/apps/docs postinstall: Done
    200:  frontend/apps/app postinstall$ cp ../../packages/db-structure/node_modules/@ruby/prism/src/prism.wasm prism.wasm
    201:  frontend/apps/app postinstall: Done
    202:  WARN  Failed to create bin at /home/runner/work/liam/liam/frontend/apps/erd-sample/node_modules/.bin/liam. ENOENT: no such file or directory, open '/home/runner/work/liam/liam/frontend/apps/erd-sample/node_modules/@liam-hq/cli/dist-cli/bin/cli.js'
    203:  Done in 5.7s using pnpm v10.8.1
    ...
    
    570:  Applying migration 20250423123350_refine_invite_organization_member.sql...
    571:  Applying migration 20250423124731_rename_github_doc_file_paths.sql...
    572:  Applying migration 20250424102300_add_organization_id_to_overall_review_knowledge_suggestion_mappings.sql...
    573:  NOTICE (42622): identifier "overall_review_knowledge_suggestion_mappings_organization_id_fkey" will be truncated to "overall_review_knowledge_suggestion_mappings_organization_id_fk"
    574:  NOTICE (42622): identifier "set_overall_review_knowledge_suggestion_mappings_organization_id" will be truncated to "set_overall_review_knowledge_suggestion_mappings_organization_i"
    575:  NOTICE (42622): identifier "set_overall_review_knowledge_suggestion_mappings_organization_id_trigger" will be truncated to "set_overall_review_knowledge_suggestion_mappings_organization_i"
    576:  NOTICE (42622): identifier "set_overall_review_knowledge_suggestion_mappings_organization_id" will be truncated to "set_overall_review_knowledge_suggestion_mappings_organization_i"
    577:  NOTICE (42622): identifier "authenticated_users_can_select_org_overall_review_knowledge_suggestion_mappings" will be truncated to "authenticated_users_can_select_org_overall_review_knowledge_sug"
    578:  NOTICE (42622): identifier "authenticated_users_can_select_org_overall_review_knowledge_suggestion_mappings" will be truncated to "authenticated_users_can_select_org_overall_review_knowledge_sug"
    579:  NOTICE (42622): identifier "service_role_can_insert_all_overall_review_knowledge_suggestion_mappings" will be truncated to "service_role_can_insert_all_overall_review_knowledge_suggestion"
    580:  Applying migration 20250424122906_update_overall_reviews_table.sql...
    581:  Applying migration 20250424163100_get_invitation_data.sql...
    582:  Applying migration 20250424163200_accept_invitation.sql...
    583:  Applying migration 20250425122828_add_organization_id_to_overall_reviews.sql...
    584:  Stopping containers...
    585:  ERROR: syntax error at or near "or" (SQLSTATE 42601)
    586:  At statement 2:                                     
    587:  UPDATE "public"."overall_reviews" or                
    588:  ^                 
    589:  Try rerunning the command with --debug to troubleshoot the error.
    590:  /home/runner/work/liam/liam/frontend/packages/db:
    591:  ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL  @liam-hq/db@0.0.0 supabase:start: `pnpm supabase start`
    592:  Exit status 1
    593:  ##[error]Process completed with exit code 1.
    594:  Post job cleanup.
    

    @NoritakaIkeda NoritakaIkeda self-assigned this Apr 25, 2025
    … related triggers, policies, and database types
    Copy link

    liam-migration-preview bot commented Apr 28, 2025

    This migration adds an organization_id column to review_suggestion_snippets along with a new trigger and RLS policies. The key issues include lack of null-check validation in the trigger and missing index on organization_id, with a potential mismatch between the TS types and database constraints. Overall, the migration follows project conventions and improves security isolation, but clarifying the TS type expectation is important.

    Migration URL: https://liam-app-git-staging-route-06-core.vercel.app/app/projects/9d777f64-400a-42f3-a60e-98a59fc97279/ref/devin%2F1745585764-add-organization-id-review-suggestion-snippets/migrations/f844ab9a-2bc4-4494-90af-2374fc674f15

    ER Diagram:


    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.

    @NoritakaIkeda NoritakaIkeda marked this pull request as ready for review April 28, 2025 09:03
    @NoritakaIkeda NoritakaIkeda requested a review from a team as a code owner April 28, 2025 09:03
    @NoritakaIkeda NoritakaIkeda requested review from hoshinotsuyoshi, FunamaYukina, junkisai, MH4GF and NoritakaIkeda and removed request for a team April 28, 2025 09:03
    Copy link
    Contributor

    qodo-merge-for-open-source bot commented Apr 28, 2025

    PR Reviewer Guide 🔍

    (Review updated until commit c511a74)

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Nullable Type

    The organization_id is defined as nullable in the TypeScript override, but the database schema enforces it as NOT NULL. This inconsistency could lead to runtime errors.

      organization_id?: string | null
    }
    Missing Index

    The migration adds organization_id column which will be used in RLS policies for filtering, but doesn't create an index on this column which could impact query performance.

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

    Copy link
    Contributor

    qodo-merge-for-open-source bot commented Apr 28, 2025

    PR Code Suggestions ✨

    Latest suggestions up to c511a74
    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Fix type inconsistency

    The override definition doesn't match the database schema. In the database
    schema, organization_id is defined as NOT NULL for inserts, but the override
    makes it optional. This mismatch could lead to runtime errors when inserting
    records without an organization_id.

    frontend/packages/db/src/types/supabase-overrides/review_suggestion_snippets.ts [1-14]

     export interface ReviewSuggestionSnippetsOverride {
       public: {
         Tables: {
           review_suggestion_snippets: {
             Insert: {
    -          organization_id?: string | null
    +          organization_id: string
             }
             Update: {
               organization_id?: string | null
             }
           }
         }
       }
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    __

    Why: The suggestion correctly identifies a type mismatch between the Insert type in the override file (organization_id?: string | null) and the generated database.types.ts (organization_id: string). Aligning these types improves consistency, although the database trigger handles setting the organization_id during insertion.

    Low
    Learned
    best practice
    Validate data before updating

    The query doesn't validate that the subquery returns a valid organization_id. If
    a review_feedback_id doesn't have a matching record, NULL could be assigned. Add
    a validation check to ensure all records can be properly updated.

    frontend/packages/db/supabase/migrations/20250426000000_add_organization_id_to_review_suggestion_snippets.sql [5-11]

    +-- First verify all records have valid organization_ids
    +DO $$
    +BEGIN
    +  IF EXISTS (
    +    SELECT 1 FROM "public"."review_suggestion_snippets" rss
    +    LEFT JOIN "public"."review_feedbacks" rf ON rf."id" = rss."review_feedback_id"
    +    WHERE rf."organization_id" IS NULL
    +  ) THEN
    +    RAISE EXCEPTION 'Some review_suggestion_snippets have invalid review_feedback_id references';
    +  END IF;
    +END $$;
    +
     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
     );
    • Apply this suggestion
    Suggestion importance[1-10]: 6
    Low
    • More

    Previous suggestions

    Suggestions up to commit 197a638
    CategorySuggestion                                                                                                                                    Impact
    Learned
    best practice
    Add data validation check

    The trigger function doesn't validate that the query returns a result before
    assigning it. If a review_feedback_id doesn't exist, this could lead to NULL
    values. Add a check to verify the query returns a valid organization_id.

    frontend/packages/db/supabase/migrations/20250426000000_add_organization_id_to_review_suggestion_snippets.sql [25-29]

     NEW.organization_id := (
       SELECT "organization_id" 
       FROM "public"."review_feedbacks" 
       WHERE "id" = NEW.review_feedback_id
     );
     
    +IF NEW.organization_id IS NULL THEN
    +  RAISE EXCEPTION 'Cannot find organization_id for review_feedback_id %', NEW.review_feedback_id;
    +END IF;
    +
    Suggestion importance[1-10]: 6
    Low

    …into devin/1745585764-add-organization-id-review-suggestion-snippets
    Copy link
    Member

    @MH4GF MH4GF left a comment

    Choose a reason for hiding this comment

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

    👍🏻

    Copy link
    Member

    @FunamaYukina FunamaYukina left a comment

    Choose a reason for hiding this comment

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

    LGTM!!!
    Thank you so much!🌟

    Base automatically changed from devin/1745585143-add-organization-id-review-feedbacks to main May 1, 2025 10:21
    Copy link

    supabase bot commented May 1, 2025

    Updates to Preview Branch (devin/1745585764-add-organization-id-review-suggestion-snippets) ↗︎

    Deployments Status Updated
    Database Thu, 01 May 2025 11:00:38 UTC
    Services Thu, 01 May 2025 11:00:38 UTC
    APIs Thu, 01 May 2025 11:00:38 UTC

    Tasks are run on every commit but only new migration files are pushed.
    Close and reopen this PR if you want to apply changes from existing seed or migration files.

    Tasks Status Updated
    Configurations Thu, 01 May 2025 11:00:51 UTC
    Migrations Thu, 01 May 2025 11:00:53 UTC
    Seeding Thu, 01 May 2025 11:00:53 UTC
    Edge Functions Thu, 01 May 2025 11:00:53 UTC

    View logs for this Workflow Run ↗︎.
    Learn more about Supabase for Git ↗︎.

    @NoritakaIkeda NoritakaIkeda added this pull request to the merge queue May 1, 2025
    Merged via the queue into main with commit da4a54f May 1, 2025
    26 of 29 checks passed
    @NoritakaIkeda NoritakaIkeda deleted the devin/1745585764-add-organization-id-review-suggestion-snippets branch May 1, 2025 11:16
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    3 participants