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
21 changes: 21 additions & 0 deletions .changeset/34973-boolean-text-storage-convergence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@objectstack/driver-sql': patch
'@objectstack/cli': patch
---

fix(driver-sql): boot converges legacy text `'true'` / `'false'` in a SQLite boolean column to `1` / `0`, so boolean filters, sorts and groups find those rows

Clause-②: yes (widening: `@objectstack/driver-sql` exports `PendingSchemaWorkKind` from its index, and the CLI consumes it, so the union gains one published value, `normalize_boolean_storage`; boolean filters, sorts and groupBy on SQLite now answer legacy text rows as their boolean)
Metadata-protocol: none

Before the engine stored the boolean a reading spells, a door that sent the text `'true'` / `'false'` (the console's inline grid) left that text in a SQLite `Field.boolean` or `Field.toggle` column. Since the previous release those rows read back as the right boolean, but every comparison ran against the raw text: `won = false` missed a stored `'false'`, `won = true` missed a stored `'true'`, `$in`, `$ne` and `$nin` answered wrongly, ORDER BY sorted the text after every integer, and an aggregate groupBy presented a text `'false'` as `true`.

Schema sync now converges those cells at boot, the fourth storage convergence beside the datetime, time and JSON backfills (`backfillCanonicalBooleans`):

- **SQLite only.** Postgres, MSSQL, MySQL and Oracle coerce or refuse the text, so no other dialect can hold such a row.
- **One guarded `UPDATE` per column**: `'true'` becomes `1` and `'false'` becomes `0`, and nothing else is written. A second boot converts 0 rows.
- **Other text is never rewritten.** A value such as `'yes'` or `'TRUE'` names no boolean the driver can read without guessing; it is counted and logged at `warn`, and boolean filters keep missing it until the value is corrected at the source.
- **The record** is an `info` line per table and field with the rows converted and up to five of their ids. There is no down step: a text row already reads as its integer, so an undo would only restore the form the filters miss.
- **Dry run.** Under `os migrate plan` the step is listed in place as `normalize_boolean_storage`, with its row count, and nothing is written; `os migrate apply` runs it after the confirmation. `OS_SKIP_SCHEMA_SYNC=1` skips it, as it skips every other schema-sync step.

`os migrate plan`'s summary line now reads `N column(s) to converge in place` instead of `N temporal column(s) …`, because a boolean column is counted there too.
15 changes: 11 additions & 4 deletions content/docs/deployment/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,7 @@ performed. So `plan` really is a dry run, and everything `apply` is about to do

In place (existing rows converged when you apply)
~ crm_contact [normalize_datetime_storage: signed_at — 1,240 row update(s)]
~ crm_deal [normalize_boolean_storage: won — 312 row update(s)]

Needs confirmation
~ crm_contact.email [relax_not_null]
Expand All @@ -1189,10 +1190,16 @@ column needs when the database predates the canonical UTC storage (ADR-0053
addendum D-B1..D-B4). It carries a row count because that is the number
deciding whether to run it now; on MySQL it reads `widen_datetime_columns` and
is an `ALTER … MODIFY` table rebuild that holds a metadata lock for its
duration.

Both are safe to apply — the convergence preserves every stored instant and is
idempotent — but only the second takes time proportional to your data.
duration. On SQLite it also lists `normalize_boolean_storage`: rows of a
`Field.boolean` or `Field.toggle` column that an older build stored as the text
`'true'` / `'false'` become `1` / `0`, so boolean filters, sorts and groups find
them. Any other text in such a column is left as stored and reported in the
log.

All three are safe to apply — the datetime convergence preserves every stored
instant, the boolean step rewrites only the two canonical spellings, and each is
idempotent — but only the MySQL widen is a table rebuild whose duration grows with
your data.

#### Runtime-owned indexes apply builds

Expand Down
20 changes: 18 additions & 2 deletions packages/cli/src/utils/schema-migrate.pending-render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
*/

import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
// `schema-migrate.ts` reaches these through guarded dynamic imports
// (`schema-migration-plugins.ts`); loading them here pays the transform during
// collection, never inside a case's clocked window (`check:test-source-alias`).
import '@objectstack/driver-sql';
import '@objectstack/driver-turso';
import '@objectstack/platform-objects/plugin';
import '@objectstack/plugin-security';
import '@objectstack/rest';
import '@objectstack/runtime';
import '@objectstack/service-datasource';
import {
renderPendingSchemaWork,
splitRefusedSchemaWork,
Expand Down Expand Up @@ -44,6 +54,7 @@ const IN_PLACE: PendingSchemaWork[] = [
{ table: 'legacy', kind: 'widen_datetime_columns', columns: ['at', 'created_at'], rows: 42 },
{ table: 'shift', kind: 'normalize_time_storage', columns: ['starts_at'], rows: 7 },
{ table: 'shift_my', kind: 'widen_time_columns', columns: ['starts_at'], rows: 9 },
{ table: 'deal', kind: 'normalize_boolean_storage', columns: ['won'], rows: 3 },
];

describe('renderPendingSchemaWork (#3954)', () => {
Expand Down Expand Up @@ -82,6 +93,9 @@ describe('renderPendingSchemaWork (#3954)', () => {
expect(out()).toContain('7 row update(s)');
expect(out()).toContain('widen_time_columns: starts_at');
expect(out()).toContain('9 row table rebuild');
// The boolean convergence (#34973) is a row update in place, like the
// temporal backfills.
expect(out()).toContain('deal [normalize_boolean_storage: won — 3 row update(s)]');
});

it('shows both sections when both kinds are pending', async () => {
Expand Down Expand Up @@ -110,8 +124,10 @@ describe('summarizePendingSchemaWork (#3954)', () => {
const summary = await summarizePendingSchemaWork([...ADDITIVE, ...IN_PLACE]);
expect(summary).toContain('1 table(s) to create');
expect(summary).toContain('1 column(s) to add');
expect(summary).toContain('5 temporal column(s) to converge in place');
expect(summary).toContain('~1,234,625 rows');
// [#34973] Not "temporal": a boolean column converges in place too.
expect(summary).toContain('6 column(s) to converge in place');
expect(summary).not.toContain('temporal');
expect(summary).toContain('~1,234,628 rows');
});
});

Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/utils/schema-migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,9 @@ export async function summarizePendingSchemaWork(pending: PendingSchemaWork[]):
if (inPlace.length > 0) {
const cols = inPlace.reduce((n, p) => n + p.columns.length, 0);
const rows = inPlace.reduce((n, p) => n + (p.rows ?? 0), 0);
parts.push(`${cols} temporal column(s) to converge in place (~${formatRows(rows)} rows)`);
// [#34973] "column(s)", not "temporal column(s)": the boolean storage
// convergence (`normalize_boolean_storage`) is counted here too.
parts.push(`${cols} column(s) to converge in place (~${formatRows(rows)} rows)`);
}
// [#30636] Named only when there are some, like the in-place count above.
const rebuilds = pending.filter((p) => p.kind === 'rebuild_table').length;
Expand Down
11 changes: 9 additions & 2 deletions packages/drivers/driver-sql/src/schema-drift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,9 @@ export interface PendingSchemaWork {
* How much data the step touches, when that is knowable up front and worth
* knowing — absent for the additive kinds, which touch none.
*
* For `normalize_datetime_storage` it is the number of row-writes (summed
* across `columns`, since each is its own `UPDATE`). For
* For `normalize_datetime_storage` and `normalize_boolean_storage` it is the
* number of row-writes (summed across `columns`, since each is its own
* `UPDATE`). For
* `widen_datetime_columns` it is the table's row count, because MySQL's
* `ALTER … MODIFY` is a full rebuild holding a metadata lock — which is the
* number that decides "now" versus "in a maintenance window".
Expand Down Expand Up @@ -398,6 +399,11 @@ export interface PendingSchemaWork {
* section tells the operator the work is never data-losing, and that claim must
* not silently come to cover a row rewrite.
*
* [#34973] `normalize_boolean_storage` rewrites rows in place too: the SQLite
* boolean backfill, which turns legacy text `'true'` / `'false'` cells into
* `1` / `0`. It is in place by the rule below, and `os migrate plan` renders
* it as a row-update count beside the temporal ones.
*
* [#30636] Two more describe the one existing-table shape the additive path
* cannot adopt — keyed by `_id`, with no `id` column (`existing-table-refusal.ts`):
* `rebuild_table` drops and recreates it while it is EMPTY (in place: it is
Expand All @@ -418,6 +424,7 @@ export type PendingSchemaWorkKind =
| 'add_columns'
| 'normalize_datetime_storage'
| 'normalize_time_storage'
| 'normalize_boolean_storage'
| 'widen_datetime_columns'
| 'widen_time_columns'
| 'rebuild_table'
Expand Down
Loading
Loading