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
20 changes: 13 additions & 7 deletions content/docs/deployment/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1167,8 +1167,8 @@ 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)]

Safe (loosening — applied without --allow-destructive)
✓ crm_contact.email [relax_not_null]
Needs confirmation
~ crm_contact.email [relax_not_null]
```

Answering `n` leaves every table and row exactly as it was. The boot's first
Expand Down Expand Up @@ -1274,10 +1274,16 @@ occupancy on its own.

| Category | Examples | Applied by |
|----------|----------|------------|
| `safe` | relax `NOT NULL` → nullable, widen a `varchar`, create a declared index (a `UNIQUE` one only when its duplicate pre-flight comes back clean), replace a legacy installation-wide unique with its per-organization composite | `os migrate apply` (and dev auto-reconcile) |
| `needs_confirm` | non-narrowing type change, rebuild a non-unique index whose columns changed | `os migrate apply` — except `manual_column_type_change` (only `os migrate multi-value-columns --apply` runs it), and `manual_widen_varchar_to_text` and `unbuildable_index`, which nothing applies |
| `safe` | widen a `varchar`, widen a `date` column to the declared `datetime` (`widen_date_to_datetime`), create a declared index (a `UNIQUE` one only when its duplicate pre-flight comes back clean), replace a legacy installation-wide unique with its per-organization composite | `os migrate apply` (and dev auto-reconcile) |
| `needs_confirm` | relax `NOT NULL` → nullable (`relax_not_null`, ADR-0113 D5), non-narrowing type change, rebuild a non-unique index whose columns changed | `os migrate apply` — except `manual_column_type_change` (only `os migrate multi-value-columns --apply` runs it), and `manual_widen_varchar_to_text` and `unbuildable_index`, which nothing applies. Never dev auto-reconcile |
| `destructive` | drop an orphaned column or index, tighten `NOT NULL`, narrow a type, rebuild an index as `UNIQUE`, create a `UNIQUE` index existing rows already violate | `os migrate apply --allow-destructive` |

Publishing an object (a save in publish mode, a publish, or a package's publish-drafts) also applies
its column changes to the existing table: every `safe` column change, and a `relax_not_null` whose
field declares `storage: { notNull: false }`, since that declaration is the deliberate relax. Index
changes stay with `os migrate apply`. A column change the database could not apply leaves the publish
a success with an `object-column-changes-not-applied` warning on its answer naming each column.

On a Turso remote primary (`libsql://`), a tightened or relaxed `NOT NULL` and a dropped DEFAULT stay drift, because that transport runs no SQLite table rebuild. `os migrate plan` and `os migrate apply` list each such entry with `"os migrate apply" cannot apply it on this database: it needs the SQLite table rebuild, which the Turso REMOTE transport does not run.`, offer `--allow-destructive` only for the destructive changes that database can apply, and count the rest as drift a re-run cannot apply.

`--allow-destructive` refuses some drops until the data they would lose is
Expand Down Expand Up @@ -1318,9 +1324,9 @@ drift, and `--allow-destructive` will not delete it.

<Callout type="tip">
**Dev self-heal.** `os dev` runs the SQL driver with `autoMigrate: 'safe'`, so
safe changes (you just made a field optional; a `unique` field became
organization-scoped) are applied to your existing dev database automatically on
restart — no `os migrate` needed, no data loss. Auto-reconcile is **dev-only and
safe changes (a `unique` field became organization-scoped) are applied to your
existing dev database automatically on restart — no `os migrate` needed, no data
loss. Relaxing a `NOT NULL` column is not one of them: it is `needs_confirm`. Auto-reconcile is **dev-only and
never destructive**; it is force-disabled under `NODE_ENV=production`, where
every change is shown by `os migrate plan` before you apply it deliberately.
</Callout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,25 @@
* `engine.applyPublishedColumnChanges(name)`), and that the save still refuses
* the change the table does not promise (`number` → `text`, the representative
* `field_type_change`).
*
* The last describe owns the other half of the answer: a change the
* datasource cannot perform. On the Turso REMOTE face (`libsql://`) the relax
* needs the SQLite table rebuild that transport does not run, so the driver
* reports it `skipped`, and the publish must say so on its answer as the
* `object-column-changes-not-applied` advisory while the table keeps its
* NOT NULL. The remote transport talks to a SQLite file through
* `@libsql/client`, the double the driver's own remote-face pins use.
*/

import { describe, it, expect, beforeAll, afterAll, afterEach } from 'vitest';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { DatabaseSync } from 'node:sqlite';
import { createClient, type Client } from '@libsql/client';
import { ObjectQL } from '@objectstack/objectql';
import { SqlDriver } from '@objectstack/driver-sql';
import { TursoDriver } from '@objectstack/driver-turso';
import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol';
import { SysMetadata, SysMetadataHistoryObject, SysMetadataAuditObject } from '@objectstack/platform-objects/metadata';

Expand Down Expand Up @@ -89,20 +100,26 @@ const v3 = () => ({
});

const liveEngines: ObjectQL[] = [];
const liveClients: Client[] = [];
afterEach(async () => {
while (liveEngines.length) {
try { await liveEngines.pop()?.destroy(); } catch { /* noop */ }
}
while (liveClients.length) {
try { liveClients.pop()?.close(); } catch { /* noop */ }
}
});

const localDriver = () => new SqlDriver({
client: 'better-sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true,
});

async function boot() {
async function boot(makeDriver: () => SqlDriver = localDriver) {
const engine = new ObjectQL();
liveEngines.push(engine);
const driver = new SqlDriver({
client: 'better-sqlite3',
connection: { filename: ':memory:' },
useNullAsDefault: true,
});
const driver = makeDriver();
engine.registerDriver(driver, true);
await engine.init();
engine.registry.registerObject(SysMetadata as any, TEST_PACKAGE_ID);
Expand Down Expand Up @@ -182,3 +199,35 @@ describe('[#32590] publishing a field change on a live object — the table foll
expect((rig.engine.registry.getObject(OBJECT) as any)?.fields?.days?.type).toBe('number');
}, 60_000);
});

describe('[#32590] publishing a column change the datasource cannot perform — the answer says so', () => {
it('Turso REMOTE: the relax needs a table rebuild, so the publish succeeds with the not-applied advisory and the column keeps NOT NULL', async () => {
const file = join(home, `turso-remote-${Date.now()}.db`);
const client = createClient({ url: `file:${file}` });
liveClients.push(client);
const rig = await boot(() => new TursoDriver({ url: 'libsql://publish-column-changes.example.turso.io', client }));
/** `notnull` of the column as the remote database file reports it. */
const notNull = (name: string) => {
const db = new DatabaseSync(file, { readOnly: true });
try {
const rows = db.prepare(`PRAGMA table_info("${OBJECT}")`).all() as Array<{ name: string; notnull: number }>;
return rows.find((r) => r.name === name)?.notnull;
} finally {
db.close();
}
};

const first = await rig.saveAndPublish(v1());
expect(first.published?.success, JSON.stringify(first.published)).toBe(true);
await rig.engine.insert(OBJECT, { title: 'before', start_date: '2026-11-12', organization_id: ORG }, { context: SYSTEM });
expect(notNull('title')).toBe(1);

const second = await rig.saveAndPublish(v2());

expect(second.published?.success, JSON.stringify(second.published)).toBe(true);
const notApplied = (second.published?.advisories ?? []).filter((a: any) => a?.rule === COLUMN_CHANGES_NOT_APPLIED);
expect(notApplied.map((a: any) => a.severity), JSON.stringify(second.published)).toEqual(['warning']);
expect(notApplied[0].message).toContain("'title' (relax_not_null)");
expect(notNull('title')).toBe(1);
}, 60_000);
});
Loading