Skip to content

fix(editor): quote PostgreSQL table completion identifiers - #791

Open
mikevillari wants to merge 1 commit into
libredb:mainfrom
mikevillari:fix/postgres-table-completion-quotes
Open

fix(editor): quote PostgreSQL table completion identifiers#791
mikevillari wants to merge 1 commit into
libredb:mainfrom
mikevillari:fix/postgres-table-completion-quotes

Conversation

@mikevillari

@mikevillari mikevillari commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

PostgreSQL table suggestions currently insert catalog names verbatim, so accepting My_Schema_With_Caps.My_Table_With_Caps produces unquoted SQL that PostgreSQL folds to lowercase. This also reproduces with the completion provider from before #715 (base 4c5f657).

Follow-up to @nktnet1's report in #705: #705 (comment).

Pass the editor's existing connection dialect into the completion provider and use the shared identifier quoter for PostgreSQL table-name components. Keep labels/filtering separate from inserted SQL, preserve qualified replacement ranges and bare-label fallback, and recognize quoted table names when offering columns after a dot. Re-register when the connection dialect changes. Other dialects keep their existing table insertion behavior.

Tests assert applied edits for mixed-case schema/table names, typed qualifiers, dot-triggered suggestions, bare labels, embedded quotes, spaces, reserved words and lowercase names; they also cover quoted column lookup and dialect changes in the editor.

Testing

  • Seven new applied-edit cases failed before the fix; they pass afterward. Two quoted column-lookup regressions also failed before the lookup change and pass afterward.
  • Focused provider regression selection: 23 pass, 0 fail.
  • Complete isolated component runner: all 35 groups passed, including the new dialect-switch regression.
  • Format, lint, typecheck, knip, chart/check, channels/showcase/check, readme/check and security/check passed. Lint retains existing warnings.
  • Production build and library build passed.
  • bun run test: 11,143 pass, 197 fail in the initial unit/API/integration stage; 191 were chart tests requiring missing Helm, four packaging tests require missing 7-Zip, and two were the quoted-column regressions subsequently fixed and rerun green in the focused selection. The chained later stages therefore did not run through this command; components were run separately above. No full-suite or full-coverage pass claimed. Browser E2E and live PostgreSQL were not run locally; CI remains the merge gate.

This uses the provider's existing dot-delimited table-label representation. It does not redesign schema metadata or add support for completing partially quoted input or quoting column suggestions.

AI-assisted implementation and validation.

@codecov

codecov Bot commented Sep 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@cevheri

cevheri commented Sep 11, 2026

Copy link
Copy Markdown
Member

I need your local test results regarding the following points:

  1. PostgreSQL tables can be defined entirely in uppercase or lowercase (e.g., user_authority / USER_AUTHORITY) and must be queryable without quotes.
  2. Tables might have been created with mixed-case names enclosed in quotes (e.g., "UserAuthority").
  3. The table might reside in the public schema or a different schema (e.g., public.orders, authschema.users).

@mikevillari

Copy link
Copy Markdown
Contributor Author

Verified locally against a real PostgreSQL 18.4 process, using the exact PR head 0a53b7e25c2dfc3d4018043dcf01fd6088a1f02f. No code change was needed for these cases.

Results: 36 autocomplete-generated queries passed, plus 21 control queries with the expected outcomes.

I created each of these in public, authschema, and additionally a quoted mixed-case schema "AuthSchema":

CREATE TABLE identifier Name stored in the catalog Unquoted table references PR autocomplete result
lowercase_table lowercase_table lowercase and uppercase both work quoted exact catalog name; query succeeds
UPPERCASE_TABLE uppercase_table lowercase and uppercase both work "uppercase_table"; query succeeds
"MixedCaseTable" MixedCaseTable fail with SQLSTATE 42P01, as expected "MixedCaseTable"; query succeeds
"QUOTED_UPPER_TABLE" QUOTED_UPPER_TABLE fails with SQLSTATE 42P01, as expected "QUOTED_UPPER_TABLE"; query succeeds

The important distinction in point 1 is how the table was created: unquoted CREATE TABLE USER_AUTHORITY stores user_authority, so both unquoted spellings resolve. Explicitly quoted CREATE TABLE "USER_AUTHORITY" creates a different, case-sensitive name that requires quotes. This PR quotes the name returned by the catalog; it does not make previously valid unquoted SQL invalid. It does insert optional quotes for ordinary lowercase catalog names.

Example queries actually generated and executed:

SELECT marker FROM "lowercase_table";
SELECT marker FROM "uppercase_table";
SELECT marker FROM public."MixedCaseTable";
SELECT marker FROM "authschema"."MixedCaseTable";
SELECT marker FROM "AuthSchema"."QUOTED_UPPER_TABLE";

For both public and authschema, controls also executed fully unquoted queries such as SELECT marker FROM public.UPPERCASE_TABLE and SELECT marker FROM authschema.lowercase_table successfully.

Method: create isolated fixture tables containing distinct marker rows; read their names from information_schema.tables; construct the cache with the same display-name convention as postgres.ts:1478 (bare name for public, schema-qualified otherwise); bundle and call this PR's actual registerSQLCompletionProvider; apply its returned replacement range and insertText; execute the resulting SQL and assert the exact expected marker. Covered empty-prefix selection, partially typed names, explicit public qualifiers, and non-public schema-dot suggestions. Used a modeled Monaco word/range interface, not the browser UI or the full database-provider connection flow. The pre-existing public-schema-dot popup and partially quoted input were not claimed as covered.

Server reported: PostgreSQL 18.4 on x86_64-apple-darwin24.6.0, compiled by Apple clang version 17.0.0, 64-bit. This was a temporary local native PostgreSQL server, not an in-memory SQL substitute. The server was stopped and its database removed after verification. AI-assisted execution and reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants