Skip to content

identity: DB unique (university_id, student_code) for student profiles - #101

Merged
qonnow merged 5 commits into
mainfrom
feature/identity-student-code-unique-per-university
Nov 21, 2026
Merged

qonnow merged 5 commits into
mainfrom
feature/identity-student-code-unique-per-university

Conversation

@rinni1536

Copy link
Copy Markdown
Collaborator

Follow-up to PR #99, as the PM decided on 2026-11-22: keep the API-level duplicate check and add a DB unique constraint in a separate PR. This branch is based on main and does not depend on #99.

Duplicate check before the migration

I ran the check query (below) against the local Docker PostgreSQL. identity_db there has no tables or data: it was never migrated, so there are 0 duplicates and 0 student profiles without a university. I could not reach any staging or production database. Run the query on every real environment before deploying. The migration also guards itself. If any profile has users.university_id IS NULL, or any (university_id, student_code) pair is duplicated, it fails with RAISE EXCEPTION and the row count. It never fixes data on its own.

SELECT count(*) AS profiles,
       count(*) FILTER (WHERE u.university_id IS NULL) AS profiles_without_university
  FROM student_profiles_identity s JOIN users u ON u.id = s.user_id;

SELECT u.university_id, s.student_code, count(*) AS n, array_agg(s.user_id) AS user_ids
  FROM student_profiles_identity s JOIN users u ON u.id = s.user_id
 GROUP BY u.university_id, s.student_code
HAVING count(*) > 1;

Changes

  • A unique index can't span two tables, so student_profiles_identity gets a university_id uuid NOT NULL column (FK universities, ON DELETE RESTRICT). The migration backfills it from users.university_id before setting NOT NULL, then adds UNIQUE(university_id, student_code) (student_profiles_identity_university_id_student_code_key).
  • POST /advisor-links/students (createStudent) sets universityId on the profile. This is the only place in src that creates a student profile. The existing API check stays. A P2002 on this constraint now maps to 409 STATE_CONFLICT / STUDENT_CODE_ALREADY_USED, as in docs/api-contract.md. Before this change it would have been reported as EMAIL_ALREADY_REGISTERED.
  • A student's university never changes in the code today. Account deletion removes the profile row before it clears users.university_id, so the copied column can't drift, and a deleted student's code can be reused.
  • Docs: docs/data-schema.md and the ER diagram in docs/diagram.md.

Tests (Identity, real PostgreSQL in Docker)

  • New test in advisor-link.spec.ts:
    • The same code in a different university → 201.
    • A raw insert of a duplicate in the same university → P2002.
    • 6 concurrent creates with one code → one 201 and five 409 STUDENT_CODE_ALREADY_USED, with exactly 1 row left.
  • I also disabled the P2002 mapping on purpose to confirm the race really reaches the DB constraint. The test then failed (field: 'email'), which shows the backstop is what handles it.
  • Fixtures in the other specs now set universityId.
  • mocha: 77 passing, 3 runs in a row. tsc (src and test) and eslint pass.
  • Note: run the tests with Node 24. On Node 20.20 here, mocha can't load the ESM build of @nestjs/core 12 (ERR_MODULE_NOT_FOUND for fast-safe-stringify). That problem is not caused by this PR.

rinni1536 and others added 5 commits November 22, 2026 02:10
Per the PM decision on 2026-11-22, back the API-level duplicate check for
advisor-created students with a database constraint so concurrent
requests can't both insert the same student code in one university.

- student_profiles_identity gets university_id (copy of users.university_id,
  FK universities), backfilled in the migration then made NOT NULL, plus
  UNIQUE(university_id, student_code). The migration fails with a clear
  message (no auto-fix) if any profile has no university or duplicates exist.
- createStudent sets universityId on the profile; a P2002 on that
  constraint maps to 409 STATE_CONFLICT / STUDENT_CODE_ALREADY_USED.
  The existing API check stays.
- Tests: same code in another university is allowed, raw duplicate insert
  hits P2002, and 6 concurrent creates give one 201 and five 409s.
- docs: data-schema and ER diagram.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…y-rate-limit' into feature/identity-student-code-unique-per-university
@qonnow
qonnow merged commit 847a006 into main Nov 21, 2026
5 checks passed
@qonnow
qonnow deleted the feature/identity-student-code-unique-per-university branch November 21, 2026 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants