drizzle-team / drizzle-team/drizzle-orm

[BUG]: SQLite pull drops or merges CHECK constraints depending on DDL formatting

Open
#6,195 2 comments 0 reactions 0 assignees View on GitHub
bug drizzle/kit
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Report hasn't been filed before.

- [x] I have verified that the bug I'm about to report hasn't been filed before.

### What version of `drizzle-orm` are you using?

1.0.0-rc.4

### What version of `drizzle-kit` are you using?

1.0.0-rc.4

### Other packages

better-sqlite3@13.0.3

### Describe the Bug

On SQLite, whether `pull` keeps a CHECK constraint depends on how the DDL is formatted. `unnamedCheckPattern` is `/CHECK\s+\((.*)\)/gi`.

```sql
CREATE TABLE t (a TEXT CHECK(a <> ''), b INTEGER, CHECK (b > 0));
CREATE TABLE two (a INTEGER, CHECK (a > 0), CHECK (a < 10));
CREATE TABLE multi (b INTEGER, CHECK (
b > 0
));
```

`drizzle-kit pull` gives:

```ts
export const t = sqliteTable("t", {...}, (table) => [check("t_check_1", sql`b > 0`)]);
export const two = sqliteTable("two", {...}, (table) => [check("two_check_1", sql`a > 0), CHECK (a < 10)`)]);
export const multi = sqliteTable("multi", { b: integer() });
```

So `CHECK(a <> '')` is dropped, the two on one line are merged into one constraint with unbalanced SQL, and the multi-line one is dropped.

The no-space case looks like an oversight rather than intentional: `namedCheckPattern` on the line above already uses `\s*`. `\s+` isn't guarding against a suffix match either, since `/CHECK\s+\(/` still matches inside `RECHECK (a > 0)`. `\bCHECK\s*\(` covers both.

For the merged case, non-greedy looks like the fix but swaps one problem for another: `(.*?)` separates those two correctly and then truncates `CHECK (length(a) > 0)` to `length(a`. Might need paren matching instead.

Noticed while looking at #3407. Not sure how much of this is worth fixing together, since the comment-stripping in #5966 runs into these same two regexes.

Contributor guide

Open the contributing guide

Research direction

Start by locating unnamedCheckPattern and namedCheckPattern in the drizzle-kit pull implementation, then reproduce the three SQLite DDL examples from the issue. Confirm that CHECK constraints are preserved separately across no-space, same-line, multiline, and nested-parenthesis cases, and add or update the relevant pull tests if present.

Written by the indexing model from the issue text.

Assessment

Tech stack
sqlite, typescript
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.