fix(db): sort the Postgres move's names with an explicit code-unit compare

Five sorts in the Postgres bootstrap and the move from SQLite relied on the
default sort, which SonarCloud reports as a bug because it compares by code
unit implicitly. They order migration names, table names and JSON keys for a
checksum, where locale-aware ordering would change the result, so each now
names the code-unit compare the default already used. Nothing changes.
This commit is contained in:
Maurice
2026-11-18 23:58:20 +01:00
parent daa4e1dfcf
commit 7ae4dcba51
4 changed files with 5 additions and 5 deletions
@@ -135,7 +135,7 @@ async function createFromBaseline(
* that migration, when there is one.
*/
async function refuseNewerHistory(executed: string[], deps: PostgresBootstrapDeps): Promise<void> {
const thisRelease = migrationLabel([...deps.known].sort().at(-1) ?? '');
const thisRelease = migrationLabel([...deps.known].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)).at(-1) ?? '');
refuseNewerDatabase(executed, deps.known, await deps.safetyNet.restoreHint(thisRelease));
}
@@ -216,7 +216,7 @@ export async function bootstrapPostgresSchema(orm: AnyOrm, deps: PostgresBootstr
if (pending.length === 0) return;
// Only a database with history has a last migration to label its copy by; a
// database the baseline just created has nothing yet to lose.
const latest = [...executed].sort().at(-1);
const latest = [...executed].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)).at(-1);
if (latest !== undefined) await deps.safetyNet.beforeMigrate(migrationLabel(latest));
logger.log(`Applying ${pending.length} pending migration(s)`);
await migrator.up();
@@ -166,7 +166,7 @@ export function migrationLevelProblem(recorded: readonly string[] | null, known:
if (unknown.length > 0) {
return (
`a newer TREK migrated the SQLite file (${unknown.length} migration(s) this version does not know, ` +
`the last ${[...unknown].sort().at(-1)}). Run the move with the TREK version that last ran on it.`
`the last ${[...unknown].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)).at(-1)}). Run the move with the TREK version that last ran on it.`
);
}
const have = new Set(recorded);
@@ -36,7 +36,7 @@ function defer(plan: CopyPlan, fk: ForeignKey): void {
export function planCopyOrder(tables: readonly string[], foreignKeys: readonly ForeignKey[]): CopyPlan {
const plan: CopyPlan = { order: [], deferred: new Map() };
const known = new Set(tables);
const pending = new Set([...tables].sort());
const pending = new Set([...tables].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)));
const keys = foreignKeys.filter((fk) => known.has(fk.table) && known.has(fk.references));
for (const fk of keys) if (fk.table === fk.references) defer(plan, fk);
const between = keys.filter((fk) => fk.table !== fk.references);
@@ -332,7 +332,7 @@ export function stableJson(text: string): string {
if (value !== null && typeof value === 'object') {
return Object.fromEntries(
Object.keys(value)
.sort()
.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))
.map((key) => [key, sort((value as Record<string, unknown>)[key])]),
);
}