Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MODFISTO-511] - Fix sql migration by using json_build_object #434

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,18 @@
WITH credit_sums AS (
SerhiiNosko marked this conversation as resolved.
Show resolved Hide resolved
SELECT
budget.id AS budget_id,
SUM((trx.jsonb->>'amount')::numeric) AS total_credits
COALESCE(SUM((trx.jsonb->>'amount')::numeric), 0) AS total_credits
FROM ${myuniversity}_${mymodule}.budget AS budget
JOIN ${myuniversity}_${mymodule}.transaction AS trx
LEFT JOIN ${myuniversity}_${mymodule}.transaction AS trx
ON trx.fiscalyearid = budget.fiscalyearid AND trx.tofundid = budget.fundid
WHERE trx.jsonb->>'transactionType' = 'Credit'
AND trx.jsonb->>'transactionType' = 'Credit'
GROUP BY budget.id
)
UPDATE ${myuniversity}_${mymodule}.budget AS budget
SET
jsonb = jsonb_set(
jsonb_set(
budget.jsonb,
'{credits}',
to_jsonb((budget.jsonb->>'credits')::numeric + credit_sums.total_credits)
),
'{expenditures}',
to_jsonb((budget.jsonb->>'expenditures')::numeric - credit_sums.total_credits)
jsonb = jsonb || jsonb_build_object(
SerhiiNosko marked this conversation as resolved.
Show resolved Hide resolved
'credits', to_jsonb(COALESCE((budget.jsonb->>'credits')::numeric, 0) + credit_sums.total_credits),
'expenditures', to_jsonb(COALESCE((budget.jsonb->>'expenditures')::numeric, 0) - credit_sums.total_credits)
)
FROM credit_sums
WHERE budget.id = credit_sums.budget_id;
Expand All @@ -45,14 +40,11 @@ WITH aggregated_amounts AS (
)
UPDATE ${myuniversity}_${mymodule}.transaction AS encumbrance
SET
SerhiiNosko marked this conversation as resolved.
Show resolved Hide resolved
jsonb = jsonb_set(
jsonb_set(
encumbrance.jsonb,
'{encumbrance,amountExpended}',
to_jsonb(aggregated_amounts.total_expended)
),
'{encumbrance,amountCredited}',
to_jsonb(aggregated_amounts.total_credited)
jsonb = jsonb || jsonb_build_object(
'encumbrance', jsonb_build_object(
'amountExpended', to_jsonb(aggregated_amounts.total_expended),
'amountCredited', to_jsonb(aggregated_amounts.total_credited)
)
)
FROM aggregated_amounts
WHERE encumbrance.id = aggregated_amounts.encumbrance_id;