Skip to content

Commit 69b20b7

Browse files
committed
Change period to be a daterange in DB
1 parent 637121f commit 69b20b7

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

service/src/main/kotlin/fi/espoo/evaka/messaging/MessageQueries.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -807,9 +807,9 @@ personal_accounts AS (
807807
JOIN message_account acc ON acc.employee_id = acl.employee_id
808808
JOIN message_account_view acc_name ON acc_name.id = acc.id
809809
LEFT JOIN LATERAL (
810-
SELECT daterange(ooo.start_date, ooo.end_date, '[]') AS period
810+
SELECT ooo.period
811811
FROM out_of_office ooo
812-
WHERE ooo.employee_id = acc.employee_id AND ${bind(today)} BETWEEN ooo.start_date AND ooo.end_date
812+
WHERE ooo.employee_id = acc.employee_id AND period && DATERANGE(${bind(today)}, NULL)
813813
LIMIT 1
814814
) ooo ON TRUE
815815
WHERE active IS TRUE

service/src/main/kotlin/fi/espoo/evaka/outofoffice/OutOfOffice.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ fun Database.Read.getOutOfOfficePeriods(
2222
createQuery {
2323
sql(
2424
"""
25-
SELECT id, daterange(start_date, end_date, '[]') AS period
25+
SELECT id, period
2626
FROM out_of_office
2727
WHERE employee_id = ${bind(employeeId)}
28-
AND end_date >= ${bind(today)}
28+
AND period && DATERANGE(${bind(today)}, NULL)
2929
"""
3030
)
3131
}
@@ -41,8 +41,8 @@ fun Database.Transaction.upsertOutOfOfficePeriod(
4141
createUpdate {
4242
sql(
4343
"""
44-
INSERT INTO out_of_office (employee_id, start_date, end_date)
45-
VALUES (${bind(employeeId)}, ${bind(period.period.start)}, ${bind(period.period.end)})
44+
INSERT INTO out_of_office (employee_id, period)
45+
VALUES (${bind(employeeId)}, DATERANGE(${bind(period.period.start)}, ${bind(period.period.end)}, '[]'))
4646
"""
4747
)
4848
}
@@ -52,7 +52,7 @@ VALUES (${bind(employeeId)}, ${bind(period.period.start)}, ${bind(period.period.
5252
sql(
5353
"""
5454
UPDATE out_of_office
55-
SET start_date = ${bind(period.period.start)}, end_date = ${bind(period.period.end)}
55+
SET period = DATERANGE(${bind(period.period.start)}, ${bind(period.period.end)}, '[]')
5656
WHERE id = ${bind(period.id)}
5757
"""
5858
)
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
CREATE TABLE out_of_office
22
(
33
id UUID PRIMARY KEY DEFAULT ext.uuid_generate_v1mc(),
4-
employee_id UUID NOT NULL,
5-
start_date DATE NOT NULL,
6-
end_date DATE NOT NULL
4+
employee_id UUID NOT NULL,
5+
period DATERANGE NOT NULL
76
);
87

9-
CREATE INDEX idx_out_of_office_employee_id_end_date
10-
ON out_of_office (employee_id, end_date);
8+
CREATE INDEX idx_out_of_office_employee_id_period
9+
ON out_of_office USING gist (employee_id, period);
1110

1211
ALTER TABLE out_of_office
1312
ADD CONSTRAINT fk_out_of_office_employee_id FOREIGN KEY (employee_id) REFERENCES employee (id),
14-
ADD CONSTRAINT start_before_end CHECK ((start_date <= end_date));
13+
ADD CONSTRAINT period_not_null CHECK ((NOT (lower_inf(period) OR upper_inf(period))));

0 commit comments

Comments
 (0)