refactor(server): dedupe shard-state shaping, upsert builder, and config DB models #89
Reference in New Issue
Block a user
No description provided.
Delete Branch "refactor/dedupe-shardstate-config-db"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What & why
Addresses the SonarQube copy-paste findings that reflect real duplication. The bulk of Sonar's duplication flags on this repo are either intentional cross-package copies (
logger.js/db.js/requireInternalKey.js/server.jsacrossserver↔bot, documented in-code as deliberate because the bot deploys independently) or the intentional admin/player mirror surfaces — those are left as-is. This PR fixes only the three clusters that are genuine duplication within a single deployable:shardState.model.js— the one finding that was a latent defect:listOnline()re-inlined the exact field mapping thatshapeOnline()already provides (and thatlistOnlineLinked()uses), so the two could silently drift when a column is added. CollapsedlistOnline()ontoshapeOnline.shardState.db.js— extracted a singleupsertRow(table, pkCol, pk, fields, { coalesce })builder for the five near-identicalINSERT … ON DUPLICATE KEY UPDATEbodies (shard_online/shard_houses/shard_champs/shard_guilds/shard_governors).shard_onlinekeeps its COALESCE-on-NULL refresh semantics via thecoalesceflag; every other table keepsVALUES()(incoming wins). This is theshardState.db.js93↔136 finding.botConfig/emailConfig/uoLinkConfig.db.js— these three were byte-identical apart from table name + column list. Now generated from a sharedsingletonConfigDb(table, cols)factory (newserver/src/model/singletonConfigDb.js).Behavior is unchanged — this is a pure refactor.
How it was tested
cd server && DB_HOST=127.0.0.1 DB_PORT=59999 npm test→ 381/381 passing (covers the touched paths viashardState.model.test.js,publicShardOnline.test.js,shardIngest.*.test.js, andemailConfig.model.test.js).Checklist
AI-assisted contributions (required)
Claude Code (claude-opus-4-8). I have reviewed and understand every change, and take responsibility for it. AI-authored commits are marked with aCo-Authored-Bytrailer.License
🤖 Generated with Claude Code
Address the SonarQube copy-paste findings that reflect real duplication (as opposed to the intentional cross-package / admin-player mirror copies, which are by-design and left as-is): - shardState.model.js: listOnline() re-inlined the exact field mapping that shapeOnline() already provides (used by listOnlineLinked). Collapse it onto shapeOnline so the two can no longer drift. - shardState.db.js: extract a single upsertRow(table, pkCol, pk, fields, {coalesce}) builder for the five near-identical INSERT ... ON DUPLICATE KEY UPDATE bodies (online/houses/champs/guilds/governors). shard_online keeps its COALESCE-on-NULL semantics via the coalesce flag. - botConfig/emailConfig/uoLinkConfig .db.js: generate get()/upsert() from a shared singletonConfigDb(table, cols) factory instead of three byte-identical copies. Behavior unchanged; full server suite (381 tests) passes. Co-Authored-By: Claude <noreply@anthropic.com>