weekend on call shifts

This commit is contained in:
2026-03-09 18:24:16 +08:00
parent aee4604fed
commit b8ec5dbf69
2 changed files with 174 additions and 33 deletions
@@ -0,0 +1,115 @@
-- Add weekend-specific on_call shift types for Saturday and Sunday
-- Saturday on_call: 5PM to 8AM (15 hours)
-- Sunday on_call: 5PM to 7AM (14 hours)
-- Update duty_schedules.shift_type.
-- If enum-backed, add new enum values.
-- If CHECK-backed, replace the existing CHECK constraint with expanded values.
DO $$
DECLARE
_is_enum boolean := false;
_con text;
BEGIN
SELECT (t.typtype = 'e') INTO _is_enum
FROM pg_attribute a
JOIN pg_type t ON t.oid = a.atttypid
WHERE a.attrelid = 'duty_schedules'::regclass
AND a.attname = 'shift_type'
AND NOT a.attisdropped;
IF _is_enum THEN
BEGIN
ALTER TYPE shift_type ADD VALUE IF NOT EXISTS 'on_call_saturday';
EXCEPTION WHEN undefined_object THEN
NULL;
END;
BEGIN
ALTER TYPE shift_type ADD VALUE IF NOT EXISTS 'on_call_sunday';
EXCEPTION WHEN undefined_object THEN
NULL;
END;
ELSE
FOR _con IN
SELECT con.conname
FROM pg_constraint con
JOIN pg_attribute att
ON att.attrelid = con.conrelid
AND att.attnum = ANY(con.conkey)
WHERE con.conrelid = 'duty_schedules'::regclass
AND con.contype = 'c'
AND att.attname = 'shift_type'
LOOP
EXECUTE format('ALTER TABLE duty_schedules DROP CONSTRAINT %I', _con);
END LOOP;
ALTER TABLE duty_schedules
ADD CONSTRAINT duty_schedules_shift_type_check
CHECK (
shift_type IN (
'normal',
'am',
'pm',
'on_call',
'weekend',
'overtime',
'on_call_saturday',
'on_call_sunday'
)
);
END IF;
END $$;
-- Replace attendance_logs shift_type CHECK constraint regardless of current name.
DO $$
DECLARE
_is_enum boolean := false;
_con text;
BEGIN
SELECT (t.typtype = 'e') INTO _is_enum
FROM pg_attribute a
JOIN pg_type t ON t.oid = a.atttypid
WHERE a.attrelid = 'attendance_logs'::regclass
AND a.attname = 'shift_type'
AND NOT a.attisdropped;
IF _is_enum THEN
BEGIN
ALTER TYPE shift_type ADD VALUE IF NOT EXISTS 'on_call_saturday';
EXCEPTION WHEN undefined_object THEN
NULL;
END;
BEGIN
ALTER TYPE shift_type ADD VALUE IF NOT EXISTS 'on_call_sunday';
EXCEPTION WHEN undefined_object THEN
NULL;
END;
ELSE
FOR _con IN
SELECT con.conname
FROM pg_constraint con
JOIN pg_attribute att
ON att.attrelid = con.conrelid
AND att.attnum = ANY(con.conkey)
WHERE con.conrelid = 'attendance_logs'::regclass
AND con.contype = 'c'
AND att.attname = 'shift_type'
LOOP
EXECUTE format('ALTER TABLE attendance_logs DROP CONSTRAINT %I', _con);
END LOOP;
ALTER TABLE attendance_logs
ADD CONSTRAINT attendance_logs_shift_type_check
CHECK (
shift_type IN (
'normal',
'am',
'pm',
'on_call',
'weekend',
'overtime',
'on_call_saturday',
'on_call_sunday'
)
);
END IF;
END $$;