fix(db): let the move to Postgres change nothing silently

The move stripped NUL from a plugin's own text, merging keys that were
distinct, and turned a plugin's values into another type (text in a BLOB
column into bytea, numbers in an untyped column into text). Its checksum
was built from the converted values, so both passed as verified.

A plugin's data now moves exactly as the Postgres plugin store would
hand it back, or not at all: a NUL, a value of another storage class
than its column, or an integer past 2^53 refuses the move before
anything is written, listing plugin, table, row and column. A column
left open by SQLite's affinity takes the kind its values need, so text
in a BLOB column stays text.

TREK's own text holding a NUL from an older install is refused by
default too. --drop-nul drops it, lists every changed value in the
summary, and names a unique key that dropping a NUL broke.

Every table's checksum is now built from the values read from SQLite,
so a conversion that changes a value fails the verification.
This commit is contained in:
Maurice
2026-11-17 23:28:01 +01:00
parent 6040b60e86
commit bc6728678b
10 changed files with 655 additions and 97 deletions
@@ -1,5 +1,5 @@
/**
* MOVE-001..016: the move of a SQLite install into Postgres
* MOVE-001..025: the move of a SQLite install into Postgres
* (src/db/engine/postgres/sqlite-move/), over real SQLite files and a
* stand-in for Postgres that keeps what it is sent (tests/helpers/fake-postgres-move.ts):
* the refusals before anything is written, the copy in foreign key order with
@@ -15,7 +15,12 @@ import {
USAGE,
type MoveCommandIo,
} from '../../../../../../src/db/engine/postgres/sqlite-move/command';
import { batchRows, describePgError, insertSql } from '../../../../../../src/db/engine/postgres/sqlite-move/copy';
import {
batchRows,
describePgError,
insertSql,
refusedRowsMessage,
} from '../../../../../../src/db/engine/postgres/sqlite-move/copy';
import {
migrationLevelProblem,
MoveRefusal,
@@ -82,6 +87,7 @@ const SCHEMA: FakeTableDef[] = [
],
primaryKey: ['id'],
serial: ['id'],
unique: [{ name: 'users_name_unique', columns: ['name'] }],
},
];
@@ -308,6 +314,134 @@ describe('moveSqliteToPostgres', () => {
});
});
describe('nothing is changed silently', () => {
/** A plugin's `plugin.db` under the plugins' data directory. */
function pluginDb(pluginId: string, sql: string): void {
fs.mkdirSync(path.join(dir, 'plugins-data', pluginId), { recursive: true });
sqlite(path.join(dir, 'plugins-data', pluginId, 'plugin.db'), sql);
}
it("MOVE-021: a plugin's NUL or a value that would change type refuses the move before anything is written", async () => {
pluginDb(
'nul-notes',
`create table notes (id integer primary key, tag text, body text);
insert into notes values (1, 'team' || char(0) || 'a', 'secret' || char(0) || 'payload'), (2, 'teama', 'other');
create table kv (k text primary key, v);
insert into kv values ('a', 5), ('b', 1.5), ('c', 'x');`,
);
const failure = moveSqliteToPostgres(options());
await expect(failure).rejects.toBeInstanceOf(MoveRefusal);
const message = await failure.catch((err: Error) => err.message);
expect(message).toContain(
'4 value(s) in plugin data cannot move to Postgres unchanged; nothing was written:\n' +
' - plugin nul-notes: kv row k=a, column v: the value 5 is stored as an INTEGER, but the column holds text ' +
'on Postgres, so the plugin would read it back as another type\n' +
' - plugin nul-notes: kv row k=b, column v: the value 1.5 is stored as a REAL',
);
expect(message).toContain(
' - plugin nul-notes: notes row id=1, column tag: the text "team\\u0000a" holds a NUL character (U+0000), ' +
'which Postgres text cannot hold\n' +
' - plugin nul-notes: notes row id=1, column body: the text "secret\\u0000payload" holds a NUL character',
);
expect(message).toMatch(/A plugin's own data moves exactly as the plugin stored it, or not at all/);
// Refused before the target was even looked at: nothing created, nothing inserted.
expect(pg.statements).toEqual([]);
});
it('MOVE-022: a BLOB column holding text stays text, and a column holding only numbers stays numbers', async () => {
pluginDb(
'typed-notes',
`create table docs (id integer primary key, payload blob, raw blob, data);
insert into docs values (1, '{"a":1}', x'00ff', 5), (2, 'plain', null, 1.5);`,
);
const summary = await moveSqliteToPostgres(options());
expect(pg.statements).toContain(
'create table "trek_plugin_typed_notes"."docs" ("id" bigint generated by default as identity, ' +
'"payload" text, "raw" bytea, "data" double precision, primary key ("id"))',
);
expect(pg.rowsOf('"trek_plugin_typed_notes"."docs"')).toEqual([
['1', '{"a":1}', Buffer.from([0, 255]), 5],
['2', 'plain', null, 1.5],
]);
expect(summary.plugins[0].tables).toEqual([{ label: 'docs', rows: 2, checksum: expect.any(String) }]);
});
it("MOVE-023: a NUL in TREK's own text is refused by default, and with dropNul dropped and listed", async () => {
sqlite(
path.join(dir, 'travel.db'),
`update users set name = 'Ad' || char(0) || 'a' where id = 1;
update trips set meta = '{"k\\u0000":1}' where id = 1;`,
);
const failure = moveSqliteToPostgres(options());
await expect(failure).rejects.toBeInstanceOf(MoveRefusal);
const message = await failure.catch((err: Error) => err.message);
expect(message).toContain(
'2 value(s) in the SQLite file cannot be stored in Postgres exactly; nothing was written:\n' +
' - users row id=1, column name: the text "Ad\\u0000a" holds a NUL character (U+0000), which Postgres text cannot hold\n' +
' - trips row id=1, column meta: the text "{\\"k\\\\u0000\\":1}" holds a NUL character (U+0000), which ' +
'Postgres text cannot hold (escaped as \\u0000)\n' +
'2 of them hold a NUL character (U+0000). TREK no longer stores this character and drops it on its next write. ' +
'Run the move again with --drop-nul',
);
expect(pg.statements.at(-1)).toBe(CLEAR_SCHEMA_SQL);
const summary = await moveSqliteToPostgres(options({ dropNul: true }));
expect(pg.rowsOf('"users"')).toEqual([
['1', 'Ada'],
['2', 'Grace'],
]);
expect(pg.rowsOf('"trips"')[0][3]).toBe('{"k":1}');
expect(summary.nulDropped).toEqual(['users row id=1, column name', 'trips row id=1, column meta']);
expect(renderSummary(summary, 'postgres://db/trek')).toContain(
' --drop-nul removed NUL characters (U+0000) from 2 value(s):\n' +
' users row id=1, column name\n' +
' trips row id=1, column meta',
);
});
it('MOVE-024: a unique key that dropping a NUL broke is named as that, never as a bare duplicate', async () => {
sqlite(path.join(dir, 'travel.db'), "update users set name = 'Ad' || char(0) || 'a' where id = 2;");
pg.failOn = /^insert into "users"/;
pg.failWith = {
code: '23505',
constraint: 'users_name_unique',
detail: 'Key (name)=(Ada) already exists.',
};
await expect(moveSqliteToPostgres(options({ dropNul: true }))).rejects.toThrow(
'Postgres refused rows of users: refused by the fake; constraint users_name_unique; Key (name)=(Ada) already ' +
'exists.\nThe key is broken because --drop-nul removed NUL characters (U+0000) from 1 value(s) of its ' +
'column(s) name, and a value that differed from another only by a NUL equals it once the NUL is gone:\n' +
' - users row id=2, column name\n' +
'Change or remove one of the two rows in the SQLite file (with TREK stopped), then run the move again.',
);
// A broken key whose columns lost no NUL is reported as Postgres reported it.
pg.failWith = { code: '23505', constraint: 'users_pkey', detail: 'Key (id)=(2) already exists.' };
await expect(moveSqliteToPostgres(options({ dropNul: true }))).rejects.toThrow(
/^Postgres refused rows of users: refused by the fake; constraint users_pkey; Key \(id\)=\(2\) already exists\.$/,
);
});
it('MOVE-025: a refused key is explained by every dropped NUL of the table when the key is not known', () => {
const spec = {
source: 't',
target: '"t"',
label: 't',
columns: [],
deferred: [],
primaryKey: [],
exact: false,
uniqueKeys: [],
};
const dropped = [{ where: 't row 1, column a', column: 'a' }];
const err = Object.assign(new Error('duplicate key'), { code: '23505', constraint: 'gone' });
expect(refusedRowsMessage(spec, err, dropped)).toMatch(
/from 1 value\(s\) of t, and a value .*\n {2}- t row 1, column a\n/,
);
expect(refusedRowsMessage(spec, new Error('other'), dropped)).toBe('Postgres refused rows of t: other');
expect(refusedRowsMessage(spec, err, [])).toBe('Postgres refused rows of t: duplicate key; constraint gone');
});
});
describe('migrationLevelProblem', () => {
it('MOVE-014: a file at this level passes; older, newer or unversioned ones say what to run', () => {
const known = new Set([M1, M2]);
@@ -327,12 +461,19 @@ describe('the pieces', () => {
expect(pluginColumnKind('INTEGER', classes('text'))).toBe('integer');
expect(pluginColumnKind('VARCHAR(20)', classes())).toBe('text');
expect(pluginColumnKind('BLOB', classes('blob'))).toBe('bytes');
expect(pluginColumnKind('BLOB', classes('integer'))).toBe('text');
expect(pluginColumnKind('BLOB', classes('integer'))).toBe('integer');
// Text in a column declared BLOB stays text: the plugin reads a string back, not a Buffer.
expect(pluginColumnKind('BLOB', classes('text'))).toBe('text');
expect(pluginColumnKind('BLOB', classes('blob', 'text'))).toBe('text');
expect(pluginColumnKind('BLOB', classes())).toBe('bytes');
expect(pluginColumnKind('DOUBLE', classes())).toBe('float');
expect(pluginColumnKind('REAL', classes('text'))).toBe('text');
// A REAL column keeps its type; text in it is refused by the copy, not turned into text with the numbers.
expect(pluginColumnKind('REAL', classes('text'))).toBe('float');
expect(pluginColumnKind('BOOLEAN', classes('integer'))).toBe('integer');
expect(pluginColumnKind('', classes('integer', 'real'))).toBe('float');
expect(pluginColumnKind('', classes('blob'))).toBe('bytes');
expect(pluginColumnKind('', classes())).toBe('text');
expect(pluginColumnKind('', classes('integer', 'text'))).toBe('text');
expect(pluginColumnKind('DATETIME', classes('text'))).toBe('text');
expect(pluginDefault(null, 'text')).toBeNull();
@@ -394,6 +535,7 @@ describe('runMoveCommand', () => {
tables: [{ label: 'users', rows: 2, checksum: 'x' }],
plugins: [],
sequences: 1,
nulDropped: [],
warnings: ['careful'],
seconds: 0.4,
};
@@ -421,6 +563,7 @@ describe('runMoveCommand', () => {
const help = io();
expect(await runMoveCommand(['--help'], help.io)).toBe(0);
expect(help.out).toEqual([USAGE]);
expect(USAGE).toMatch(/\[--drop-nul\][\s\S]*--drop-nul +drop NUL characters \(U\+0000\) from TREK's own text/);
const unknown = io();
expect(await runMoveCommand(['--to=x'], unknown.io)).toBe(1);
expect(unknown.err[0]).toMatch(/The move was refused: Unknown argument --to=x/);
@@ -436,8 +579,12 @@ describe('runMoveCommand', () => {
expect.objectContaining({
sqliteFile: path.join(dir, 'other.db'),
pluginsDataDir: expect.stringMatching(/plugins-data$/),
dropNul: false,
}),
);
const dropping = io();
expect(await runMoveCommand(['--drop-nul'], dropping.io)).toBe(0);
expect(dropping.io.move).toHaveBeenCalledWith(expect.objectContaining({ dropNul: true }));
const printed = run.out.join('\n');
expect(printed).toContain('postgres://trek:***@db:5432/trek');
expect(printed).not.toContain('secret');
@@ -63,7 +63,12 @@ describe('planCopyOrder', () => {
describe('planTrekTables', () => {
const target: TargetSchema = {
tables: [
{ name: 'mikro_orm_migrations', columns: [{ name: 'name', dataType: 'text', nullable: false }], primaryKey: [] },
{
name: 'mikro_orm_migrations',
columns: [{ name: 'name', dataType: 'text', nullable: false }],
primaryKey: [],
uniqueKeys: [],
},
{
name: 'messages',
columns: [
@@ -71,6 +76,7 @@ describe('planTrekTables', () => {
{ name: 'reply_to', dataType: 'bigint', nullable: true },
],
primaryKey: ['id'],
uniqueKeys: [{ name: 'messages_pkey', columns: ['id'] }],
},
{
name: 'users',
@@ -79,6 +85,7 @@ describe('planTrekTables', () => {
{ name: 'avatar', dataType: 'bytea', nullable: true },
],
primaryKey: ['id'],
uniqueKeys: [],
},
],
foreignKeys: [{ table: 'messages', columns: ['reply_to'], references: 'messages' }],
@@ -96,7 +103,13 @@ describe('planTrekTables', () => {
);
expect(plan.problems).toEqual([]);
expect(plan.specs.map((spec) => spec.target)).toEqual(['"messages"', '"users"']);
expect(plan.specs[0]).toMatchObject({ deferred: ['reply_to'], primaryKey: ['id'], label: 'messages' });
expect(plan.specs[0]).toMatchObject({
deferred: ['reply_to'],
primaryKey: ['id'],
label: 'messages',
exact: false,
uniqueKeys: [{ name: 'messages_pkey', columns: ['id'] }],
});
expect(plan.specs[1].columns).toEqual([
{ name: 'id', kind: 'integer' },
{ name: 'avatar', kind: 'bytes' },
@@ -119,8 +132,9 @@ describe('planTrekTables', () => {
{ name: 'sent_at', dataType: 'timestamp with time zone', nullable: true },
],
primaryKey: ['id'],
uniqueKeys: [],
},
{ name: 'extra', columns: [], primaryKey: [] },
{ name: 'extra', columns: [], primaryKey: [], uniqueKeys: [] },
],
};
const plan = planTrekTables(
@@ -143,7 +157,7 @@ describe('planTrekTables', () => {
});
describe('readTargetSchema', () => {
it('MOVEORD-007: reads the columns, keys and sequences of the current schema', async () => {
it('MOVEORD-007: reads the columns, keys, unique keys and sequences of the current schema', async () => {
const pg = new FakePostgres([
{
name: 'trips',
@@ -154,6 +168,7 @@ describe('readTargetSchema', () => {
primaryKey: ['id'],
foreignKeys: [{ columns: ['user_id'], references: 'users' }],
serial: ['id'],
unique: [{ name: 'trips_user_id_unique', columns: ['user_id'] }],
},
{ name: 'users', columns: [{ name: 'id', dataType: 'bigint', nullable: false }], primaryKey: ['id'] },
]);
@@ -166,8 +181,17 @@ describe('readTargetSchema', () => {
{ name: 'id', dataType: 'bigint', nullable: false },
{ name: 'user_id', dataType: 'bigint', nullable: true },
],
uniqueKeys: [
{ name: 'trips_pkey', columns: ['id'] },
{ name: 'trips_user_id_unique', columns: ['user_id'] },
],
},
{
name: 'users',
primaryKey: ['id'],
columns: [{ name: 'id', dataType: 'bigint', nullable: false }],
uniqueKeys: [{ name: 'users_pkey', columns: ['id'] }],
},
{ name: 'users', primaryKey: ['id'], columns: [{ name: 'id', dataType: 'bigint', nullable: false }] },
]);
expect(schema.foreignKeys).toEqual([{ table: 'trips', columns: ['user_id'], references: 'users' }]);
expect(schema.sequences).toEqual([{ table: 'trips', column: 'id', sequence: 'public.trips_id_seq' }]);
@@ -1,14 +1,16 @@
/**
* MOVEVAL-001..010: how a SQLite value becomes the value a Postgres column of
* the move's target takes, the text both sides are compared in, and the
* order-free table checksum (src/db/engine/postgres/sqlite-move/values.ts).
* MOVEVAL-001..014: how a SQLite value becomes the value a Postgres column of
* the move's target takes, under TREK's rules and under a plugin's, the text
* both sides are compared in, and the order-free table checksum
* (src/db/engine/postgres/sqlite-move/values.ts).
*/
import {
beyondSafeInteger,
canonicalFromPostgres,
canonicalFromSent,
canonicalFromSource,
columnKind,
convertValue,
PLUGIN_RULES,
stableJson,
TableChecksum,
type ColumnKind,
@@ -19,6 +21,7 @@ import path from 'node:path';
import { describe, expect, it } from 'vitest';
const ok = (value: unknown) => ({ ok: true, value });
const DROP_NUL = { exact: false, dropNul: true };
describe('columnKind', () => {
it('MOVEVAL-001: knows every column type the Postgres baseline uses', () => {
@@ -80,11 +83,15 @@ describe('convertValue', () => {
expect(convertValue('', 'float')).toMatchObject({ ok: false });
});
it('MOVEVAL-005: text takes strings and numbers, drops a NUL as every write does, and refuses a BLOB', () => {
it('MOVEVAL-005: text takes strings and numbers, refuses a NUL unless told to drop it, and refuses a BLOB', () => {
expect(convertValue('Lisboa', 'text')).toEqual(ok('Lisboa'));
expect(convertValue(7n, 'text')).toEqual(ok('7'));
expect(convertValue(2.5, 'text')).toEqual(ok('2.5'));
expect(convertValue('a\u0000b\u0000', 'text')).toEqual(ok('ab'));
expect(convertValue('a\u0000b\u0000', 'text')).toEqual({
ok: false,
reason: 'the text "a\\u0000b\\u0000" holds a NUL character (U+0000), which Postgres text cannot hold',
});
expect(convertValue('a\u0000b\u0000', 'text', DROP_NUL)).toEqual({ ok: true, value: 'ab', droppedNul: true });
// The six characters of an escape are text like any other.
expect(convertValue('a\\u0000b', 'text')).toEqual(ok('a\\u0000b'));
expect(convertValue(new Uint8Array([0]), 'text')).toMatchObject({
@@ -97,15 +104,25 @@ describe('convertValue', () => {
});
});
it('MOVEVAL-006: JSON must parse, and loses the NUL characters it escapes', () => {
it('MOVEVAL-006: JSON must parse, and loses the NUL characters it escapes only when told to', () => {
expect(convertValue('{"a":1}', 'json')).toEqual(ok('{"a":1}'));
expect(convertValue(5n, 'json')).toEqual(ok('5'));
expect(convertValue('{broken', 'json')).toMatchObject({
ok: false,
reason: expect.stringMatching(/is not valid JSON/),
});
expect(convertValue('"\\u0000"', 'json')).toEqual(ok('""'));
expect(convertValue('{"a\\u0000":["x\\u0000y",1]}', 'json')).toEqual(ok('{"a":["xy",1]}'));
expect(convertValue('"\\u0000"', 'json')).toMatchObject({
ok: false,
reason: expect.stringMatching(/holds a NUL character \(U\+0000\), .* \(escaped as \\u0000\)$/),
});
expect(convertValue('"\\u0000"', 'json', DROP_NUL)).toEqual({ ok: true, value: '""', droppedNul: true });
expect(convertValue('{"a\\u0000":["x\\u0000y",1]}', 'json', DROP_NUL)).toEqual({
ok: true,
value: '{"a":["xy",1]}',
droppedNul: true,
});
// An escaped backslash followed by u0000 holds no NUL, and stays exactly as it is.
expect(convertValue('"\\\\u0000"', 'json', DROP_NUL)).toEqual(ok('"\\\\u0000"'));
// A raw NUL is not valid JSON in the first place.
expect(convertValue('"\u0000"', 'json')).toMatchObject({ ok: false, reason: expect.stringMatching(/valid JSON/) });
expect(convertValue(new Uint8Array([1]), 'json')).toMatchObject({
@@ -130,24 +147,33 @@ describe('convertValue', () => {
});
describe('the compared text', () => {
const cases: Array<[ColumnKind, Parameters<typeof canonicalFromSent>[0], string]> = [
['integer', '-12', '-12'],
const cases: Array<[ColumnKind, Parameters<typeof canonicalFromSource>[0], string]> = [
['integer', ' -12 ', '-12'],
['integer', 9007199254740993n, '9007199254740993'],
['integer', 42, '42'],
['float', 0.30000000000000004, '0.30000000000000004'],
['float', 1e21, '1e+21'],
['float', 12n, '12'],
['float', '3.5e2', '350'],
['text', 'São Paulo', 'São Paulo'],
['text', 7n, '7'],
['json', '{"b":1,"a":[2,{"d":1,"c":0}]}', '{"a": [2, {"c": 0, "d": 1}], "b": 1}'],
['bytes', Buffer.from([0, 171]), '\\x00ab'],
['json', 5n, '5'],
['bytes', new Uint8Array([0, 171]), '\\x00ab'],
];
it.each(cases)('MOVEVAL-009: %s values compare equal as sent and as Postgres prints them', (kind, sent, printed) => {
expect(canonicalFromSent(sent, kind)).toBe(canonicalFromPostgres(printed, kind));
expect(canonicalFromSent(null, kind)).toBeNull();
expect(canonicalFromPostgres(null, kind)).toBeNull();
});
it.each(cases)(
'MOVEVAL-009: %s values compare equal as read from SQLite and as Postgres prints them',
(kind, read, printed) => {
expect(canonicalFromSource(read, kind)).toBe(canonicalFromPostgres(printed, kind));
expect(canonicalFromSource(null, kind)).toBeNull();
expect(canonicalFromPostgres(null, kind)).toBeNull();
},
);
it('MOVEVAL-009b: text sent for a BLOB compares as its bytes, and a float as Postgres spells it', () => {
expect(canonicalFromSent('ab', 'bytes')).toBe('6162');
expect(canonicalFromPostgres('1e+20', 'float')).toBe(canonicalFromSent(1e20, 'float'));
it('MOVEVAL-009b: text read for a BLOB compares as its bytes, and a float as Postgres spells it', () => {
expect(canonicalFromSource('ab', 'bytes')).toBe('6162');
expect(canonicalFromPostgres('1e+20', 'float')).toBe(canonicalFromSource(1e20, 'float'));
expect(canonicalFromPostgres('plain', 'bytes')).toBe('plain');
expect(stableJson('{"z":null,"y":true}')).toBe('{"y":true,"z":null}');
});
@@ -176,3 +202,63 @@ describe('the compared text', () => {
expect(sum([['null']])).not.toBe(sum([[null]]));
});
});
describe("a plugin's values (PLUGIN_RULES)", () => {
const exact = (value: Parameters<typeof convertValue>[0], kind: ColumnKind) =>
convertValue(value, kind, PLUGIN_RULES);
const changed =
/is stored as .*, but the column holds .* on Postgres, so the plugin would read it back as another type/;
it('MOVEVAL-011: a value keeps its storage class and its bytes, as the Postgres plugin store hands it back', () => {
expect(exact(42n, 'integer')).toEqual(ok('42'));
expect(exact(1.5, 'float')).toEqual(ok(1.5));
expect(exact(3n, 'float')).toEqual(ok(3));
expect(exact('{"a":1}', 'text')).toEqual(ok('{"a":1}'));
expect(exact(new Uint8Array([0, 1]), 'bytes')).toEqual(ok(Buffer.from([0, 1])));
expect(exact(null, 'text')).toEqual(ok(null));
});
it('MOVEVAL-012: a value that would come back as another type is refused, not converted', () => {
expect(exact(1.5, 'text')).toEqual({
ok: false,
reason:
'the value 1.5 is stored as a REAL, but the column holds text on Postgres, ' +
'so the plugin would read it back as another type',
});
expect(exact(5n, 'text')).toMatchObject({ ok: false, reason: expect.stringMatching(changed) });
expect(exact('{"a":1}', 'bytes')).toMatchObject({ ok: false, reason: expect.stringMatching(changed) });
expect(exact(' 7', 'integer')).toMatchObject({ ok: false, reason: expect.stringMatching(changed) });
expect(exact(2.5, 'integer')).toMatchObject({ ok: false, reason: expect.stringMatching(changed) });
expect(exact('2.5', 'float')).toMatchObject({ ok: false, reason: expect.stringMatching(changed) });
expect(exact(new Uint8Array([1]), 'text')).toMatchObject({ ok: false, reason: expect.stringMatching(changed) });
expect(exact(9007199254740993n, 'integer')).toMatchObject({
ok: false,
reason: expect.stringMatching(/beyond 2\^53, which the Postgres plugin store refuses to read/),
});
expect(exact(-9007199254740993n, 'float')).toMatchObject({
ok: false,
reason: expect.stringMatching(/more digits than a double holds/),
});
});
it('MOVEVAL-013: a NUL in a plugin value is refused, never dropped, whatever the operator asked for', () => {
expect(exact('team\u0000a', 'text')).toEqual({
ok: false,
reason: 'the text "team\\u0000a" holds a NUL character (U+0000), which Postgres text cannot hold',
});
expect(convertValue('team\u0000a', 'text', { exact: true, dropNul: true })).toMatchObject({ ok: false });
});
it('MOVEVAL-014: the compared text comes from the SQLite value, so a value that changed on the way does not match', () => {
// What Postgres would print had the NUL been dropped, or the type changed, on the way.
expect(canonicalFromSource('team\u0000a', 'text', PLUGIN_RULES)).not.toBe(canonicalFromPostgres('teama', 'text'));
expect(canonicalFromSource('team\u0000a', 'text', { exact: true, dropNul: true })).not.toBe('teama');
expect(canonicalFromSource(new Uint8Array([0x61]), 'text', PLUGIN_RULES)).not.toBe(
canonicalFromPostgres('a', 'text'),
);
expect(canonicalFromSource(5n, 'bytes')).not.toBe(canonicalFromPostgres('\\x35', 'bytes'));
// The one change allowed is the one the operator asked for on TREK's own data.
expect(canonicalFromSource('team\u0000a', 'text', DROP_NUL)).toBe(canonicalFromPostgres('teama', 'text'));
expect(canonicalFromSource('{"k\\u0000":1}', 'json', DROP_NUL)).toBe(canonicalFromPostgres('{"k": 1}', 'json'));
});
});