-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(sqlite-kit): scaffold BOOLEAN columns as integer with boolean mode #6186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: rc5
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,6 +97,8 @@ const intAffinities = [ | |
| 'unsigned big int', | ||
| 'int2', | ||
| 'int8', | ||
| 'boolean', | ||
| 'bool', | ||
| ]; | ||
|
|
||
| export const Int: SqlType<'timestamp' | 'timestamp_ms'> = { | ||
|
|
@@ -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; | ||
| 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); | ||
|
|
@@ -169,7 +190,6 @@ export const Real: SqlType = { | |
| const numericAffinities = [ | ||
| 'numeric', | ||
| 'decimal', | ||
| 'boolean', | ||
| 'date', | ||
| 'datetime', | ||
| ]; | ||
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a pulled view exposes a column inherited from a 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'; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an existing schema declares
BOOLEAN DEFAULT 'true'orBOOLEAN 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 integer1/0instead of the original text value and potentially changing query behavior; only unquoted SQL keywords should be normalized.Useful? React with 👍 / 👎.