Skip to content

Conversation

@ricardo-valero
Copy link

@ricardo-valero ricardo-valero commented Jan 29, 2026

Summary

  • Fix Repo.exists? generating invalid SQL with empty SELECT clause
  • Add tests for Repo.exists?/2 functionality

Problem

Repo.exists? was generating invalid SQL:

SELECT  FROM "users" AS s0 WHERE (s0."age" > 25) LIMIT 1

Instead of:

SELECT 1 FROM "users" AS s0 WHERE (s0."age" > 25) LIMIT 1

This caused: SQLite failure: near "FROM": syntax error

Solution

Added a clause to handle empty field lists in select_fields/3:

defp select_fields(%{fields: []}, _sources, _query), do: "1"

This matches the behaviour of the ecto_sqlite3 adapter.

Testing

  • Added comprehensive tests in test/exists_query_test.exs
  • All existing tests still pass

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of empty field selections in SELECT queries to ensure valid output.
  • Tests

    • Added comprehensive test coverage for exists? functionality, including edge cases with empty selections and complex query scenarios.

✏️ Tip: You can customize this high-level summary in your review settings.

Repo.exists? was generating invalid SQL with an empty SELECT clause:
`SELECT  FROM "users"` instead of `SELECT 1 FROM "users"`

This caused a syntax error: "near FROM: syntax error"

The fix adds a clause to handle empty field lists in select_fields/3,
matching the behaviour of ecto_sqlite3 adapter.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 29, 2026

Walkthrough

The changes add a new private clause in the LibSQL adapter's select_fields function to handle empty field selections by returning "1" instead of constructing fields, and introduce a comprehensive test module validating Repo.exists?/2 behaviour across various query scenarios including empty selects and transactions.

Changes

Cohort / File(s) Summary
LibSQL Adapter
lib/ecto/adapters/libsql/connection.ex
Adds a new private clause to select_fields that returns "1" when handling empty field selections, providing a valid default for SELECT generation.
Test Suite
test/exists_query_test.exs
New test module with comprehensive ExUnit tests for Repo.exists?/2 functionality across empty selects, simple/complex where clauses, transactions, and includes test setup with TestRepo and User schema.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes

Poem

🐰 In fields so empty, "1" shall bloom,
Our tests hop through the query room,
LibSQL's path now smooth and bright,
Exists? Exists! We checked it right! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding support for generating valid SQL with SELECT 1 when Repo.exists? has empty field selections.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

1 participant