Skip to content

Conversation

@maparent
Copy link
Collaborator

@maparent maparent commented Jun 16, 2025

Summary by CodeRabbit

  • New Features

    • Added support for batch upserting multiple concepts using a new function, allowing efficient import or update of concept data from JSON input.
    • Introduced enhanced handling of local identifiers and references, enabling automatic resolution of related entities during concept creation or update.
  • Documentation

    • Expanded type definitions to reflect new functions and input types for improved clarity and type safety in database interactions.

@linear
Copy link

linear bot commented Jun 16, 2025

@vercel
Copy link

vercel bot commented Jun 16, 2025

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

Name Status Preview Comments Updated (UTC)
discourse-graph ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 19, 2025 1:31am

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 16, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

This update introduces a new composite type for representing concept data with local references, and adds two PostgreSQL functions: one to convert local concept inputs to database records by resolving local identifiers, and another to upsert multiple concepts from JSON input, handling conflicts and returning affected IDs. Type definitions are updated accordingly.

Changes

File(s) Change Summary
.../migrations/20250611180757_concept_upsert_functions.sql Adds composite type concept_local_input and functions local_concept_to_db_concept, upsert_concepts
.../schemas/concept.sql Declares new composite type and functions for concept conversion and upsert
.../types.gen.ts Updates type definitions to include new composite type and functions

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant DB

    Client->>DB: Call upsert_concepts(space_id, concepts_json)
    loop For each concept in concepts_json
        DB->>DB: Convert JSON to concept_local_input
        DB->>DB: local_concept_to_db_concept(input)
        DB->>DB: Resolve local IDs to DB IDs
        DB->>DB: Prepare Concept record
        DB->>DB: INSERT INTO Concept (on conflict update)
        alt Unique violation (other constraint)
            DB->>DB: Log warning, return -1
        end
    end
    DB-->>Client: Return array of inserted/updated concept IDs
Loading

Poem

In the warren of data, new tunnels appear,
Concepts hop in, both far and near.
Local IDs find their way,
Mapped to the fields where real records stay.
With upserts and types, the garden expands—
More carrots for all, in our database lands!
🥕


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@maparent
Copy link
Collaborator Author

@CodeRabbit review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 16, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (2)
packages/database/supabase/migrations/20250611180757_concept_upsert_functions.sql (2)

1-21: Same arity omission as in schema file

The composite type declared here duplicates the one in concept.sql and carries the same missing arity field. Keep both definitions identical to prevent migration drift.


94-97: Duplicate default JSON with unknown arity key

Mirrors the comment in the schema file – remove or add the column for consistency.

🧹 Nitpick comments (2)
packages/database/supabase/schemas/concept.sql (2)

94-97: Default-record JSON contains unknown keys

Same issue from the caller side: the literal used to seed defaults includes "arity", which currently has no effect. If you elect not to add arity to the composite, drop the key here to avoid dead code paths.

- local_concept := jsonb_populate_record(NULL::public.concept_local_input, '{"epistemic_status": "unknown", "arity":0, "literal_content":{},"reference_content":{},"is_schema":false}');
+ local_concept := jsonb_populate_record(
+     NULL::public.concept_local_input,
+     '{"epistemic_status":"unknown","literal_content":{},"reference_content":{},"is_schema":false}'
+ );

85-90: v_platform is selected but never used

The SELECT platform INTO STRICT v_platform guard succeeds (or raises) but the variable is not referenced afterwards.
Unless you plan to validate platform-specific constraints later, remove the variable and the SELECT to reduce noise and pointless work.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4eacf3a and 4a8b7ff.

📒 Files selected for processing (3)
  • packages/database/supabase/migrations/20250611180757_concept_upsert_functions.sql (1 hunks)
  • packages/database/supabase/schemas/concept.sql (1 hunks)
  • packages/database/types.gen.ts (3 hunks)
🔇 Additional comments (3)
packages/database/supabase/schemas/concept.sql (2)

152-173: Overwrites existing reference_content unconditionally

If the incoming JSON already contained a valid reference_content, providing local_reference_content will replace it entirely rather than merge.
Confirm this is intentional; otherwise consider jsonb_concat() or keyed merges to avoid data loss.


101-121: ```shell
#!/bin/bash
set -e

Show the definition of concept_local_input

echo "---- concept_local_input definition ----"
rg -n "CREATE TYPE public\.concept_local_input" -A10 packages/database/supabase/schemas/concept.sql

Show usage of 'arity' in the file

echo -e "\n---- occurrences of 'arity' ----"
rg -n "arity" packages/database/supabase/schemas/concept.sql

Show the upsert_concepts function context

echo -e "\n---- upsert_concepts function ----"
rg -n "upsert_concepts" -A10 packages/database/supabase/schemas/concept.sql


</details>
<details>
<summary>packages/database/types.gen.ts (1)</summary>

`672-691`: **Generated type will need regeneration if `arity` is added**

Once you update the composite definition, remember to regenerate `types.gen.ts`; otherwise TypeScript will be out of sync with the database schema.

</details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

@maparent
Copy link
Collaborator Author

@CodeRabbit: arity will be handled separately. But removing it from the constants right now.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 16, 2025

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for arity-related constants in the codebase
echo "=== Searching for arity constants ==="
rg -i "arity.*const|const.*arity" -A 2 -B 2

echo -e "\n=== Searching for arity in constant files ==="
rg -i "arity" --type-add 'constants:*.{const,constants}*' -t constants -A 2 -B 2

echo -e "\n=== Searching for arity in all files ==="
rg "arity" -A 1 -B 1 --max-count 10

echo -e "\n=== Looking for files with 'constant' in name containing arity ==="
fd -i constant | head -10 | xargs -I {} rg -l "arity" {} 2>/dev/null || echo "No matches found"

Length of output: 41435


@maparent Got it! I can see you're referring to the hardcoded "arity":0 in the default values within the upsert_concepts function. That makes sense to handle arity calculation separately rather than having it as a static constant in the JSON defaults.

The current line in the migration:

local_concept := jsonb_populate_record(NULL::public.concept_local_input, '{"epistemic_status": "unknown", "arity":0, "literal_content":{},"reference_content":{},"is_schema":false}');

Removing the arity from those defaults will allow for more flexible arity handling based on your specific business logic. This approach gives you better control over how arity is determined for each concept rather than always defaulting to 0.

@maparent
Copy link
Collaborator Author

@CodeRabbit: Actually will remove arity from constants in the other pr.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 16, 2025

Tip

For best results, initiate chat on the files or code changes.

@maparent Understood! That makes perfect sense to handle the arity constant removal in a separate PR. It's good practice to keep changes focused - this PR handles the new concept upsert functionality, and the arity constant cleanup can be addressed independently in its own PR. Thanks for the clarification!

Copy link
Contributor

@mdroidian mdroidian left a comment

Choose a reason for hiding this comment

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

Overall looks good, but we could use more documentation for the caller on things like "What does this do", "What are the required inputs", "What are some conflicts that might arise and what to do about them", "What return value(s) should I look for and what should I do about them", etc

@maparent maparent merged commit 9132c1f into main Jun 19, 2025
3 checks passed
@github-project-automation github-project-automation bot moved this to Done in General Jun 19, 2025
@maparent maparent deleted the ENG-446-bulk-db-concept-upsert branch June 19, 2025 01:35
maparent added a commit that referenced this pull request Jun 19, 2025
There were a few discrepancies between schemas and migrations introduced in #215 and #196. 
This ensures that the `supabase db diff` confirms equivalence.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants