Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
V-FEXrt committed Jul 31, 2024
1 parent 7113eee commit 7635ffd
Showing 1 changed file with 47 additions and 48 deletions.
95 changes: 47 additions & 48 deletions rust/migration/src/m20240731_152842_create_job_size_proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,56 @@ impl MigrationTrait for Migration {
.get_connection()
.execute_unprepared(
"
CREATE OR REPLACE PROCEDURE calculate_job_size(
job_lim int,
INOUT updated_count int
)
language plpgsql
as $$
BEGIN
CREATE OR REPLACE PROCEDURE calculate_job_size(
job_lim int,
INOUT updated_count int
)
language plpgsql
as $$
BEGIN
-- Run the query that find the jobs, calcs their sizes, and then updates the table
WITH
eligible_jobs as (
SELECT id, stdout_blob_id, stderr_blob_id
FROM job
WHERE size IS NULL
ORDER BY created_at
ASC
LIMIT job_lim
),
job_blob_size as (
SELECT ej.id, SUM(COALESCE(b.size,0)) as size
FROM eligible_jobs ej
LEFT JOIN output_file o
ON ej.id = o.job_id
LEFT JOIN blob b
ON o.blob_id = b.id
GROUP BY ej.id
),
full_size as (
SELECT
ej.id,
CAST(jb.size + stdout.size + stderr.size as BIGINT) as size
FROM eligible_jobs ej
INNER JOIN job_blob_size jb
ON ej.id = jb.id
INNER JOIN blob stdout
ON ej.stdout_blob_id = stdout.id
INNER JOIN blob stderr
ON ej.stderr_blob_id = stderr.id
)
UPDATE job j
SET size = f.size
FROM full_size f
WHERE j.id = f.id;
-- Run the query that find the jobs, calcs their sizes, and then updates the table
WITH
eligible_jobs as (
SELECT id, stdout_blob_id, stderr_blob_id
FROM job
WHERE size IS NULL
ORDER BY created_at
ASC
LIMIT job_lim
),
job_blob_size as (
SELECT ej.id, SUM(COALESCE(b.size,0)) as size
FROM eligible_jobs ej
LEFT JOIN output_file o
ON ej.id = o.job_id
LEFT JOIN blob b
ON o.blob_id = b.id
GROUP BY ej.id
),
full_size as (
SELECT
ej.id,
CAST(jb.size + stdout.size + stderr.size as BIGINT) as size
FROM eligible_jobs ej
INNER JOIN job_blob_size jb
ON ej.id = jb.id
INNER JOIN blob stdout
ON ej.stdout_blob_id = stdout.id
INNER JOIN blob stderr
ON ej.stderr_blob_id = stderr.id
)
UPDATE job j
SET size = f.size
FROM full_size f
WHERE j.id = f.id;
-- Grab the rows affected count
GET DIAGNOSTICS updated_count = ROW_COUNT;
-- Grab the rows affected count
GET DIAGNOSTICS updated_count = ROW_COUNT;
END;
$$;
",
END;
$$;
",
)
.await?;
Ok(())
Expand Down

0 comments on commit 7635ffd

Please sign in to comment.