Skip to content

Commit 8cf4067

Browse files
committed
Add default value for schedule.timezone upgrades
The value must not be null, so we have to set a default when upgrading the schema. For that we look for the first `timeperiod_entry` and use it's `timezone` column. If no entry exists, the fallback is **'UTC'**.
1 parent dd8fa78 commit 8cf4067

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
ALTER TABLE schedule ADD COLUMN timezone text NOT NULL AFTER name;
1+
ALTER TABLE schedule ADD COLUMN timezone text AFTER name;
2+
UPDATE schedule SET timezone = (
3+
SELECT entry.timezone
4+
FROM timeperiod_entry entry
5+
INNER JOIN timeperiod ON timeperiod.id = entry.timeperiod_id
6+
INNER JOIN rotation ON rotation.id = timeperiod.owned_by_rotation_id
7+
WHERE rotation.schedule_id = schedule.id
8+
ORDER BY entry.id
9+
LIMIT 1
10+
);
11+
UPDATE schedule SET timezone = 'UTC' WHERE timezone IS NULL;
12+
ALTER TABLE schedule MODIFY COLUMN timezone text NOT NULL;
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
ALTER TABLE schedule ADD COLUMN timezone text NOT NULL;
1+
ALTER TABLE schedule ADD COLUMN timezone text;
2+
UPDATE schedule SET timezone = (
3+
SELECT entry.timezone
4+
FROM timeperiod_entry entry
5+
INNER JOIN timeperiod ON timeperiod.id = entry.timeperiod_id
6+
INNER JOIN rotation ON rotation.id = timeperiod.owned_by_rotation_id
7+
WHERE rotation.schedule_id = schedule.id
8+
ORDER BY entry.id
9+
LIMIT 1
10+
);
11+
UPDATE schedule SET timezone = 'UTC' WHERE timezone IS NULL;
12+
ALTER TABLE schedule ALTER COLUMN timezone SET NOT NULL;

0 commit comments

Comments
 (0)