Fix upsert ON CONFLICT: missing :identifier expr + numbered parameters#95
Conversation
Two bugs in SQL generation that cause "near ?: syntax error" on any
INSERT ... ON CONFLICT DO UPDATE query:
1. Missing :identifier expression handler. Ecto generates ON CONFLICT
UPDATE clauses using fragment expressions containing {:identifier, _,
["column_name"]} tuples. Without a matching expr/3 clause, these fall
through to the catch-all which returns bare "?", producing invalid SQL
like: SET "col" = EXCLUDED.?
2. Bare ? parameter placeholders. SQLite requires numbered positional
parameters (?1, ?2, ...) when a statement contains multiple parameter
groups (INSERT values + ON CONFLICT UPDATE). The ecto_sqlite3 adapter
already uses this pattern.
Fixes all upsert operations including Ash Framework's upsert? actions.
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
WalkthroughThe LibSQL adapter now generates numbered SQLite positional bind parameters ( ChangesLibSQL Insert Parameter Binding
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. 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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@lib/ecto/adapters/libsql/connection.ex`:
- Around line 1184-1189: The IN with a bound list branch still emits bare "?"
placeholders; update the clause handling the pattern {:in, _, [left, {:^, _, [_,
length]}]} inside expr/3 to emit numbered placeholders like the generic {:^, [],
[ix]} clause does: generate a sequence of "?" + Integer.to_string(start_index)
through "?" + Integer.to_string(start_index + length - 1) (matching the same
numbering scheme used by expr({:^,[],[ix]},...)), join them with ", " and return
the full parenthesized placeholder list so SQLite gets ?1, ?2, ... for IN ^list.
Ensure you reference and reuse the same index base/offset logic used by
expr({:^,...}) so numbering remains consistent across other parameter groups.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 7f519f1b-e1b3-479f-8e81-0e161b5058ae
📒 Files selected for processing (1)
lib/ecto/adapters/libsql/connection.ex
|
Thanks @AlanMcCann ! Sorry I didn't see this right away, and was already in the process of making some updates and fixes, so I've fixed this issue in my branch. Should be another release out soon. |
|
@ocean Thanks! |
- Update all 8 test assertions from VALUES (?, ?) to VALUES (?1, ?2, ...)
to match the numbered parameter format introduced in this PR.
- Fix the IN with bound list handler to also use numbered parameters
(?start+1, ..., ?start+length) consistent with the rest of the adapter,
resolving the gap flagged by CodeRabbit.
- Guard the empty-list case (length=0) explicitly, using
"IN (SELECT NULL WHERE 1=0)" since SQLite rejects IN ().
- Add connection-level tests for numbered IN parameter generation,
including the non-zero start index case.
- Add four integration tests exercising Repo.insert with on_conflict:
:replace_all, :nothing, {:replace, fields}, and insert-without-conflict,
proving the upsert fix works end-to-end against a real database.
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
test/ecto_connection_test.exs (1)
582-635: ⚡ Quick winAdd a direct empty-
INlist assertion to lock the guard branch.You now test numbered expansion well; adding a
{:^, [], [0, 0]}case would pin theIN (SELECT NULL WHERE 1=0)behaviour and prevent regressions on that edge path.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/ecto_connection_test.exs` around lines 582 - 635, Add a new test in the same "IN clause parameter numbering" describe block that constructs a query like the others but with the bound IN list node using {:^, [], [0, 0]} (start_ix=0, length=0), call Connection.all(query) |> IO.iodata_to_binary() and assert the produced SQL contains the empty-IN sentinel clause (e.g. assert sql =~ ~s["id" IN (SELECT NULL WHERE 1=0)]), thereby locking the guard branch that handles zero-length IN lists.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/ecto_integration_test.exs`:
- Around line 943-959: The test "Repo.insert with on_conflict: :replace_all
upserts an existing record" currently only asserts email and row count, so fetch
the persisted record after the upsert and assert the updated field was actually
stored: after calling TestRepo.insert (the updated binding), query the DB for
the User by email (e.g., TestRepo.get_by/User.get_by equivalent) and assert that
the name equals "Alice Updated" to ensure the on_conflict :replace_all truly
persisted the change.
---
Nitpick comments:
In `@test/ecto_connection_test.exs`:
- Around line 582-635: Add a new test in the same "IN clause parameter
numbering" describe block that constructs a query like the others but with the
bound IN list node using {:^, [], [0, 0]} (start_ix=0, length=0), call
Connection.all(query) |> IO.iodata_to_binary() and assert the produced SQL
contains the empty-IN sentinel clause (e.g. assert sql =~ ~s["id" IN (SELECT
NULL WHERE 1=0)]), thereby locking the guard branch that handles zero-length IN
lists.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 962d481e-630e-45b4-a43a-99b2c491c170
📒 Files selected for processing (3)
lib/ecto/adapters/libsql/connection.extest/ecto_connection_test.exstest/ecto_integration_test.exs
🚧 Files skipped from review as they are similar to previous changes (1)
- lib/ecto/adapters/libsql/connection.ex
Two bugs in SQL generation that cause "near ?: syntax error" on any INSERT ... ON CONFLICT DO UPDATE query:
Missing :identifier expression handler. Ecto generates ON CONFLICT UPDATE clauses using fragment expressions containing {:identifier, _, ["column_name"]} tuples. Without a matching expr/3 clause, these fall through to the catch-all which returns bare "?", producing invalid SQL like: SET "col" = EXCLUDED.?
Bare ? parameter placeholders. SQLite requires numbered positional parameters (?1, ?2, ...) when a statement contains multiple parameter groups (INSERT values + ON CONFLICT UPDATE). The ecto_sqlite3 adapter already uses this pattern.
Fixes all upsert operations including Ash Framework's upsert? actions.
Summary by CodeRabbit