Rewrite SQLite-only functions in D1 CHECK constraints - #1399
Merged
Conversation
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
marked this pull request as ready for review
September 7, 2026 23:35
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 6 potential issues.
❌ 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.
mscoutermarsh
approved these changes
Sep 7, 2026
joshmgross
approved these changes
Sep 7, 2026
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Problem
pscale import d1copies SQLiteCHECK/GENERATEDexpressions into Postgres DDL after requoting identifiers, but it left SQLite function names unchanged. Schema apply then fails with errors like:That shows up on Drizzle JSON columns (
CHECK (json_valid("row_json"))) and any other dump that uses SQLite-only builtins.Fix
convertCheckExprnow rewrites SQLite-only functions and a few operators to Postgres:json_valid(x)→(x) IS JSONifnull→coalesce,iif→CASE,instr→strposjson_extractwith a literal$.path→jsonb #>>hex/unhex/quote/typeof/char/unicodelikely/unlikely/likelihood(no-ops)datetime/unixepoch/strftime/juliandaywhen they already have a DEFAULT mappingGLOB/REGEXPagainst a string literal, and==→=Functions we cannot translate (
printf,json_set, non-literalGLOB, …) stay in the expression and lint reportsSQLITE_FUNCTIONas an error so import fails early with a remediation instead of a rawpsqlerror.Tests
Unit and convert-pipeline coverage for the mappings above, including the Drizzle
json_validCHECK shape.assertValidPostgresDDLruns when a local Postgres is reachable.Slack Thread