Skip to content

Commit 4834bd9

Browse files
committed
embed size_used update CTE
it will have to change independent of the schema update anyway
1 parent 95696ef commit 4834bd9

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

nexus/db-queries/src/db/datastore/region.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,33 @@ impl DataStore {
315315
.await?;
316316

317317
// Update datasets to which the regions belonged.
318-
// XXX put this file somewhere else
319-
sql_query(include_str!(
320-
"../../../../../schema/crdb/crucible-agent-reservation-overhead/up04.sql"
321-
))
322-
.execute_async(&conn)
323-
.await?;
318+
sql_query(r#"
319+
WITH size_used_with_reservation AS (
320+
SELECT
321+
crucible_dataset.id AS crucible_dataset_id,
322+
SUM(
323+
CASE
324+
WHEN block_size IS NULL THEN 0
325+
ELSE
326+
CASE
327+
WHEN reservation_percent = '25' THEN
328+
(block_size * blocks_per_extent * extent_count) / 4 +
329+
(block_size * blocks_per_extent * extent_count)
330+
END
331+
END
332+
) AS reserved_size
333+
FROM crucible_dataset
334+
LEFT JOIN region ON crucible_dataset.id = region.dataset_id
335+
WHERE crucible_dataset.time_deleted IS NULL
336+
GROUP BY crucible_dataset.id
337+
)
338+
UPDATE crucible_dataset
339+
SET size_used = size_used_with_reservation.reserved_size
340+
FROM size_used_with_reservation
341+
WHERE crucible_dataset.id = size_used_with_reservation.crucible_dataset_id"#
342+
)
343+
.execute_async(&conn)
344+
.await?;
324345

325346
// Whenever a region is hard-deleted, validate invariants
326347
// for all volumes

0 commit comments

Comments
 (0)