-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: storage used should not count failed uploads (#1430)
Co-authored-by: Adam Alton <adamalton@gmail.com>
- Loading branch information
Showing
5 changed files
with
178 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/db/postgres/migrations/016-user-used-storage-count-pinned-only.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
-- Due to the parameters differing from the old function definition, if we don't drop the old one then it's not replaced. | ||
DROP FUNCTION IF EXISTS user_used_storage; | ||
|
||
CREATE OR REPLACE FUNCTION user_used_storage(query_user_id BIGINT) | ||
RETURNS stored_bytes | ||
LANGUAGE plpgsql | ||
AS | ||
$$ | ||
DECLARE | ||
used_storage stored_bytes; | ||
uploaded BIGINT; | ||
psa_pinned BIGINT; | ||
total BIGINT; | ||
BEGIN | ||
uploaded := | ||
( | ||
SELECT COALESCE(( | ||
SELECT SUM(dag_size) | ||
FROM ( | ||
SELECT c.cid, | ||
c.dag_size | ||
FROM upload u | ||
JOIN content c ON c.cid = u.content_cid | ||
JOIN pin p ON p.content_cid = u.content_cid | ||
WHERE u.user_id = query_user_id::BIGINT | ||
AND u.deleted_at is null | ||
AND p.status = 'Pinned' | ||
GROUP BY c.cid, | ||
c.dag_size | ||
) AS uploaded_content), 0) | ||
); | ||
|
||
psa_pinned := | ||
( | ||
SELECT COALESCE(( | ||
SELECT SUM(dag_size) | ||
FROM ( | ||
SELECT psa_pr.content_cid, | ||
c.dag_size | ||
FROM psa_pin_request psa_pr | ||
JOIN content c ON c.cid = psa_pr.content_cid | ||
JOIN pin p ON p.content_cid = psa_pr.content_cid | ||
JOIN auth_key a ON a.id = psa_pr.auth_key_id | ||
WHERE a.user_id = query_user_id::BIGINT | ||
AND psa_pr.deleted_at is null | ||
AND p.status = 'Pinned' | ||
GROUP BY psa_pr.content_cid, | ||
c.dag_size | ||
) AS pinned_content), 0) | ||
); | ||
|
||
total := uploaded + psa_pinned; | ||
|
||
SELECT uploaded::TEXT, | ||
psa_pinned::TEXT, | ||
total::TEXT | ||
INTO used_storage; | ||
|
||
return used_storage; | ||
END | ||
$$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters