127 lines
5.3 KiB
JavaScript
127 lines
5.3 KiB
JavaScript
"use strict";
|
|
|
|
// adpanel:storage-form-inventory — the READ-ONLY first step of the deferred
|
|
// Wave 4 "storage-form persistence" item (.campaign/backlog.md C2): size how
|
|
// many live servers rows are affected before anyone considers a backfill or
|
|
// a consumer switch. Writes NOTHING: a single SELECT over the servers index,
|
|
// with every comparison computed in-process by the REAL sanitizer
|
|
// (utils/server-name.js sanitizeNodeStorageName) so the counts are exact
|
|
// rather than a SQL-regex approximation (MySQL's Unicode-aware \w and
|
|
// missing leading-alnum strip make a pure-SQL count untrustworthy — see the
|
|
// Wave 3 report's decline note).
|
|
//
|
|
// Reported:
|
|
// 1. rows whose `name` differs from its node-storage form (a Wave 4
|
|
// backfill would write a different value than today's consumers read);
|
|
// 2. same for the legacy_id / bot aliases (the collision scan compares
|
|
// all three);
|
|
// 3. same-node groups where two DISTINCT rows' alias storage forms
|
|
// collide — the user-data-loss class scanNodeStorageCollisions refuses
|
|
// for NEW creates (services/server-create.js);
|
|
// 4. rows whose name fails today's token form outright (sanitizeServerToken
|
|
// empty) — created by the pre-consolidation paths (e.g. the nodes.js
|
|
// cap-100 sanitizer era) and unpurgeable by name-based flows.
|
|
//
|
|
// Usage: node scripts/storage-form-inventory.js [--json]
|
|
|
|
require("dotenv").config({
|
|
path: require("path").join(__dirname, "..", ".env"),
|
|
override: true,
|
|
});
|
|
|
|
const { sanitizeNodeStorageName, sanitizeServerToken } = require("../utils/server-name");
|
|
const db = require("../db");
|
|
|
|
async function main() {
|
|
// Read-only by construction: the only statement this script issues.
|
|
const rows = await db.query(
|
|
"SELECT id, name, display_name, legacy_id, bot, node_id, ip FROM servers"
|
|
);
|
|
|
|
const divergentNames = [];
|
|
const divergentAliases = [];
|
|
const invalidTokenNames = [];
|
|
// storage form -> [{ id, alias, nodeId }]
|
|
const byStorageForm = new Map();
|
|
|
|
for (const row of rows || []) {
|
|
const id = row.id;
|
|
const nodeId = String(row.node_id || "").trim();
|
|
const name = String(row.name || "");
|
|
const aliases = [
|
|
["name", name],
|
|
["legacy_id", String(row.legacy_id || "")],
|
|
["bot", String(row.bot || "")],
|
|
].filter(([, v]) => v);
|
|
|
|
if (!sanitizeServerToken(name)) invalidTokenNames.push({ id, name });
|
|
|
|
for (const [column, alias] of aliases) {
|
|
const storageForm = sanitizeNodeStorageName(alias);
|
|
if (storageForm !== alias) {
|
|
const entry = { id, column, alias, storageForm, nodeId };
|
|
if (column === "name") divergentNames.push(entry);
|
|
else divergentAliases.push(entry);
|
|
}
|
|
if (storageForm) {
|
|
if (!byStorageForm.has(storageForm)) byStorageForm.set(storageForm, []);
|
|
byStorageForm.get(storageForm).push({ id, column, alias, nodeId });
|
|
}
|
|
}
|
|
}
|
|
|
|
// Same-node storage-form collisions between DISTINCT rows: two rows whose
|
|
// aliases sanitize to one node directory. Within one row (name vs its own
|
|
// bot alias) is the legacy same-server aliasing, not a collision.
|
|
const collisions = [];
|
|
for (const [storageForm, owners] of byStorageForm) {
|
|
const distinctRows = [...new Set(owners.map((o) => o.id))];
|
|
if (distinctRows.length < 2) continue;
|
|
const byNode = new Map();
|
|
for (const owner of owners) {
|
|
const key = owner.nodeId || "(no node)";
|
|
if (!byNode.has(key)) byNode.set(key, []);
|
|
byNode.get(key).push(owner);
|
|
}
|
|
for (const [nodeId, group] of byNode) {
|
|
const rowIds = [...new Set(group.map((g) => g.id))];
|
|
if (rowIds.length < 2) continue;
|
|
collisions.push({ storageForm, nodeId, rows: group });
|
|
}
|
|
}
|
|
|
|
const summary = {
|
|
totalRows: (rows || []).length,
|
|
nameDivergesFromStorageForm: divergentNames.length,
|
|
legacyAliasDivergesFromStorageForm: divergentAliases.length,
|
|
invalidTokenNames: invalidTokenNames.length,
|
|
sameNodeStorageFormCollisions: collisions.length,
|
|
};
|
|
|
|
if (process.argv.includes("--json")) {
|
|
console.log(JSON.stringify({ summary, divergentNames, divergentAliases, invalidTokenNames, collisions }, null, 2));
|
|
} else {
|
|
console.log("Storage-form inventory (read-only)");
|
|
console.log("==================================");
|
|
console.log(`servers rows scanned: ${summary.totalRows}`);
|
|
console.log(`name != sanitizeNodeStorageName(name): ${summary.nameDivergesFromStorageForm}`);
|
|
console.log(`legacy_id/bot aliases diverging: ${summary.legacyAliasDivergesFromStorageForm}`);
|
|
console.log(`names failing today's token form: ${summary.invalidTokenNames}`);
|
|
console.log(`same-node storage-form collisions: ${summary.sameNodeStorageFormCollisions}`);
|
|
for (const c of collisions) {
|
|
console.log(` collision on "${c.storageForm}" (node ${c.nodeId}): rows ${c.rows.map((r) => `#${r.id}(${r.column}=${JSON.stringify(r.alias)})`).join(", ")}`);
|
|
}
|
|
for (const d of divergentNames.slice(0, 50)) {
|
|
console.log(` divergent name: #${d.id} ${JSON.stringify(d.alias)} -> ${JSON.stringify(d.storageForm)}`);
|
|
}
|
|
if (divergentNames.length > 50) console.log(` … and ${divergentNames.length - 50} more (use --json for the full dump)`);
|
|
}
|
|
|
|
await db.close();
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error("storage-form inventory failed:", err.message || err);
|
|
process.exit(1);
|
|
});
|