Fills homeShort/awayShort on events created before the automatic fill, from the same official FPA club list. Dry-run by default; --apply writes. Uses pg and DATABASE_URL so it needs no build. Claude-Session: https://claude.ai/code/session_01RAkmwWv6L5KmpX4h9JPD2i
69 lines
2.9 KiB
JavaScript
69 lines
2.9 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* One-off: fill homeShort/awayShort on existing events from the official FPA club list, for events created before
|
|
* the automatic fill (apps/api/src/sports/fpa-siglas.ts). Safe: only fills when a short code is missing; never
|
|
* overwrites one that is set, and a club not on the list is left alone.
|
|
*
|
|
* Run from apps/api where DATABASE_URL is available (dry-run first):
|
|
* node scripts/backfill-siglas.mjs # shows what it would change
|
|
* node scripts/backfill-siglas.mjs --apply # writes the changes
|
|
*
|
|
* Keep the list below in sync with src/sports/fpa-siglas.ts.
|
|
*/
|
|
import pg from "pg";
|
|
|
|
const CLUBS = [
|
|
["ABC", ["ABC de Braga", "ABC Braga"]],
|
|
["ADC", ["AD Carvalhos Sernox", "AD Carvalhos"]],
|
|
["AAAS", ["Águas Santas Milaneza", "Águas Santas", "AA Águas Santas"]],
|
|
["AAA", ["Avanca Bioria Bondalti", "Avanca"]],
|
|
["CFB", ["CF Os Belenenses", "Os Belenenses"]],
|
|
["FCP", ["FC Porto"]],
|
|
["GCST", ["GC Santo Tirso Retrotarget", "GC Santo Tirso"]],
|
|
["CSM", ["Marítimo Madeira Andebol SAD", "Marítimo Madeira"]],
|
|
["PAC", ["Póvoa AC Bodegão CCR Proteu", "Póvoa AC"]],
|
|
["SLB", ["SL Benfica"]],
|
|
["SCP", ["Sporting CP"]],
|
|
["VSC", ["Vitória SC"]],
|
|
["SPS", ["ADA São Pedro do Sul Multisac", "ADA São Pedro do Sul"]],
|
|
["ALA", ["Alavarium"]],
|
|
["CALE", ["CA Leça"]],
|
|
["CDF", ["CD Feirense Movit", "CD Feirense"]],
|
|
["GIL", ["CDE Gil Eanes"]],
|
|
["CJAG", ["CJ Almeida Garrett Gesti Douro UAE", "CJ Almeida Garrett"]],
|
|
["JUVE", ["Juve Lis"]],
|
|
["MAD", ["Madeira SAD"]],
|
|
];
|
|
const norm = (s) => s.normalize("NFD").replace(/\p{Diacritic}/gu, "").toLowerCase().replace(/[^a-z0-9]+/g, " ").trim();
|
|
const BY_NAME = new Map();
|
|
for (const [sigla, names] of CLUBS) for (const n of names) BY_NAME.set(norm(n), sigla);
|
|
const siglaFor = (name) => (name ? BY_NAME.get(norm(name)) : undefined);
|
|
|
|
const apply = process.argv.includes("--apply");
|
|
if (!process.env.DATABASE_URL) {
|
|
console.error("DATABASE_URL não definido. Corre a partir de apps/api (ou faz `source .env`).");
|
|
process.exit(1);
|
|
}
|
|
const client = new pg.Client({ connectionString: process.env.DATABASE_URL });
|
|
await client.connect();
|
|
try {
|
|
const { rows } = await client.query(
|
|
`SELECT id, home, away, home_short, away_short FROM events WHERE away <> '' AND (home_short IS NULL OR away_short IS NULL)`,
|
|
);
|
|
let n = 0;
|
|
for (const r of rows) {
|
|
const hs = r.home_short ?? siglaFor(r.home);
|
|
const as = r.away_short ?? siglaFor(r.away);
|
|
const changes = (!r.home_short && hs) || (!r.away_short && as);
|
|
if (!changes) continue;
|
|
n++;
|
|
console.log(`${r.home} x ${r.away} -> ${hs ?? r.home_short ?? "—"} x ${as ?? r.away_short ?? "—"}`);
|
|
if (apply) {
|
|
await client.query(`UPDATE events SET home_short = COALESCE(home_short, $1), away_short = COALESCE(away_short, $2) WHERE id = $3`, [hs ?? null, as ?? null, r.id]);
|
|
}
|
|
}
|
|
console.log(`\n${apply ? "Atualizados" : "A atualizar (dry-run)"}: ${n} evento(s).`);
|
|
} finally {
|
|
await client.end();
|
|
}
|