|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#35341] On SQLite, a `text` field retyped to `boolean` keeps its TEXT |
| 5 | + * column, and the driver's own boolean writes landed there as the text |
| 6 | + * `'1.0'` / `'0.0'`: knex binds a JS boolean as `Number(b)`, better-sqlite3 |
| 7 | + * binds a JS number as a REAL, and TEXT affinity keeps the REAL as text. The |
| 8 | + * row read presented both as `true` (`presentStoredBoolean('0.0')` is |
| 9 | + * `Boolean('0.0')`), so a stored `false` read as `true` through `find()` and |
| 10 | + * `distinct()`, while the `= false` comparand, bound the same way, matched |
| 11 | + * `'0.0'` and missed the `'0'` rows the boot convergence writes on that column. |
| 12 | + * |
| 13 | + * The write and the comparand now bind `sqliteBooleanBinding`'s `'1'` / `'0'`, |
| 14 | + * which a TEXT-affinity column keeps as that text and the NUMERIC affinity of a |
| 15 | + * column the driver created folds into the INTEGER `1` / `0`; the boot |
| 16 | + * convergence rewrites the `'1.0'` / `'0.0'` rows already stored. Three |
| 17 | + * describes: the retyped column's new writes, its existing rows across a |
| 18 | + * boot, and the CONTROL — an INTEGER-form boolean column the driver created, |
| 19 | + * whose stored form and answers do not change. |
| 20 | + */ |
| 21 | + |
| 22 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 23 | +import type { DriverQuery } from '@objectstack/spec/contracts'; |
| 24 | +import { SqlDriver } from './sql-driver.js'; |
| 25 | + |
| 26 | +const RETYPED = 'bool_retyped_35341'; |
| 27 | +const EXISTING = 'bool_retyped_existing_35341'; |
| 28 | +const CONTROL = 'bool_integer_35341'; |
| 29 | + |
| 30 | +interface Logged { msg: string; meta?: any } |
| 31 | + |
| 32 | +function sqliteDriver(): SqlDriver { |
| 33 | + return new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true } as any); |
| 34 | +} |
| 35 | + |
| 36 | +const asText = (name: string) => ({ name, fields: { label: { type: 'string' }, flag: { type: 'text' } } }); |
| 37 | +const asBoolean = (name: string) => ({ name, fields: { label: { type: 'string' }, flag: { type: 'boolean' } } }); |
| 38 | + |
| 39 | +/** The rows every write door leaves, by id: which boolean each one was last written as. */ |
| 40 | +const WRITTEN: Record<string, boolean> = { |
| 41 | + bulk_false: false, bulk_true: true, bulkupd_false: false, create_false: false, create_true: true, |
| 42 | + many_false: false, update_false: false, upsert_false: false, |
| 43 | +}; |
| 44 | + |
| 45 | +/** Write `WRITTEN` through every door: create, update, upsert, bulkCreate, bulkUpdate, updateMany. */ |
| 46 | +async function writeThroughEveryDoor(driver: SqlDriver, table: string): Promise<void> { |
| 47 | + await driver.create(table, { id: 'create_false', label: 'x', flag: false }); |
| 48 | + await driver.create(table, { id: 'create_true', label: 'x', flag: true }); |
| 49 | + await driver.create(table, { id: 'update_false', label: 'x', flag: true }); |
| 50 | + await driver.update(table, 'update_false', { flag: false }); |
| 51 | + await driver.upsert(table, { id: 'upsert_false', label: 'x', flag: false }); |
| 52 | + await driver.bulkCreate(table, [{ id: 'bulk_false', label: 'x', flag: false }, { id: 'bulk_true', label: 'x', flag: true }]); |
| 53 | + await driver.create(table, { id: 'bulkupd_false', label: 'x', flag: true }); |
| 54 | + await driver.bulkUpdate(table, [{ id: 'bulkupd_false', data: { flag: false } }]); |
| 55 | + await driver.create(table, { id: 'many_false', label: 'x', flag: true }); |
| 56 | + await driver.updateMany(table, { where: { id: 'many_false' } } as DriverQuery, { flag: false }); |
| 57 | +} |
| 58 | + |
| 59 | +async function stored(driver: SqlDriver, table: string): Promise<Array<[string, unknown, string]>> { |
| 60 | + const rows = await (driver as any).knex.raw(`select id, flag, typeof(flag) as t from ${table} order by id`); |
| 61 | + return rows.map((r: any) => [r.id, r.flag, r.t]); |
| 62 | +} |
| 63 | + |
| 64 | +async function presented(driver: SqlDriver, table: string): Promise<Record<string, unknown>> { |
| 65 | + const rows = (await driver.find(table, {} as DriverQuery)) as Array<{ id: string; flag: unknown }>; |
| 66 | + return Object.fromEntries(rows.map((r) => [r.id, r.flag])); |
| 67 | +} |
| 68 | + |
| 69 | +async function idsWhere(driver: SqlDriver, table: string, where: Record<string, unknown>): Promise<string[]> { |
| 70 | + const rows = (await driver.find(table, { where } as DriverQuery)) as Array<{ id: string }>; |
| 71 | + return rows.map((r) => r.id).sort(); |
| 72 | +} |
| 73 | + |
| 74 | +const idsWritten = (value: boolean, rows: Record<string, boolean>) => |
| 75 | + Object.keys(rows).filter((id) => rows[id] === value).sort(); |
| 76 | + |
| 77 | +describe('[#35341] driver-sql — a boolean retyped from text: every write door stores 1 / 0 the reads and filters agree on (sqlite)', () => { |
| 78 | + let driver: SqlDriver; |
| 79 | + |
| 80 | + beforeAll(async () => { |
| 81 | + driver = sqliteDriver(); |
| 82 | + await driver.initObjects([asText(RETYPED)]); |
| 83 | + await driver.initObjects([asBoolean(RETYPED)]); |
| 84 | + await writeThroughEveryDoor(driver, RETYPED); |
| 85 | + }); |
| 86 | + |
| 87 | + afterAll(async () => { |
| 88 | + await driver.disconnect(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('the fixture: the retyped column is still TEXT', async () => { |
| 92 | + const cols = await (driver as any).knex.raw(`select type from pragma_table_info('${RETYPED}') where name = 'flag'`); |
| 93 | + expect(cols).toEqual([{ type: 'TEXT' }]); |
| 94 | + }); |
| 95 | + |
| 96 | + it('every write door stores the text 1 / 0, never 1.0 / 0.0', async () => { |
| 97 | + expect(await stored(driver, RETYPED)).toEqual( |
| 98 | + Object.keys(WRITTEN).sort().map((id) => [id, WRITTEN[id] ? '1' : '0', 'text']), |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it('find() presents each row as the boolean it was written as', async () => { |
| 103 | + expect(await presented(driver, RETYPED)).toEqual(WRITTEN); |
| 104 | + }); |
| 105 | + |
| 106 | + it('distinct() answers false and true', async () => { |
| 107 | + expect([...(await driver.distinct(RETYPED, 'flag'))].sort()).toEqual([false, true]); |
| 108 | + }); |
| 109 | + |
| 110 | + it('= false and = true select exactly the rows find() presents as false and true', async () => { |
| 111 | + expect(await idsWhere(driver, RETYPED, { flag: false })).toEqual(idsWritten(false, WRITTEN)); |
| 112 | + expect(await idsWhere(driver, RETYPED, { flag: true })).toEqual(idsWritten(true, WRITTEN)); |
| 113 | + }); |
| 114 | + |
| 115 | + it('$ne and $in agree too', async () => { |
| 116 | + expect(await idsWhere(driver, RETYPED, { flag: { $ne: true } })).toEqual(idsWritten(false, WRITTEN)); |
| 117 | + expect(await idsWhere(driver, RETYPED, { flag: { $in: [false] } })).toEqual(idsWritten(false, WRITTEN)); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('[#35341] driver-sql — a boot converges the 1.0 / 0.0 an older build stored on a retyped column (sqlite)', () => { |
| 122 | + let driver: SqlDriver; |
| 123 | + let pendingBeforeBoot: unknown; |
| 124 | + const info: Logged[] = []; |
| 125 | + const warn: Logged[] = []; |
| 126 | + const OLD: Record<string, boolean> = { old_false: false, old_true: true, old_zero: false }; |
| 127 | + |
| 128 | + beforeAll(async () => { |
| 129 | + driver = sqliteDriver(); |
| 130 | + await driver.initObjects([asText(EXISTING)]); |
| 131 | + const knex = (driver as any).knex; |
| 132 | + // What the driver's own writes stored on this column before the fix, and |
| 133 | + // the '0' an earlier convergence of a text 'false' left. |
| 134 | + await knex(EXISTING).insert([ |
| 135 | + { id: 'old_false', label: 'x', flag: '0.0' }, |
| 136 | + { id: 'old_true', label: 'x', flag: '1.0' }, |
| 137 | + { id: 'old_zero', label: 'x', flag: '0' }, |
| 138 | + ]); |
| 139 | + (driver as any).logger = { |
| 140 | + info: (msg: string, meta?: any) => { info.push({ msg, meta }); }, |
| 141 | + warn: (msg: string, meta?: any) => { warn.push({ msg, meta }); }, |
| 142 | + }; |
| 143 | + driver.setDeferredDdl(true); |
| 144 | + await driver.initObjects([asBoolean(EXISTING)]); |
| 145 | + pendingBeforeBoot = (await driver.previewDeferredSchemaWork()).filter((p) => p.kind === 'normalize_boolean_storage'); |
| 146 | + driver.setDeferredDdl(false); |
| 147 | + await driver.initObjects([asBoolean(EXISTING)]); |
| 148 | + }); |
| 149 | + |
| 150 | + afterAll(async () => { |
| 151 | + await driver.disconnect(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('os migrate plan counts the 1.0 / 0.0 rows as normalize_boolean_storage', () => { |
| 155 | + expect(pendingBeforeBoot).toEqual([{ table: EXISTING, kind: 'normalize_boolean_storage', columns: ['flag'], rows: 2 }]); |
| 156 | + }); |
| 157 | + |
| 158 | + it('the boot rewrites them to the text 1 / 0, withholds nothing, and says so', async () => { |
| 159 | + expect(await stored(driver, EXISTING)).toEqual([ |
| 160 | + ['old_false', '0', 'text'], ['old_true', '1', 'text'], ['old_zero', '0', 'text'], |
| 161 | + ]); |
| 162 | + expect(warn.filter((l) => l.msg.includes('unconverted'))).toEqual([]); |
| 163 | + const converted = info.filter((l) => l.msg.includes('canonicalised boolean storage')); |
| 164 | + expect(converted.map((l) => [l.meta.field, l.meta.rowsConverted])).toEqual([['flag', 2]]); |
| 165 | + }); |
| 166 | + |
| 167 | + it('find(), distinct() and = false then agree on every row', async () => { |
| 168 | + expect(await presented(driver, EXISTING)).toEqual(OLD); |
| 169 | + expect([...(await driver.distinct(EXISTING, 'flag'))].sort()).toEqual([false, true]); |
| 170 | + expect(await idsWhere(driver, EXISTING, { flag: false })).toEqual(idsWritten(false, OLD)); |
| 171 | + }); |
| 172 | +}); |
| 173 | + |
| 174 | +describe('[#35341] driver-sql — CONTROL: a boolean column the driver created stores and answers as before (sqlite)', () => { |
| 175 | + let driver: SqlDriver; |
| 176 | + |
| 177 | + beforeAll(async () => { |
| 178 | + driver = sqliteDriver(); |
| 179 | + await driver.initObjects([asBoolean(CONTROL)]); |
| 180 | + await writeThroughEveryDoor(driver, CONTROL); |
| 181 | + }); |
| 182 | + |
| 183 | + afterAll(async () => { |
| 184 | + await driver.disconnect(); |
| 185 | + }); |
| 186 | + |
| 187 | + it('every write door stores the INTEGER 1 / 0', async () => { |
| 188 | + expect(await stored(driver, CONTROL)).toEqual( |
| 189 | + Object.keys(WRITTEN).sort().map((id) => [id, WRITTEN[id] ? 1 : 0, 'integer']), |
| 190 | + ); |
| 191 | + }); |
| 192 | + |
| 193 | + it('find(), distinct() and = false / = true agree', async () => { |
| 194 | + expect(await presented(driver, CONTROL)).toEqual(WRITTEN); |
| 195 | + expect([...(await driver.distinct(CONTROL, 'flag'))].sort()).toEqual([false, true]); |
| 196 | + expect(await idsWhere(driver, CONTROL, { flag: false })).toEqual(idsWritten(false, WRITTEN)); |
| 197 | + expect(await idsWhere(driver, CONTROL, { flag: true })).toEqual(idsWritten(true, WRITTEN)); |
| 198 | + }); |
| 199 | +}); |
0 commit comments