Skip to content

Add non-null constraint to zones.number#13992

Merged
reteps merged 2 commits intomasterfrom
reteps/zone-number-not-null
Feb 3, 2026
Merged

Add non-null constraint to zones.number#13992
reteps merged 2 commits intomasterfrom
reteps/zone-number-not-null

Conversation

@reteps
Copy link
Member

@reteps reteps commented Feb 1, 2026

Description

Add a database migration to make the number column in the zones table NOT NULL. This change has been verified to not affect production data, as no rows contain NULL values for this column. Noted in #13991.

Updates related type definitions to reflect the schema change:

  • Updated ZoneSchema in db-types.ts to remove .nullable() from the number field
  • Updated test fixtures to use a valid number instead of null
prairielearn=*> SELECT COUNT(*) FROM zones WHERE number IS NULL;
 count
-------
     0
(1 row)

in production

Testing

Build passes successfully with no type errors. Test fixtures updated to reflect the new constraint.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 1, 2026

All images
Image Platform Old Size New Size Change
prairielearn/executor:60d3e05ea73238616fb3ec50acac98451a13a31f linux/amd64 1266.69 MB 1266.68 MB 0.00%
prairielearn/executor:60d3e05ea73238616fb3ec50acac98451a13a31f linux/arm64 1234.16 MB 1234.17 MB 0.00%
prairielearn/prairielearn:60d3e05ea73238616fb3ec50acac98451a13a31f linux/amd64 1266.68 MB 1266.68 MB 0.00%
prairielearn/prairielearn:60d3e05ea73238616fb3ec50acac98451a13a31f linux/arm64 1234.16 MB 1234.16 MB 0.00%

No significant size changes

@reteps reteps marked this pull request as ready for review February 1, 2026 18:34
@reteps reteps requested a review from a team as a code owner February 1, 2026 18:34
@reteps reteps requested review from nwalters512 and removed request for a team February 1, 2026 18:34
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 1, 2026

📝 Walkthrough

Walkthrough

This PR enforces a NOT NULL constraint on the zones.number column by updating the database schema definition, type definitions, migrations, and test data. The changes span schema files, two-stage database migrations, and corresponding test updates.

Changes

Cohort / File(s) Summary
Schema and Type Definitions
database/tables/zones.pg, apps/prairielearn/src/lib/db-types.ts
Modified zones.number column from nullable to NOT NULL at both the database schema and Zod validation layer.
Database Migrations
apps/prairielearn/src/migrations/20260201181253_zones__number__nonnull_constraint_add.sql, apps/prairielearn/src/migrations/20260201181254_zones__number__nonnull_constraint_validate.sql
Added two-stage migration: first creates a NOT VALID CHECK constraint, then validates it, sets the column to NOT NULL, and drops the temporary constraint.
Test Updates
apps/prairielearn/src/lib/client/safe-db-types.test.ts
Updated test data in MinimalStaffZone object to set number field from null to 1, reflecting the non-nullable constraint requirement.
Documentation
AGENTS.md
Added Git guidance section documenting commit and rebase policies.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: adding a non-null constraint to the zones.number column, which matches the primary objective of the pull request.
Description check ✅ Passed The description directly relates to the changeset, explaining the database migration, verifying production data safety, and documenting related type definition updates.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@reteps reteps changed the title Make zone number non-null Add non-null constraint to zones.number Feb 1, 2026
@reteps reteps added the low-priority This doesn't need to be merged, reviewed or done with any priority label Feb 1, 2026
Copy link
Contributor

@nwalters512 nwalters512 Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other recent migrations, e.g. the pair of 20260115220136_courses__branch__nonnull_constraint_add.sql and 20260115220137_courses__branch__nonnull_constraint_validate.sql, we first added an invalid constraint, then validated it and added the NOT NULL constraint (which Postgres could immediately validate because of the existence of the other constraint). Can you say why you didn't take a similar approach here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put up sbdchd/squawk#910 so we can potentially enable this in the future

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this was resolved in ff79009?

(Do we need to add "NEVER amend and force push unless specifically requested" to AGENTS.md?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i'll add that to this PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reteps
Copy link
Member Author

reteps commented Feb 3, 2026

Once we get to postgres 18 we can do this: https://neon.com/postgresql/postgresql-18/not-null-as-not-valid

Add migration to set NOT NULL constraint on zones.number column.
Update related type definitions to reflect the schema change.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
@reteps reteps force-pushed the reteps/zone-number-not-null branch from f97a4b8 to ff79009 Compare February 3, 2026 15:39
@reteps reteps requested a review from nwalters512 February 3, 2026 15:39
@reteps reteps enabled auto-merge February 3, 2026 16:29
@reteps reteps added this pull request to the merge queue Feb 3, 2026
Merged via the queue into master with commit 0f43a3e Feb 3, 2026
28 checks passed
@reteps reteps deleted the reteps/zone-number-not-null branch February 3, 2026 17:02
reteps added a commit that referenced this pull request Feb 4, 2026
Add NOT NULL constraints to the `number` column in: alternative_groups,
assessment_instances, assessment_modules, course_instance_access_rules,
job_sequences, questions, tags, and topics. This follows the pattern from
PR #13992 (zones table) with a two-phase migration approach: add CHECK
constraints with NOT VALID, then validate + set NOT NULL + drop CHECK.

Two tables required backfills: topics (37 NULLs) and assessment_instances
(1 NULL). These use batched migrations to safely handle large datasets.
The remaining 6 tables have no NULL values, so their constraints are added
and validated directly.

This ensures the database accurately reflects the application's type
constraints via Zod schemas.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

low-priority This doesn't need to be merged, reviewed or done with any priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants