Skip to content

Tighten sqlite3 parser reference facade#138

Open
mmkal wants to merge 4 commits into
issue-110-sqlite3-parser-schemadifffrom
bedtime/2026-05-27-pr111-parser-facade
Open

Tighten sqlite3 parser reference facade#138
mmkal wants to merge 4 commits into
issue-110-sqlite3-parser-schemadifffrom
bedtime/2026-05-27-pr111-parser-facade

Conversation

@mmkal
Copy link
Copy Markdown
Collaborator

@mmkal mmkal commented May 26, 2026

Summary

This stacked PR extends the parser-backed reference facade from PR #111 so partial-index where expressions are analyzed by sqlite3-parser instead of the planner token-scanning raw SQL.

The net effect is narrower direct drop-column planning: function names and collation names inside a partial index no longer look like dropped-column references, while real column references in the where expression still force the conservative table rebuild path.

Base PR: #111 (issue-110-sqlite3-parser-schemadiff).

Before / after

Before, the planner used sqlMentionsIdentifier(index.where, column.name), so these valid drops could be misread as partial-index dependencies:

create table t(x text, lower text);
create index t_x_partial on t(x) where lower(x) = 'a';
create table t(x text, nocase text);
create index t_x_partial on t(x) where x collate nocase = 'a';

Those now stay on the direct path:

alter table t drop column lower;
alter table t drop column nocase;

A true reference still rebuilds the table:

create table t(x int, y int);
create index t_x_partial on t(x) where y > 0;
-- rebuilding table "t": column "y" dropped (cannot drop in place)
alter table t rename to __sqlfu_old_t;
create table t(x int);
insert into t(x) select x from __sqlfu_old_t;
drop table __sqlfu_old_t;

Implementation

  • Add indexWhereReferenceFacts and indexWhereReferencesDroppedColumns to packages/sqlfu/src/schemadiff/sqlite/references.ts.
  • Keep all sqlite3-parser imports inside references.ts.
  • Replace the remaining plan.ts partial-index token scan with the sqlfu-owned facade helper.
  • Extend drop-column.sql schemadiff fixtures for lower(x), collate nocase, and where y > 0.

Tests

  • pnpm --filter sqlfu exec vitest run test/schemadiff/fixtures.test.ts
  • pnpm --filter sqlfu typecheck
  • pnpm exec oxfmt --check packages/sqlfu/src/schemadiff/sqlite/references.ts packages/sqlfu/src/schemadiff/sqlite/plan.ts

Note

Medium Risk
Changes SQLite drop-column vs rebuild decisions in schemadiff; incorrect reference extraction could pick the wrong migration path, though behavior is covered by fixture tests.

Overview
Partial-index WHERE clauses are now analyzed through the sqlite3-parser facade in references.ts instead of token-scanning index.where in the planner. Direct ALTER TABLE … DROP COLUMN eligibility uses indexWhereReferencesDroppedColumns, which parses CreateIndexStmt and only treats real column references as blockers.

Behavior change: dropping columns named like SQL builtins (e.g. lower, nocase) no longer misfires when those names appear only as a function or COLLATE in the partial index predicate; a genuine reference such as where y > 0 still forces a table rebuild. New schemadiff fixtures document those cases.

Reviewed by Cursor Bugbot for commit b62ca54. Bugbot is set up for automated code reviews on this repo. Configure here.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 26, 2026

Open in StackBlitz

npm i https://pkg.pr.new/sqlfu@138

commit: b62ca54

@mmkal mmkal marked this pull request as ready for review May 27, 2026 00:07
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