Skip to content

Conditionally bulk_insert Messages#205

Closed
an7oine wants to merge 1 commit intovintasoftware:mainfrom
an7oine:main
Closed

Conditionally bulk_insert Messages#205
an7oine wants to merge 1 commit intovintasoftware:mainfrom
an7oine:main

Conversation

@an7oine
Copy link
Copy Markdown

@an7oine an7oine commented Nov 19, 2025

Compatibility with e.g. Mysql, where bulk insert does not return generated primary keys.

Summary by Sourcery

Support databases without bulk insert row-returning capability by falling back to individual saves for message creation.

Bug Fixes:

  • Ensure compatibility with DBs (e.g. MySQL) that don’t return generated primary keys from bulk insert.

Enhancements:

  • Conditionally use bulk_create when the DB can return inserted rows and otherwise save messages individually to assign IDs correctly.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Nov 19, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Introduce conditional bulk insertion in save_django_messages by checking the DB’s can_return_rows_from_bulk_insert feature, falling back to per-instance saves when primary keys cannot be returned.

Sequence diagram for conditional bulk insert in save_django_messages

sequenceDiagram
    participant save_django_messages
    participant DB_Feature_Check
    participant DjangoMessage
    participant Database
    save_django_messages->>DB_Feature_Check: Check can_return_rows_from_bulk_insert
    alt Bulk insert supported
        save_django_messages->>DjangoMessage: bulk_create(messages)
        DjangoMessage->>Database: Bulk insert
        Database-->>DjangoMessage: Return created messages with IDs
    else Bulk insert not supported
        save_django_messages->>DjangoMessage: Create message instances
        loop For each message
            DjangoMessage->>Database: save()
            Database-->>DjangoMessage: Return created message with ID
        end
    end
Loading

Class diagram for updated save_django_messages logic

classDiagram
    class DjangoMessage {
        +thread
        +message
        +id
        +save()
    }
    class save_django_messages {
        +messages: list[BaseMessage]
        +thread: Thread
        +return: list[DjangoMessage]
    }
    save_django_messages --> DjangoMessage : creates
Loading

File-Level Changes

Change Details Files
Check DB feature before using bulk_create
  • Add connections[db].features.can_return_rows_from_bulk_insert check
  • Wrap existing bulk_create call in an if branch
django_ai_assistant/helpers/django_messages.py
Fallback to saving each message to obtain PKs
  • Build created_messages list with comprehension
  • Iterate over list and call save() on each message instance
django_ai_assistant/helpers/django_messages.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Member

@fjsj fjsj left a comment

Choose a reason for hiding this comment

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

Thanks, this looks great. Could you please add a unit test? You can mock .features.can_return_rows_from_bulk_insert if necessary, Let me know if you need assistance.

@an7oine
Copy link
Copy Markdown
Author

an7oine commented Nov 27, 2025

The tests already fail like this (repeated 21 times):

FAILED tests/test_conf.py::test_call_fn - TypeError: NinjaAPI.__init__() got an unexpected keyword argument 'csrf'

Where would the new test go?

@fjsj
Copy link
Copy Markdown
Member

fjsj commented Nov 27, 2025

You can make a new test file under tests/test_helpers/test_django_messages.py
Add a test that verify if save_django_messages works both when can_return_rows_from_bulk_insert is true and false.

Regarding the failing tests, we'll verify.

@codecov
Copy link
Copy Markdown

codecov Bot commented Nov 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (34d1b6b) to head (d2b7184).
⚠️ Report is 16 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main      #205      +/-   ##
===========================================
+ Coverage   93.82%   100.00%   +6.17%     
===========================================
  Files          19         5      -14     
  Lines         696       113     -583     
  Branches       47         6      -41     
===========================================
- Hits          653       113     -540     
+ Misses         33         0      -33     
+ Partials       10         0      -10     
Flag Coverage Δ
backend-python-3.10 ?
backend-python-3.11 ?
backend-python-3.12 ?
backend-python-3.13 ?
frontend 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rvlb rvlb self-requested a review January 23, 2026 13:23
@rvlb
Copy link
Copy Markdown
Contributor

rvlb commented Jan 23, 2026

I've moved over this change to this PR #211 and added the tests.

The original commit has been preserved there https://github.com/vintasoftware/django-ai-assistant/pull/211/commits.

Closing this one.

@rvlb rvlb closed this Jan 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants