Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions drizzle-kit/src/dialects/sqlite/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ const intAffinities = [
'unsigned big int',
'int2',
'int8',
'boolean',
'bool',
];

export const Int: SqlType<'timestamp' | 'timestamp_ms'> = {
Expand All @@ -122,16 +124,35 @@ export const Int: SqlType<'timestamp' | 'timestamp_ms'> = {
},
defaultFromIntrospect: (value) => {
const it = trimChar(value, "'");
const lower = it.toLowerCase();
if (lower === 'true' || lower === 'false') return lower;
Comment on lines 125 to +128

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 👍 / 👎.

const check = Number(it);
if (Number.isNaN(check)) return value; // unknown
if (check >= Number.MIN_SAFE_INTEGER && check <= Number.MAX_SAFE_INTEGER) return it;
return it; // bigint
},
toTs: (value) => {
toTs: (value, type) => {
const isBool = type?.toLowerCase() === 'boolean' || type?.toLowerCase() === 'bool';
if (isBool) {
let def = '';
if (value !== undefined && value !== null && value !== '') {
const lower = String(value).toLowerCase().trim();
if (lower === 'true' || lower === '1') {
def = 'true';
} else if (lower === 'false' || lower === '0') {
def = 'false';
} else {
def = `sql\`${value}\``;
}
}
return { def, options: { mode: 'boolean' } };
}

if (!value) return '';

if (value === 'true' || value === 'false') {
return { def: value, options: { mode: 'boolean' } };
const lower = String(value).toLowerCase().trim();
if (lower === 'true' || lower === 'false') {
return { def: lower, options: { mode: 'boolean' } };
}

const check = Number(value);
Expand Down Expand Up @@ -169,7 +190,6 @@ export const Real: SqlType = {
const numericAffinities = [
'numeric',
'decimal',
'boolean',
'date',
'datetime',
];
Expand Down Expand Up @@ -399,9 +419,15 @@ export function sqlTypeFrom(sqlType: string): string {
return 'real';
}

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

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 👍 / 👎.

}

// https://www.sqlite.org/datatype3.html -> 3.1.1. Affinity Name Examples
if (
['numeric', 'decimal', 'boolean', 'date', 'datetime'].some((it) => lowered.startsWith(it))
['numeric', 'decimal', 'date', 'datetime'].some((it) => lowered.startsWith(it))
) {
return 'numeric';
}
Expand Down
52 changes: 51 additions & 1 deletion drizzle-kit/tests/sqlite/grammar.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseSqliteDdl, parseTableSQL, parseViewSQL, stripSqlComments } from 'src/dialects/sqlite/grammar';
import { parseDefault, parseSqliteDdl, parseTableSQL, parseViewSQL, sqlTypeFrom, stripSqlComments, typeFor } from 'src/dialects/sqlite/grammar';
import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'vitest';
import { prepareTestDatabase, TestDatabase } from './mocks';

Expand Down Expand Up @@ -262,3 +262,53 @@ describe('parse ddl', (t) => {
});
});
});

describe('sqlite boolean column scaffolding and defaults (issue #6182)', () => {
test('sqlTypeFrom maps BOOLEAN and bool to boolean', () => {
expect(sqlTypeFrom('BOOLEAN')).toBe('boolean');
expect(sqlTypeFrom('boolean')).toBe('boolean');
expect(sqlTypeFrom('BOOL')).toBe('boolean');
expect(sqlTypeFrom('bool')).toBe('boolean');
});

test('typeFor boolean returns integer import with boolean mode options', () => {
const grammarType = typeFor('boolean');
expect(grammarType.drizzleImport()).toBe('integer');

expect(grammarType.toTs('true', 'boolean')).toStrictEqual({
def: 'true',
options: { mode: 'boolean' },
});
expect(grammarType.toTs('false', 'boolean')).toStrictEqual({
def: 'false',
options: { mode: 'boolean' },
});
expect(grammarType.toTs('TRUE', 'boolean')).toStrictEqual({
def: 'true',
options: { mode: 'boolean' },
});
expect(grammarType.toTs('FALSE', 'boolean')).toStrictEqual({
def: 'false',
options: { mode: 'boolean' },
});
expect(grammarType.toTs('1', 'boolean')).toStrictEqual({
def: 'true',
options: { mode: 'boolean' },
});
expect(grammarType.toTs('0', 'boolean')).toStrictEqual({
def: 'false',
options: { mode: 'boolean' },
});
expect(grammarType.toTs(null, 'boolean')).toStrictEqual({
def: '',
options: { mode: 'boolean' },
});
});

test('parseDefault normalizes boolean literals from introspection', () => {
expect(parseDefault('BOOLEAN', 'true')).toBe('true');
expect(parseDefault('BOOLEAN', 'TRUE')).toBe('true');
expect(parseDefault('BOOLEAN', 'false')).toBe('false');
expect(parseDefault('BOOLEAN', 'FALSE')).toBe('false');
});
});