Repository navigation
chore(db): modernize Drizzle usage, fix query bugs, and drop redundant indexes - #223
Merged
Merged
Conversation
Signed-off-by: Theo Siemens-Rhodes <112789743+theosiemensrhodes@users.noreply.github.com>
theosiemensrhodes
marked this pull request as draft
November 23, 2026 22:49
…ed queries Signed-off-by: Theo Siemens-Rhodes <112789743+theosiemensrhodes@users.noreply.github.com>
…uery bugs Signed-off-by: Theo Siemens-Rhodes <112789743+theosiemensrhodes@users.noreply.github.com>
…db files Signed-off-by: Theo Siemens-Rhodes <112789743+theosiemensrhodes@users.noreply.github.com>
theosiemensrhodes
force-pushed
the
chore/drizzle-db-updates
branch
from
November 23, 2026 22:53
ec2454c to
e0c1078
Compare
…ry test coverage Signed-off-by: Theo Siemens-Rhodes <112789743+theosiemensrhodes@users.noreply.github.com>
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.
Summary
Modernizes Drizzle ORM usage for
drizzle-orm@0.45.4/drizzle-kit@0.31.11, fixes DB/query bugs found during audit, and cleans up redundant indexes and schema relations.Changes
Fix
DrizzleQueryError.causehandling for unique violations (6fdc4369, hardened inc537839e)drizzle-orm@0.44+wraps driver errors inDrizzleQueryErrorwith the underlyingpostgreserror on.cause, duplicate-email checks inUserService(createUser,createInstructor,updateInstructor) stopped matching SQLSTATE23505.isUniqueViolationinsrc/server/db/errors.tswalking.cause(with cycle guard) and treating explicit SQLSTATEcodeas authoritative rather than matching"unique"substrings.Modernize Drizzle config, client, view helpers, and typed queries (
de654735, refined inc537839e)src/server/db/migration-config.tssodrizzle.config.tsusesdefineConfig(strict: true,verbose: true) without pullingdrizzle-kitinto runtime code; removed stale"drizzle.config.*"fromnext.config.jsoutputFileTracingIncludesand added--forcetopnpm test:db:push.drizzle(conn, { schema })todrizzle({ client: conn, schema })insrc/server/db/index.tsandsrc/test/test-db.ts.getViewColumns(ViewBaseConfigcast) with built-ingetViewSelectedFieldsfromdrizzle-orm..$onUpdate(() => sql\now()`)toupdatedAtcolumns on app-updated tables (course,user,notification_preference) so updates stay on Postgres's clock, and dropped the redundant manualupdatedAt: new Date()inPreferenceService.setPreference(leaving Better Auth-managed tables inauth.ts` untouched).sqlEXISTS/NOT EXISTSsubqueries inCoverageServiceandShiftServicewith typedexists(),notExists(), andaliasedTable().eslint-plugin-drizzledrizzleObjectNamerules to["db", "tx"]so transaction updates/deletes are guarded and the inert"ctx.db"entry is removed.Fix DB/query bugs & add query optimizations (
036da941, hardened inc537839e)TermService.createTerm: awaitedthis.insertHolidays(tx, termId, holidays)inside the transaction.migrate.ts: uses a dedicatedmax: 1postgresconnection closed infinallyand rethrows migration errors on startup instead of swallowing them. Deploy note:instrumentation.tsawaitsmigrate()at boot, so an unreachable DB or failing migration will now fail startup fast (causing container restart underrestart: unless-stopped) rather than serving against a stale schema..mapWith(Number)tocount(*) over()queries inUserService,VolunteerService, andCoverageService(Postgres returnsint8as string viapostgres-js), and replacedUserService.getVerificationRequestCountwith Drizzle'scount().ClassService.updateSchedules: match fetched schedules by ID via aMapinstead of array index afterWHERE id IN (...).searchUtils.ts: iteratecolumns.slice(1)so the first column (name) is not double-counted in similarity/search expressions. Behavior note: user search ranking/thresholding (gt(similarity, 0.4)) now weightsname,lastName, andemailequally instead of double-weightingname.inArray(...)queries inClassService,UserService,VolunteerService,TermService, andCoverageServiceto skip unnecessaryWHERE falseround-trips.Drop redundant indexes, refine relations, and remove dead DB files (
e0c10784, extended inc537839e)session_token_index,verification_identifier_index,idx_user_email,shift_schedule_id_index,volunteer_to_schedule_volunteer_user_id_index,instructor_to_schedule_instructor_user_id_index,idx_notification_pref_user) and generated idempotent migration0011_drop_redundant_indexes.sql(DROP INDEX IF EXISTS).relationNametocoverageRequestRelationsand completed missing reverse relations (account,user,volunteer,schedule,shift).src/server/db/cache.tsandsrc/server/services/entity/profileService.ts.Validation
pnpm exec dotenv -e .env.test -- drizzle-kit check— passed.SKIP_ENV_VALIDATION=1 npx next typegen && pnpm typecheck— passed (0 errors).vitest runon unit tests (db-errors,search-utils,availability-utils,schedule-fmt,service-unavailable,example) and integration suites (class-service,coverage-queries,job-service,preference-service,shift-service,term-service,user-service) — passed against Postgres with migration0011applied (includingcoverage-queries.test.tscoveringnotExists+aliasedTable("cr2")+mapWith(Number)+getViewSelectedFields,preference-service.test.tsverifyingupdated_atadvances on upsert, andshift-service.test.tsverifying instructorexists()).Limitations / Notes
src/test/integration/coverage-service.test.tsfails onmaindue to pre-existing hardcoded 2026 fixture dates; tracked separately asN13on the project board.eslintonmainfails to load due toFlatCompat+eslint-config-next@16.4.6(addressed separately on the CI branch).drizzle-orm@1.0.0-rc.6was intentionally skipped because it is pre-release and introduces breaking RQB v2 / internal schema changes incompatible withbetter-auth's Drizzle adapter.N05/PR fix(classes): idempotent class publication, unique shift occurrences, and single-date boundaries #217,N06/PR fix(schedule): enforce schedule ownership and published-occurrence immutability #219,N08/PR fix(startup): ensure eager worker readiness, fail-fast migrations, and correlated schedule recovery #214,N11/PR fix(pagination): enforce deterministic ordering for lists #211, plus newly filedN12forTermServicepast-term blackouts & holiday-only updates andN13forcoverage-service.test.tsdates).Council review
Rounds: 2
thought_level: high)thought_level: high)reasoningEffort: high)antigravity / claude-opus-5-5hit its individual quota during Round 2 (Resets in 4h39m41s) and was not substituted, so Round 2 has 2 of 3 requested members.c537839e(authoritative SQLSTATE checking inisUniqueViolation, dedicatedmax: 1migrator connection,verification_identifier_index+DROP INDEX IF EXISTSin migration0011,--forceontest:db:push, removing staledrizzle.config.*tracing and deadctx.dbESLint entry, and adding direct Postgres integration tests forCoverageService,PreferenceServiceupsertupdatedAt, and instructorShiftService). Out-of-scope pre-existing bugs were verified against existing project items (N05,N06,N08,N11) and newly tracked asN12andN13.9/10). Minor optional polish noted (outer-wrapper.codeshort-circuit inisUniqueViolation,timeout: 5onmigrationClient.end(), and additional negative/volunteer test cases) was left as-is to avoid unnecessary churn.