refactor(server): dedupe shard-state shaping, upsert builder, and config DB models #89

Merged
whitlocktech merged 1 commits from refactor/dedupe-shardstate-config-db into main 2026-07-21 17:49:33 +00:00
Member

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.js across serverbot, 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 that shapeOnline() already provides (and that listOnlineLinked() uses), so the two could silently drift when a column is added. Collapsed listOnline() onto shapeOnline.
  • shardState.db.js — extracted a single upsertRow(table, pkCol, pk, fields, { coalesce }) builder for the five near-identical INSERT … ON DUPLICATE KEY UPDATE bodies (shard_online / shard_houses / shard_champs / shard_guilds / shard_governors). shard_online keeps its COALESCE-on-NULL refresh semantics via the coalesce flag; every other table keeps VALUES() (incoming wins). This is the shardState.db.js 93↔136 finding.
  • botConfig / emailConfig / uoLinkConfig .db.js — these three were byte-identical apart from table name + column list. Now generated from a shared singletonConfigDb(table, cols) factory (new server/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 test381/381 passing (covers the touched paths via shardState.model.test.js, publicShardOnline.test.js, shardIngest.*.test.js, and emailConfig.model.test.js).

Checklist

  • I have read CONTRIBUTING.md.
  • The change builds and existing tests/checks pass locally.
  • I have added or updated tests/docs where it makes sense.
  • My commits are reasonably scoped with clear messages.

AI-assisted contributions (required)

  • AI tools were used. Tool(s): Claude Code (claude-opus-4-8). I have reviewed and understand every change, and take responsibility for it. AI-authored commits are marked with a Co-Authored-By trailer.

License

  • I agree that my contribution is licensed under this project's license (GNU GPL v3.0 or later), and I have the right to contribute it.

🤖 Generated with Claude Code

## 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.js` across `server`↔`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 that `shapeOnline()` already provides (and that `listOnlineLinked()` uses), so the two could silently drift when a column is added. Collapsed `listOnline()` onto `shapeOnline`. - **`shardState.db.js`** — extracted a single `upsertRow(table, pkCol, pk, fields, { coalesce })` builder for the five near-identical `INSERT … ON DUPLICATE KEY UPDATE` bodies (`shard_online` / `shard_houses` / `shard_champs` / `shard_guilds` / `shard_governors`). `shard_online` keeps its COALESCE-on-NULL refresh semantics via the `coalesce` flag; every other table keeps `VALUES()` (incoming wins). This is the `shardState.db.js` 93↔136 finding. - **`botConfig` / `emailConfig` / `uoLinkConfig` `.db.js`** — these three were byte-identical apart from table name + column list. Now generated from a shared `singletonConfigDb(table, cols)` factory (new `server/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 via `shardState.model.test.js`, `publicShardOnline.test.js`, `shardIngest.*.test.js`, and `emailConfig.model.test.js`). ## Checklist - [x] I have read [CONTRIBUTING.md](CONTRIBUTING.md). - [x] The change builds and existing tests/checks pass locally. - [x] I have added or updated tests/docs where it makes sense. <!-- pure refactor, behavior unchanged; existing tests already cover these paths --> - [x] My commits are reasonably scoped with clear messages. ## AI-assisted contributions (required) - [x] AI tools were used. Tool(s): `Claude Code (claude-opus-4-8)`. I have reviewed and understand every change, and take responsibility for it. AI-authored commits are marked with a `Co-Authored-By` trailer. ## License - [x] I agree that my contribution is licensed under this project's license (**GNU GPL v3.0 or later**), and I have the right to contribute it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
wtclaude added 1 commit 2026-07-21 17:38:19 +00:00
refactor(server): dedupe shard-state shaping, upsert builder, and config DB models
All checks were successful
PR Checks / bot-install (pull_request) Successful in 18s
PR Checks / client-build (pull_request) Successful in 26s
PR Checks / server-tests (pull_request) Successful in 11m4s
401db8f75c
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>
whitlocktech approved these changes 2026-07-21 17:39:30 +00:00
whitlocktech scheduled this pull request to auto merge when all checks succeed 2026-07-21 17:39:47 +00:00
whitlocktech merged commit 68126efc0b into main 2026-07-21 17:49:33 +00:00
whitlocktech deleted branch refactor/dedupe-shardstate-config-db 2026-07-21 17:49:34 +00:00
Sign in to join this conversation.
No description provided.