refactor(config): dedupe bindings migrations

This commit is contained in:
Peter Steinberger
2026-02-15 16:47:06 +00:00
parent fe303fc016
commit 08f16da8d7

View File

@@ -6,80 +6,85 @@ import {
mergeMissing, mergeMissing,
} from "./legacy.shared.js"; } from "./legacy.shared.js";
function migrateBindings(
raw: Record<string, unknown>,
changes: string[],
changeNote: string,
mutator: (match: Record<string, unknown>) => boolean,
) {
const bindings = Array.isArray(raw.bindings) ? raw.bindings : null;
if (!bindings) {
return;
}
let touched = false;
for (const entry of bindings) {
if (!isRecord(entry)) {
continue;
}
const match = getRecord(entry.match);
if (!match) {
continue;
}
if (!mutator(match)) {
continue;
}
entry.match = match;
touched = true;
}
if (touched) {
raw.bindings = bindings;
changes.push(changeNote);
}
}
export const LEGACY_CONFIG_MIGRATIONS_PART_1: LegacyConfigMigration[] = [ export const LEGACY_CONFIG_MIGRATIONS_PART_1: LegacyConfigMigration[] = [
{ {
id: "bindings.match.provider->bindings.match.channel", id: "bindings.match.provider->bindings.match.channel",
describe: "Move bindings[].match.provider to bindings[].match.channel", describe: "Move bindings[].match.provider to bindings[].match.channel",
apply: (raw, changes) => { apply: (raw, changes) => {
const bindings = Array.isArray(raw.bindings) ? raw.bindings : null; migrateBindings(
if (!bindings) { raw,
return; changes,
} "Moved bindings[].match.provider → bindings[].match.channel.",
(match) => {
let touched = false; if (typeof match.channel === "string" && match.channel.trim()) {
for (const entry of bindings) { return false;
if (!isRecord(entry)) { }
continue; const provider = typeof match.provider === "string" ? match.provider.trim() : "";
} if (!provider) {
const match = getRecord(entry.match); return false;
if (!match) { }
continue; match.channel = provider;
} delete match.provider;
if (typeof match.channel === "string" && match.channel.trim()) { return true;
continue; },
} );
const provider = typeof match.provider === "string" ? match.provider.trim() : "";
if (!provider) {
continue;
}
match.channel = provider;
delete match.provider;
entry.match = match;
touched = true;
}
if (touched) {
raw.bindings = bindings;
changes.push("Moved bindings[].match.provider → bindings[].match.channel.");
}
}, },
}, },
{ {
id: "bindings.match.accountID->bindings.match.accountId", id: "bindings.match.accountID->bindings.match.accountId",
describe: "Move bindings[].match.accountID to bindings[].match.accountId", describe: "Move bindings[].match.accountID to bindings[].match.accountId",
apply: (raw, changes) => { apply: (raw, changes) => {
const bindings = Array.isArray(raw.bindings) ? raw.bindings : null; migrateBindings(
if (!bindings) { raw,
return; changes,
} "Moved bindings[].match.accountID → bindings[].match.accountId.",
(match) => {
let touched = false; if (match.accountId !== undefined) {
for (const entry of bindings) { return false;
if (!isRecord(entry)) { }
continue; const accountID =
} typeof match.accountID === "string" ? match.accountID.trim() : match.accountID;
const match = getRecord(entry.match); if (!accountID) {
if (!match) { return false;
continue; }
} match.accountId = accountID;
if (match.accountId !== undefined) { delete match.accountID;
continue; return true;
} },
const accountID = );
typeof match.accountID === "string" ? match.accountID.trim() : match.accountID;
if (!accountID) {
continue;
}
match.accountId = accountID;
delete match.accountID;
entry.match = match;
touched = true;
}
if (touched) {
raw.bindings = bindings;
changes.push("Moved bindings[].match.accountID → bindings[].match.accountId.");
}
}, },
}, },
{ {