fix(students): scope status constraint lookup and add it without blocking

Check pg_constraint by conname + conrelid so a same-named CHECK on another table cannot skip creation, add the constraint as NOT VALID and validate separately to avoid a long ACCESS EXCLUSIVE lock at boot. Mark lib/students.js as partially extracted in the migration plan.
This commit is contained in:
dev
2026-10-23 09:19:44 +03:00
parent c2cc7db26d
commit 7a2f8893a6
3 changed files with 17 additions and 6 deletions
+9 -3
View File
@@ -8,9 +8,15 @@ function normalizeStudentStatus(value) {
async function ensureStudentsTable(pool) {
await pool.query(`ALTER TABLE students ADD COLUMN IF NOT EXISTS status VARCHAR(20) NOT NULL DEFAULT 'active'`);
const { rows } = await pool.query(`SELECT 1 FROM pg_constraint WHERE conname = 'students_status_check'`);
if (rows.length) return;
await pool.query(`ALTER TABLE students ADD CONSTRAINT students_status_check CHECK (status IN ('active','paused','left'))`);
const { rows } = await pool.query(
`SELECT 1 FROM pg_constraint WHERE conname = 'students_status_check' AND conrelid = 'students'::regclass`
);
if (!rows.length) {
await pool.query(
`ALTER TABLE students ADD CONSTRAINT students_status_check CHECK (status IN ('active','paused','left')) NOT VALID`
);
}
await pool.query(`ALTER TABLE students VALIDATE CONSTRAINT students_status_check`);
}
module.exports = { STUDENT_STATUSES, normalizeStudentStatus, ensureStudentsTable };