Skip to content

Rewrite SQLite-only functions in D1 CHECK constraints - #1399

Merged
no-itsbackpack merged 3 commits into
mainfrom
cursor/d1-sqlite-check-functions-d28b
Sep 8, 2026
Merged

Rewrite SQLite-only functions in D1 CHECK constraints#1399
no-itsbackpack merged 3 commits into
mainfrom
cursor/d1-sqlite-check-functions-d28b

Conversation

@no-itsbackpack

Copy link
Copy Markdown
Contributor

Problem

pscale import d1 copies SQLite CHECK / GENERATED expressions into Postgres DDL after requoting identifiers, but it left SQLite function names unchanged. Schema apply then fails with errors like:

ERROR: function json_valid(text) does not exist

That shows up on Drizzle JSON columns (CHECK (json_valid("row_json"))) and any other dump that uses SQLite-only builtins.

Fix

convertCheckExpr now rewrites SQLite-only functions and a few operators to Postgres:

  • json_valid(x)(x) IS JSON
  • ifnullcoalesce, iifCASE, instrstrpos
  • json_extract with a literal $.pathjsonb #>>
  • hex / unhex / quote / typeof / char / unicode
  • likely / unlikely / likelihood (no-ops)
  • datetime / unixepoch / strftime / julianday when they already have a DEFAULT mapping
  • GLOB / REGEXP against a string literal, and ===

Functions we cannot translate (printf, json_set, non-literal GLOB, …) stay in the expression and lint reports SQLITE_FUNCTION as an error so import fails early with a remediation instead of a raw psql error.

Tests

Unit and convert-pipeline coverage for the mappings above, including the Drizzle json_valid CHECK shape. assertValidPostgresDDL runs when a local Postgres is reachable.

Slack Thread

Open in Web Open in Cursor 

cursoragent and others added 2 commits September 7, 2026 23:27
pscale import d1 was passing json_valid() and other SQLite builtins
through into Postgres DDL, so schema apply died with "function does
not exist". Map the common CHECK/GENERATED functions to Postgres
equivalents and lint the ones we cannot translate.

Co-authored-by: Gomez <no-itsbackpack@users.noreply.github.com>
The function walker treated SQL keywords like IN as calls, which
collapsed "IN (0, 1)" to "IN(0, 1)" and broke boolean CHECK tests.
Skip parenthesized keywords, and match GLOB/REGEXP on the full
expression so the pattern literal is not split away.

Co-authored-by: Gomez <no-itsbackpack@users.noreply.github.com>
@no-itsbackpack
no-itsbackpack marked this pull request as ready for review September 7, 2026 23:35
@no-itsbackpack
no-itsbackpack requested a review from a team as a code owner September 7, 2026 23:35

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 6 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 0fcc678. Configure here.

Comment thread internal/import/d1/check_functions.go
Comment thread internal/import/d1/check_functions.go Outdated
Comment thread internal/import/d1/check_functions.go
Comment thread internal/import/d1/check_functions.go
Comment thread internal/import/d1/check_functions.go
Comment thread internal/import/d1/check_functions.go
Stop reusing the DEFAULT datetime mapper, which turned any
datetime(col) into now(). Only rewrite current-time forms.

Also: use jsonb_array_length for the two-arg path form, convert
only one-arg unhex, fold json_valid = 1/0 into IS [NOT] JSON,
and lint leftover REGEXP plus unconverted json_array_length.

Co-authored-by: Gomez <no-itsbackpack@users.noreply.github.com>
@no-itsbackpack
no-itsbackpack merged commit cf7e294 into main Sep 8, 2026
4 checks passed
@no-itsbackpack
no-itsbackpack deleted the cursor/d1-sqlite-check-functions-d28b branch September 8, 2026 02:02
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.

4 participants