fix(backend): reconcile scores table column names with ensure-core-tables migration (#1064) - #1681
Merged
blurbeast merged 3 commits intoAug 30, 2026
Conversation
solaawojobi00-bit
force-pushed
the
fix/issue-1064-scores-schema-mismatch
branch
from
August 30, 2026 17:41
d15ae01 to
4e675db
Compare
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: Reconcile Scores Table Column Names with Core Migrations
Problem
Migration
1789000000000_ensure-core-tables.jsaligns core tables and renamesscores.user_idtoborrowerandscores.current_scoretoscore. However, live queries across services (scoresService,scoreDecayService,scoreReconciliationService), controllers (scoreController,simulationController), and seed scripts continued to queryuser_idandcurrent_score. On any migrated database, scoring operations failed on both reads and writes withcolumn user_id does not existorcolumn current_score does not exist.Scenarios
GET /api/score/:userId)column current_score does not existorcolumn user_id does not existscoreWHEREborrower = $1, falling back gracefully to 500 if unrecordedPOST /api/score/update)INSERT INTO scores (user_id, current_score)scores (borrower, score)withON CONFLICT (borrower)and returns updated scoregetInactiveBorrowers,applyScoreDecay)s.user_idands.current_scores.borrowerands.scorecorrectlyupdateUserScoresBulk,setAbsoluteUserScoresBulk)scores (borrower, score)andON CONFLICT (borrower)npm run seed:dev)scores (borrower, score, created_at)with conflict handling onborrowerSolution
scorestable column names to canonical schema:id,borrower,score,created_at,updated_at.borrowerandscore.score ?? current_score) in controllers and reconciliation service to maintain compatibility during transitions.1789000000000_ensure-core-tables.jsincludescreated_aton table creation and addscreated_atif missing in the existing table branch.migration.test.tsexercisingupdateUserScoresBulkagainst the post-migration table schema.Changes
backend/migrations/1789000000000_ensure-core-tables.jscreated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMPis specified inCREATE TABLE scoresand added viaALTER TABLEif missing in theELSEbranch for idempotent migration.backend/src/services/scoresService.tsupdateUserScoresBulktoINSERT INTO scores (borrower, score)withON CONFLICT (borrower) DO UPDATE SET score = ....setAbsoluteUserScoresBulkCTE and insert statement to useborrowerandscore.backend/src/controllers/scoreController.tsgetScoreandupdateScorequeries to targetscoreandborrower.getScoreBreakdownCTE query to selectCOALESCE(score, 500) AS current_score FROM scores WHERE borrower = $1.backend/src/controllers/simulationController.tsgetRemittanceHistoryandsimulatePaymentto selectscore FROM scores WHERE borrower = $1.backend/src/services/scoreDecayService.tsgetInactiveBorrowerstoSELECT s.borrower AS borrower, s.score AS score ... FROM scores sandapplyScoreDecaytoUPDATE scores SET score = $1 ... WHERE borrower = $2.backend/src/services/scoreReconciliationService.tsfetchActiveBorrowerScorestoSELECT DISTINCT a.address, s.score FROM active_loans a LEFT JOIN scores s ON s.borrower = a.address.backend/src/services/eventIndexer.ts_updateUserScoretoborrower, score.backend/src/seed/index.ts&backend/src/seed/data/users.tsseedScoresto insert intoscores (borrower, score, created_at)withON CONFLICT (borrower).SeedUserinterface and seed records to defineborrowerandscore.Tests
backend/src/__tests__/migration.test.ts: Added test assertingupdateUserScoresBulkoperates against the post-migrationscorestable.backend/src/__tests__/scoresService.test.ts: Updated test table schema and test queries to useborrowerandscore.backend/src/services/__tests__/scoresService.test.ts: Updated assertion checking forON CONFLICT (borrower).backend/src/services/__tests__/scoreDecayService.test.ts: Updated SQL string assertions fors.borrower,s.score, andWHERE borrower = $2.backend/src/services/__tests__/scoreReconciliationService.test.ts,backend/src/__tests__/scoreReconciliationService.test.ts,backend/src/__tests__/simulationController.test.ts,backend/src/__tests__/score.test.ts: Updated mock DB records to providescore: ....Regression Tests (Acceptance Criteria Mapping)
scoresbackend/src; all targetborrowerandscorescoresService,scoreController, andsimulationControllerqueries run against migrated schemascoresService,scoreController, andsimulationControllerupdateUserScoresBulkagainst post-migration schemashould exercise updateUserScoresBulk against post-migration scores tableinmigration.test.tsseedScoresinseed/index.tsandseedUsersinseed/data/users.tsupdated and type-checkednpm run typecheck,npm run build,npm run lint,npm run format:check, andnpm testall greenTesting (Literal Output)
Test Suites
TypeScript Typecheck & Build
ESLint & Prettier
Notes for Reviewers
created_atcolumn.Closes #1064