Skip to content

fix(psql): strip trailing semicolon before appending auto-LIMIT - #58

Merged
NicolaasGrobler merged 8 commits into
mainfrom
fix/38-psql-auto-limit-trailing-semicolon
May 15, 2026
Merged

fix(psql): strip trailing semicolon before appending auto-LIMIT#58
NicolaasGrobler merged 8 commits into
mainfrom
fix/38-psql-auto-limit-trailing-semicolon

Conversation

@NicolaasGrobler

@NicolaasGrobler NicolaasGrobler commented May 15, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • The psql console's auto-LIMIT helper appended LIMIT 1000 verbatim, so SELECT 1; produced SELECT 1; LIMIT 1000 — two statements, the second invalid (syntax error at or near "LIMIT").
  • Extracted the inline helper from MainContent.tsx into a pure src/utils/applyQueryLimit.ts so it can be unit-tested without jsdom, then strip any trailing ; + whitespace before appending.

Testing

  • New src/utils/applyQueryLimit.test.ts covering the regression (SELECT 1;, SELECT 1, SELECT 1; , SELECT 1 WHERE x = 1; all → ... LIMIT 1000 with no trailing ;) plus skip-cases (non-SELECT, already-has-LIMIT, CTE, UNION).
  • npx tsc --noEmit clean.

Closes #38

Summary by CodeRabbit

  • New Features

    • Automatically applies safe row limits to eligible SQL queries, handling trailing semicolons, end-of-query comments, existing limits, and complex query forms to avoid unintended modifications.
  • Tests

    • Added thorough regression tests covering trailing-semicolon/comment trimming, preservation of inline comments, non-SELECT behavior, existing limits, and complex-query detection.

Review Change Stack

The psql console's auto-LIMIT helper appended ` LIMIT 1000` verbatim,
so `SELECT 1;` produced `SELECT 1; LIMIT 1000` — two statements, the
second invalid. Strip any trailing `;` and surrounding whitespace
first. Adds Vitest coverage so it can't regress.

Closes #38
@vercel

vercel Bot commented May 15, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
queryden Ready Ready Preview, Comment May 15, 2026 9:08am

@coderabbitai

coderabbitai Bot commented May 15, 2026

Copy link
Copy Markdown
Contributor

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

Extracts an inline SQL LIMIT helper into src/utils/applyQueryLimit.ts, replaces the inline helper in MainContent.tsx with the import, and adds Vitest tests validating trailing-semicolon and other edge-case behaviors.

Changes

Query Limit Utility Extraction and Enhancement

Layer / File(s) Summary
applyQueryLimit implementation
src/utils/applyQueryLimit.ts
New exported applyQueryLimit(query, maxRows) detects SELECT-like queries (skips non-SELECT, SHOW/EXPLAIN/RETURNING), avoids double-appending LIMIT, skips complex queries (CTEs, parenthesized SELECTs, UNION/INTERSECT/EXCEPT), strips trailing semicolons and end-of-query comments, and appends LIMIT <maxRows> to eligible queries.
applyQueryLimit Vitest suite
src/utils/applyQueryLimit.test.ts
Adds tests confirming trailing semicolon (and semicolon+whitespace) are removed before appending LIMIT, trailing line/block comments after a semicolon are stripped, inline comments are preserved, non-SELECT queries are unchanged, existing LIMIT is not duplicated, and CTE/UNION queries are unmodified.
MainContent component integration
src/components/layout/MainContent.tsx
Replaces the inline applyQueryLimit helper with an import from ../../utils/applyQueryLimit and adjusts executeQuery to rely on the shared utility.

🎯 3 (Moderate) | ⏱️ ~20 minutes

🐰 I found a stray semicolon last night,
Snipped it off tidy, then set LIMIT right.
Tests hop around to ensure no surprise,
Queries now run clean beneath open skies. 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: fixing the psql auto-LIMIT feature by stripping trailing semicolons before appending LIMIT, which directly addresses issue #38.
Linked Issues check ✅ Passed The PR fully addresses issue #38 objectives: strips trailing semicolons before appending LIMIT, includes comprehensive Vitest tests covering the regression cases and skip conditions, and type-checking passes.
Out of Scope Changes check ✅ Passed All changes are in-scope: extracting the inline helper to a testable utility, implementing the fix for trailing semicolons, and adding comprehensive test coverage for issue #38.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/38-psql-auto-limit-trailing-semicolon

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 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 `@src/utils/applyQueryLimit.test.ts`:
- Around line 10-28: The test suite is missing a regression test for queries
that end with a SQL line comment after a semicolon (e.g. "SELECT 1; --
comment"), which currently allows the LIMIT to be commented out; add a new spec
in applyQueryLimit.test.ts under the "trailing semicolon stripping" describe
block that calls applyQueryLimit with an input like "SELECT 1; -- comment" (and
one with trailing whitespace after the comment) and expects "SELECT 1 LIMIT
1000" to ensure applyQueryLimit correctly strips the semicolon and comment
before appending the LIMIT.

In `@src/utils/applyQueryLimit.ts`:
- Line 49: The current trimmed = query.trim().replace(/;\s*$/, "") fails when
the query ends with a trailing SQL comment; update this logic to first strip
trailing comments (both single-line -- and block /* */) from the trimmed query
(reuse the same comment-stripping approach used earlier in the function for
query detection), then remove a trailing semicolon from that comment-stripped
string, and finally use that cleaned string for appending the LIMIT (or reattach
preserved comments after adding LIMIT if desired). Target the trimmed variable
and the semicolon-removal step in the applyQueryLimit function so trailing
comments cannot cause the LIMIT to be commented out.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8e0f2399-f989-4e7f-a25c-4b82520d92d3

📥 Commits

Reviewing files that changed from the base of the PR and between bf43c8f and c087d18.

📒 Files selected for processing (3)
  • src/components/layout/MainContent.tsx
  • src/utils/applyQueryLimit.test.ts
  • src/utils/applyQueryLimit.ts

Comment thread src/utils/applyQueryLimit.test.ts
Comment thread src/utils/applyQueryLimit.ts Outdated
Follow-up to the original #38 fix: when a query ends with a SQL comment
after the semicolon (e.g. `SELECT 1; -- foo`), the regex `/;\s*$/`
doesn't match (the string ends with `"foo"`, not `;`), so LIMIT was
appended after the comment. Since `--` extends to end-of-line, the
LIMIT clause was silently commented out — a worse outcome than the
original syntax error, because the safety LIMIT was completely bypassed
without warning.

Strip trailing whitespace, semicolons, line comments (`--`), and block
comments (`/* */`) iteratively so any combination at the tail is
handled. Surgical to the tail only — inline comments mid-query are
preserved.

Tests added for: trailing line comment, trailing block comment,
multi-line block comment, line comment with no trailing semicolon, and
a regression guard that an inline mid-query comment is preserved.

Flagged by CodeRabbit on PR #58.
@NicolaasGrobler
NicolaasGrobler merged commit 10459c8 into main May 15, 2026
9 of 10 checks passed
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.

psql console: auto-LIMIT appended after trailing semicolon produces syntax error

1 participant