Skip to content
Merged
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
19 changes: 19 additions & 0 deletions .changeset/boolean-write-spelling-canonical.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@objectstack/driver-sql': patch
'@objectstack/driver-turso': patch
---

fix(driver-sql)!: a boolean field is stored as a boolean whatever spelling the caller sends

Clause-②: no (narrowing)
Metadata-protocol: none

<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export changes shape. What moves is the write door of the SQLite-family drivers (SqlDriver on SQLite, both faces of TursoDriver, SqliteWasmDriver) for a direct caller: a value on a declared boolean or toggle field that spells no boolean is refused instead of stored, and the spellings it accepts are stored as the boolean they spell. The engine's own door already refuses those values with invalid_boolean, so no authored metadata, seed or stored row is rewritten and nothing needs a tombstone. -->

**BREAKING** for a caller that writes through a SQLite-family driver directly (`create`, `update`, `upsert`, `bulkCreate`, `bulkUpdate`, `updateMany`) and sends a declared `boolean` / `toggle` field a value that spells no boolean, such as `'yes'`, `'TRUE'`, `''`, `2` or an array: it used to be stored as written and is now refused with `VALIDATION_ERROR` / 400 before anything is written. It ships `patch` because it is a non-protocol breaking change (AGENTS.md Post-Task Checklist step 3): no metadata author writes a driver call, so `Metadata-protocol` is `none`, and the engine's door already refuses those values.

A declared scalar `Field.boolean` / `Field.toggle` is now stored as a boolean on SQLite whatever spelling the write sends. `true` / `false`, the numbers `1` / `0` and the text `'1'` / `'0'`, `'true'` / `'false'` and `'1.0'` / `'0.0'` are bound as the boolean they spell, so a column the driver created holds the INTEGER `1` / `0` and a `text` column retyped to `boolean` holds the text `'1'` / `'0'`. These are the spellings the local boot convergence already rewrites, read from the same table. Before, a string reached the column as text, on the local and the remote face of `TursoDriver` alike, so filters, sorts and groups did not read the row as the boolean it was written as. That includes a `beforeInsert` / `beforeUpdate` hook that assigns a string to such a field: the engine's own rewrite runs before the hooks, so the assignment now lands as a boolean too. On the remote face, a number written to a `toggle` field is stored as `'1'` / `'0'` instead of `'1.0'` / `'0.0'`.

A remote `TursoDriver.bulkCreate` checks every row before it writes the first one, so a refused row refuses the batch with nothing written, as the local face's single INSERT already did. `null` still writes NULL. Rows stored before this change keep their stored form.

To keep a direct write working, send `true` or `false` (or one of the spellings above) for a boolean or toggle field.
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* A declared scalar `Field.boolean` / `Field.toggle` is stored as a boolean on
* SQLite whatever spelling the caller hands a write door, and a value that
* spells no boolean is refused instead of stored.
*
* The engine writes the boolean its validator judged, but a caller that writes
* through the driver itself (a plugin, a migration, a `beforeInsert` hook's
* assignment, which lands after the engine's rewrite) reached `formatInput`
* with the text `'true'` / `'false'`, and SQLite kept that TEXT beside rows
* holding the INTEGER `1` / `0`. Every filter, sort and group then ran against
* the raw cell: `'true' <> 1` holds in SQLite, so a row written as `'true'`
* satisfied `$ne: true`. Any other text (`'yes'`) was stored as itself and read
* back as `Boolean('yes')`.
*
* The accepted spellings are exactly the ones the boot convergence reads
* (`SQLITE_BOOLEAN_TEXT_READINGS`): `true` / `false`, the numbers `1` / `0`,
* and the text `'1'` / `'0'`, `'true'` / `'false'` and `'1.0'` / `'0.0'`. Each
* is bound as `sqliteBooleanBinding`'s `'1'` / `'0'`, which a column the driver
* created stores as the INTEGER `1` / `0` and a TEXT-affinity column (a `text`
* field retyped to `boolean`) as the text `'1'` / `'0'`. Anything else answers
* `VALIDATION_ERROR` / 400 on every door and writes nothing.
*/

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import type { DriverQuery } from '@objectstack/spec/contracts';
import { lowerFilterCondition } from '@objectstack/spec/data';
import { SqlDriver } from './sql-driver.js';

const CREATED = 'bool_spellings_created';
const RETYPED = 'bool_spellings_retyped';

function sqliteDriver(): SqlDriver {
return new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true } as any);
}

const asText = (name: string) => ({ name, fields: { label: { type: 'string' }, flag: { type: 'text' }, live: { type: 'text' } } });
const asBoolean = (name: string) => ({ name, fields: { label: { type: 'string' }, flag: { type: 'boolean' }, live: { type: 'toggle' } } });

/** Every spelling a write door accepts, and the boolean it is stored as. */
const ACCEPTED: ReadonlyArray<readonly [unknown, boolean]> = [
[true, true], [false, false], [1, true], [0, false], ['1', true], ['0', false],
['true', true], ['false', false], ['1.0', true], ['0.0', false],
];

/** Values that spell no boolean: refused on every door. */
const REFUSED: ReadonlyArray<unknown> = ['yes', 'TRUE', ' true', '', 2, -1, Number.NaN, ['true'], { value: true }];

type Door = 'create' | 'update' | 'upsert' | 'bulkCreate' | 'bulkUpdate' | 'updateMany';
const DOORS: readonly Door[] = ['create', 'update', 'upsert', 'bulkCreate', 'bulkUpdate', 'updateMany'];

/** Write `value` to both boolean fields of row `id` through `door`; update doors first create the row as `seed`. */
async function writeThrough(driver: SqlDriver, table: string, door: Door, id: string, value: unknown, seed = false): Promise<void> {
const row = { id, label: door, flag: value, live: value };
switch (door) {
case 'create': await driver.create(table, row); return;
case 'upsert': await driver.upsert(table, row); return;
case 'bulkCreate': await driver.bulkCreate(table, [row]); return;
default: break;
}
await driver.create(table, { id, label: door, flag: seed, live: seed });
const patch = { flag: value, live: value };
if (door === 'update') await driver.update(table, id, patch);
else if (door === 'bulkUpdate') await driver.bulkUpdate(table, [{ id, data: patch }]);
else await driver.updateMany(table, { where: { id } } as DriverQuery, patch);
}

async function storedCell(driver: SqlDriver, table: string, id: string): Promise<Array<[unknown, string]> | null> {
const rows = await (driver as any).knex.raw(`select flag, typeof(flag) as t, live, typeof(live) as lt from ${table} where id = ?`, [id]);
return rows.length ? [[rows[0].flag, rows[0].t], [rows[0].live, rows[0].lt]] : null;
}

const label = (v: unknown) => (typeof v === 'number' && Number.isNaN(v) ? 'NaN' : JSON.stringify(v));

for (const [table, form] of [[CREATED, 'INTEGER'], [RETYPED, 'TEXT']] as const) {
describe(`driver-sql — a boolean / toggle field on a ${form}-form column is stored as a boolean whatever spelling it is written in (sqlite)`, () => {
let driver: SqlDriver;
const stored = (b: boolean) => (form === 'INTEGER' ? [b ? 1 : 0, 'integer'] : [b ? '1' : '0', 'text']);

beforeAll(async () => {
driver = sqliteDriver();
if (form === 'TEXT') await driver.initObjects([asText(table)]);
await driver.initObjects([asBoolean(table)]);
});

afterAll(async () => {
await driver.disconnect();
});

it('the fixture: the column forms are the ones named', async () => {
const cols = await (driver as any).knex.raw(`select name, type from pragma_table_info('${table}') where name in ('flag', 'live') order by name`);
expect(cols.map((c: any) => c.type)).toEqual(form === 'INTEGER' ? ['boolean', 'boolean'] : ['TEXT', 'TEXT']);
});

for (const door of DOORS) {
it(`${door}: every accepted spelling is stored as its boolean`, async () => {
const got: Record<string, unknown> = {};
const want: Record<string, unknown> = {};
for (const [i, [value, b]] of ACCEPTED.entries()) {
const id = `${door}_ok_${i}`;
// An update door seeds the opposite value, so a write that changed nothing is seen.
await writeThrough(driver, table, door, id, value, !b);
got[label(value)] = await storedCell(driver, table, id);
want[label(value)] = [stored(b), stored(b)];
}
expect(got).toEqual(want);
});

it(`${door}: a value that spells no boolean is refused with VALIDATION_ERROR / 400 and writes nothing`, async () => {
const verdicts: Record<string, unknown> = {};
const want: Record<string, unknown> = {};
for (const [i, value] of REFUSED.entries()) {
const id = `${door}_no_${i}`;
const outcome = await writeThrough(driver, table, door, id, value, true).then(
() => 'written',
(e: any) => ({ code: e?.code, status: e?.status }),
);
const cell = await storedCell(driver, table, id);
verdicts[label(value)] = { outcome, cell };
// An insert door leaves no row; an update door leaves the seeded `true` as it was.
const cellWant = ['create', 'upsert', 'bulkCreate'].includes(door) ? null : [stored(true), stored(true)];
want[label(value)] = { outcome: { code: 'VALIDATION_ERROR', status: 400 }, cell: cellWant };
}
expect(verdicts).toEqual(want);
});
}

it('null still writes NULL, and a write that names neither field leaves them as they were', async () => {
await driver.create(table, { id: 'nul', label: 'x', flag: null, live: null });
expect(await storedCell(driver, table, 'nul')).toEqual([[null, 'null'], [null, 'null']]);
await driver.create(table, { id: 'untouched', label: 'x', flag: 'true', live: 'false' });
await driver.update(table, 'untouched', { label: 'y' });
expect(await storedCell(driver, table, 'untouched')).toEqual([stored(true), stored(false)]);
});

it('$ne: true / $nin: [true] / $not of = true never select a row written as the text true', async () => {
await driver.create(table, { id: 'neg_text_true', label: 'neg', flag: 'true', live: 'true' });
await driver.create(table, { id: 'neg_text_false', label: 'neg', flag: 'false', live: 'false' });
await driver.create(table, { id: 'neg_bool_true', label: 'neg', flag: true, live: true });
// The filter as a seam hands it to the driver (the shared lowering).
const ids = async (where: Record<string, unknown>) => {
const lowered = lowerFilterCondition({ label: 'neg', ...where }, { isDatetimeColumn: () => false });
return ((await driver.find(table, { where: lowered } as DriverQuery)) as Array<{ id: string }>).map((r) => r.id).sort();
};
for (const field of ['flag', 'live']) {
expect(await ids({ [field]: { $ne: true } })).toEqual(['neg_text_false']);
expect(await ids({ [field]: { $nin: [true] } })).toEqual(['neg_text_false']);
expect(await ids({ $not: { [field]: true } })).toEqual(['neg_text_false']);
expect(await ids({ [field]: { $ne: false } })).toEqual(['neg_bool_true', 'neg_text_true']);
expect(await ids({ [field]: true })).toEqual(['neg_bool_true', 'neg_text_true']);
}
});
});
}
21 changes: 18 additions & 3 deletions packages/drivers/driver-sql/src/sql-driver-schema-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ import { activeConcurrentIndexDeferral, attachUniqueConstraintSql, clearConcurre
import { isJsonColumnType, mediaColumnMoveDialect, mediaColumnMovePlan, type MediaColumnMovePlan, type MediaColumnMoveRefusal, type MediaColumnMoveScan } from './media-column-move.js';
import { StandardErrorCode } from '@objectstack/spec/api';
import { recoverUnencodedJsonText } from './unencoded-json-text.js';
import { SQLITE_BOOLEAN_TEXT_SPELLINGS } from './sql-driver-sqlite-serialisation.js';
import { renderExistingTableAdoptionRefusal } from './existing-table-refusal.js';
import { renderDeclaredRenameRefusal } from './declared-rename.js';
import { aggregandColumnClass } from '@objectstack/core';
Expand Down Expand Up @@ -2058,8 +2059,22 @@ export async function backfillCanonicalTimes(this: Pick<SchemaSyncHost, 'canonic
* `boolean`), the driver's own writes stored the REAL its JS number bound as,
* as that text, until it bound `sqliteBooleanBinding`'s `'1'` / `'0'`. `IN`
* compares with the BINARY collation, so `'TRUE'` or `'yes'` is not selected.
* The four are the `legacy` rows of `SQLITE_BOOLEAN_TEXT_SPELLINGS`, the table
* the write door binds from, so no write stores one of them any more and
* neither door nor convergence can take a spelling the other does not.
*/
const SQLITE_LEGACY_BOOLEAN_TEXT = `typeof(??) = 'text' and ?? in ('true', 'false', '1.0', '0.0')`;
const SQLITE_LEGACY_BOOLEAN_TEXT = `typeof(??) = 'text' and ?? in (${sqlTextList((s) => s.legacy)})`;

/**
* The spellings of {@link SQLITE_BOOLEAN_TEXT_SPELLINGS} that `pick` keeps, as a
* SQL list of string literals (`'true', 'false', '1.0', '0.0'` for the legacy
* ones). The table is the write door's too (`sqliteBooleanWriteReading`), so
* the convergence and the write accept one set of spellings. Each spelling is
* a constant of that table, never a caller's value, and holds no quote.
*/
function sqlTextList(pick: (s: (typeof SQLITE_BOOLEAN_TEXT_SPELLINGS)[number]) => boolean): string {
return SQLITE_BOOLEAN_TEXT_SPELLINGS.filter(pick).map((s) => `'${s.text}'`).join(', ');
}

/**
* [#34973] How many ids of the converted rows the backfill's `info` line
Expand Down Expand Up @@ -2126,7 +2141,7 @@ export async function backfillCanonicalBooleans(this: Pick<SchemaSyncHost, 'bool
// `1` / `0`: it already reads as its boolean, so it is not withheld.
const counted: any = await this.knex.raw(
`select count(*) as text_rows, coalesce(sum(case when ${SQLITE_LEGACY_BOOLEAN_TEXT} then 1 else 0 end), 0) as legacy, ` +
`coalesce(sum(case when ?? in ('1', '0') then 1 else 0 end), 0) as canonical ` +
`coalesce(sum(case when ?? in (${sqlTextList((s) => !s.legacy)}) then 1 else 0 end), 0) as canonical ` +
`from ?? where typeof(??) = 'text'`,
[field, field, field, table, field],
);
Expand All @@ -2139,7 +2154,7 @@ export async function backfillCanonicalBooleans(this: Pick<SchemaSyncHost, 'bool
[table, field, field, BOOLEAN_BACKFILL_SAMPLE_IDS],
);
const res = await this.knex.raw(
`update ?? set ?? = case when ?? in ('true', '1.0') then 1 else 0 end where ${SQLITE_LEGACY_BOOLEAN_TEXT}`,
`update ?? set ?? = case when ?? in (${sqlTextList((s) => s.legacy && s.value)}) then 1 else 0 end where ${SQLITE_LEGACY_BOOLEAN_TEXT}`,
[table, field, field, field, field],
);
// better-sqlite3 reports `changes`; a client whose raw path returns
Expand Down
Loading
Loading