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:
+7
-2
@@ -18,8 +18,13 @@ ALTER TABLE students ADD COLUMN IF NOT EXISTS photo_path VARCHAR(255);
|
||||
ALTER TABLE students ADD COLUMN IF NOT EXISTS profile JSONB;
|
||||
ALTER TABLE students ADD COLUMN IF NOT EXISTS status VARCHAR(20) NOT NULL DEFAULT 'active';
|
||||
DO $$ BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'students_status_check') THEN
|
||||
ALTER TABLE students ADD CONSTRAINT students_status_check CHECK (status IN ('active','paused','left'));
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'students_status_check' AND conrelid = 'students'::regclass) THEN
|
||||
ALTER TABLE students ADD CONSTRAINT students_status_check CHECK (status IN ('active','paused','left')) NOT VALID;
|
||||
END IF;
|
||||
END $$;
|
||||
DO $$ BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'students_status_check' AND conrelid = 'students'::regclass AND NOT convalidated) THEN
|
||||
ALTER TABLE students VALIDATE CONSTRAINT students_status_check;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user