fix: parquet_file billing trigger respects to_delete

pull/24376/head
Luke Bond 2022-09-06 16:33:24 +01:00
parent 80661a5d1c
commit feae712881
2 changed files with 12 additions and 12 deletions

View File

@ -30,21 +30,23 @@ CREATE TRIGGER update_billing
FOR EACH ROW
EXECUTE PROCEDURE increment_billing_summary();
CREATE OR REPLACE FUNCTION decrement_billing_summary()
CREATE OR REPLACE FUNCTION maybe_decrement_billing_summary()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS
$$
BEGIN
UPDATE billing_summary
SET total_file_size_bytes = billing_summary.total_file_size_bytes - OLD.file_size_bytes
WHERE billing_summary.namespace_id = OLD.namespace_id;
IF OLD.to_delete IS NULL AND NEW.to_delete IS NOT NULL THEN
UPDATE billing_summary
SET total_file_size_bytes = billing_summary.total_file_size_bytes - OLD.file_size_bytes
WHERE billing_summary.namespace_id = OLD.namespace_id;
END IF;
RETURN OLD;
END;
$$ ;
CREATE TRIGGER decrement_summary
AFTER DELETE
AFTER UPDATE
ON parquet_file
FOR EACH ROW
EXECUTE PROCEDURE decrement_billing_summary();
EXECUTE PROCEDURE maybe_decrement_billing_summary();

View File

@ -2896,10 +2896,7 @@ mod tests {
.expect("fetch total file size failed");
assert_eq!(total_file_size_bytes, 1337 * 3);
// flag f1 for deletion and assert that the total file size hasn't changed.
// i don't yet know whether we're going to charge for "flagged to delete" files,
// but i want to expressly assert the current behaviour so we have a test harness for
// changing that behaviour. the current implementation will charge for to-delete files.
// flag f1 for deletion and assert that the total file size is reduced accordingly.
postgres
.repositories()
.await
@ -2912,8 +2909,10 @@ mod tests {
.fetch_one(&pool)
.await
.expect("fetch total file size failed");
assert_eq!(total_file_size_bytes, 1337 * 3);
// we marked the first file of size 1337 for deletion leaving only the second that was 2x that
assert_eq!(total_file_size_bytes, 1337 * 2);
// actually deleting shouldn't change the total
let now = Timestamp::new((time_provider.now()).timestamp_nanos());
postgres
.repositories()
@ -2927,7 +2926,6 @@ mod tests {
.fetch_one(&pool)
.await
.expect("fetch total file size failed");
// we deleted the first file of size 1337 leaving only the second that was 2x that
assert_eq!(total_file_size_bytes, 1337 * 2);
}
}