Skip to content

fix(sqlite-kit): scaffold BOOLEAN columns as integer with boolean mode - #6186

Open
webdevsamran wants to merge 1 commit into
drizzle-team:rc5from
webdevsamran:fix/issue-6182-sqlite-boolean-scaffold
Open

fix(sqlite-kit): scaffold BOOLEAN columns as integer with boolean mode#6186
webdevsamran wants to merge 1 commit into
drizzle-team:rc5from
webdevsamran:fix/issue-6182-sqlite-boolean-scaffold

Conversation

@webdevsamran

Copy link
Copy Markdown

Summary of Changes

Fixes #6182

Problem

When pulling/introspecting a SQLite database schema with BOOLEAN columns and default values (e.g. pending BOOLEAN NOT NULL DEFAULT true), drizzle-kit previously mapped BOOLEAN under numericAffinities, generating numeric().default(true).notNull().
In Drizzle ORM, numeric() does not accept boolean literals as defaults, resulting in TypeScript compilation errors:
Argument of type 'boolean' is not assignable to parameter of type 'string | SQL<unknown>'.ts(2345)
Additionally, uppercase SQL defaults like DEFAULT TRUE / DEFAULT FALSE were scaffolded verbatim as undeclared identifiers.

Solution

  1. Affinity Mapping: Moved boolean and bool from numericAffinities to intAffinities and updated sqlTypeFrom() to return 'boolean'.
  2. Schema Generation: Int.toTs() now checks if the column type is boolean and outputs options: { mode: 'boolean' } with normalized lowercase 'true' / 'false' defaults, generating integer({"mode":"boolean"}).default(true) (which maps to SQLiteBooleanBuilder in Drizzle ORM).
  3. Default Normalization: Int.defaultFromIntrospect() normalizes case-insensitive TRUE/FALSE strings from SQLite catalog introspection into lowercase 'true'/'false'.
  4. Regression Tests: Added unit tests in drizzle-kit/tests/sqlite/grammar.test.ts verifying sqlTypeFrom, typeFor, toTs, and parseDefault with various boolean formats (BOOLEAN, bool, true, false, TRUE, FALSE, 1, 0, null).

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ebbf6b7862

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 125 to +128
defaultFromIntrospect: (value) => {
const it = trimChar(value, "'");
const lower = it.toLowerCase();
if (lower === 'true' || lower === 'false') return lower;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve quoted boolean defaults as SQL strings

When an existing schema declares BOOLEAN DEFAULT 'true' or BOOLEAN DEFAULT 'false', SQLite introspection returns a quoted string literal, but this normalization strips the quotes and converts it into a TypeScript boolean. The scaffolded schema therefore emits .default(true) or .default(false), recreating the default as integer 1/0 instead of the original text value and potentially changing query behavior; only unquoted SQL keywords should be normalized.

Useful? React with 👍 / 👎.

Comment on lines +422 to +425
if (
['boolean', 'bool'].some((it) => lowered.startsWith(it))
) {
return 'boolean';

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve boolean mode when scaffolding view columns

When a pulled view exposes a column inherited from a BOOLEAN/BOOL table column, SQLite reports that declared type for the view and this new mapping routes it to Int. However, createViewColumns() discards the options returned by Int.toTs() and emits integer() rather than integer({ mode: 'boolean' }), so the generated view field is typed and decoded as a number instead of a boolean. The view generator must propagate the boolean-mode option before mapping these types to boolean.

Useful? React with 👍 / 👎.

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