mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +00:00
bc6728678b
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.
113 lines
4.4 KiB
TypeScript
113 lines
4.4 KiB
TypeScript
import { readEnv, type AppEnv } from '../../../../app-config';
|
|
import { resolveDataPaths } from '../../../../app-config/data-paths';
|
|
import { knownMigrationNames } from '../../../known-migrations';
|
|
import type { AnyOrm } from '../../../orm';
|
|
import { buildOrmConfig, resolveEngine } from '../../index';
|
|
import { redactUrl } from '../../redact-url';
|
|
import { readPostgresBaseline } from '../baseline';
|
|
import { MoveRefusal, moveSqliteToPostgres, renderSummary, type MoveOptions } from './move';
|
|
import { MikroORM } from '@mikro-orm/core';
|
|
|
|
import path from 'node:path';
|
|
|
|
/**
|
|
* The command around the move (`src/move-to-postgres.ts` runs it): reads
|
|
* TREK_DB_URL as the server does, takes the SQLite file from the data
|
|
* directory or `--from=<file>`, the plugin data from the plugins' data
|
|
* directory, runs the move and prints its summary. `--drop-nul` lets the move
|
|
* drop NUL characters from TREK's own text instead of refusing them. Answers
|
|
* the exit code.
|
|
*/
|
|
|
|
export const USAGE =
|
|
'Usage: move-to-postgres [--from=<SQLite file>] [--drop-nul]\n' +
|
|
'Copies a TREK SQLite database (data/travel.db by default) into the empty Postgres database TREK_DB_URL names.\n' +
|
|
' --from=<file> the SQLite file to copy\n' +
|
|
" --drop-nul drop NUL characters (U+0000) from TREK's own text, which Postgres cannot hold and TREK drops on\n" +
|
|
' its next write anyway, instead of refusing the move; the summary lists every value changed.\n' +
|
|
" A plugin's own data is never changed: a NUL there refuses the move either way.";
|
|
|
|
export interface MoveCommandIo {
|
|
env: () => AppEnv;
|
|
out: (line: string) => void;
|
|
err: (line: string) => void;
|
|
/** An ORM on the target database, from the Postgres engine's own config. */
|
|
openOrm: (env: AppEnv) => Promise<AnyOrm>;
|
|
move: (options: MoveOptions) => ReturnType<typeof moveSqliteToPostgres>;
|
|
}
|
|
|
|
/** The ORM the move runs on: the engine's config, without entity discovery (the move runs SQL only). */
|
|
async function openTargetOrm(env: AppEnv): Promise<AnyOrm> {
|
|
const config = buildOrmConfig(resolveEngine(env.db));
|
|
return MikroORM.init({
|
|
...config,
|
|
entities: [],
|
|
entitiesTs: [],
|
|
discovery: { warnWhenNoEntities: false },
|
|
migrations: { ...config.migrations, silent: true },
|
|
});
|
|
}
|
|
|
|
const defaultIo: MoveCommandIo = {
|
|
env: readEnv,
|
|
out: (line) => process.stdout.write(`${line}\n`),
|
|
err: (line) => process.stderr.write(`${line}\n`),
|
|
openOrm: openTargetOrm,
|
|
move: moveSqliteToPostgres,
|
|
};
|
|
|
|
const FROM = '--from=';
|
|
const DROP_NUL = '--drop-nul';
|
|
|
|
async function run(argv: readonly string[], io: MoveCommandIo): Promise<number> {
|
|
if (argv.includes('--help') || argv.includes('-h')) {
|
|
io.out(USAGE);
|
|
return 0;
|
|
}
|
|
const unknown = argv.find((arg) => !arg.startsWith(FROM) && arg !== DROP_NUL);
|
|
if (unknown !== undefined) throw new MoveRefusal(`Unknown argument ${unknown}.\n${USAGE}`);
|
|
const env = io.env();
|
|
if (env.db.engine !== 'postgres')
|
|
throw new MoveRefusal('Set TREK_DB_URL to the empty Postgres database to move into.');
|
|
|
|
const paths = resolveDataPaths(env);
|
|
const from = argv.find((arg) => arg.startsWith(FROM))?.slice(FROM.length);
|
|
const sqliteFile = path.resolve(from || paths.dbFile);
|
|
const target = redactUrl(env.db.url);
|
|
io.out(`Moving ${sqliteFile} into ${target}`);
|
|
const orm = await io.openOrm(env);
|
|
try {
|
|
const summary = await io.move({
|
|
sqliteFile,
|
|
pluginsDataDir: env.plugins.dataDir || path.join(paths.dataDir, 'plugins-data'),
|
|
orm,
|
|
known: knownMigrationNames(),
|
|
baseline: readPostgresBaseline,
|
|
log: (line) => io.out(` ${line}`),
|
|
dropNul: argv.includes(DROP_NUL),
|
|
});
|
|
io.out('');
|
|
io.out(renderSummary(summary, target));
|
|
return 0;
|
|
} finally {
|
|
await orm.close(true);
|
|
}
|
|
}
|
|
|
|
/** Runs the command and answers its exit code: 0 once the move committed, 1 for a refusal or a failure. */
|
|
export async function runMoveCommand(argv: readonly string[], io: Partial<MoveCommandIo> = {}): Promise<number> {
|
|
const resolved = { ...defaultIo, ...io };
|
|
try {
|
|
return await run(argv, resolved);
|
|
} catch (err) {
|
|
if (err instanceof MoveRefusal) resolved.err(`\nThe move was refused: ${err.message}`);
|
|
else {
|
|
const text = err instanceof Error ? (err.stack ?? err.message) : String(err);
|
|
resolved.err(
|
|
`\nThe move failed; the Postgres database is left as the move found it unless this says otherwise: ${text}`,
|
|
);
|
|
}
|
|
return 1;
|
|
}
|
|
}
|