schema/9,10,18: Fix to run with psql15 (files 11+ may still fail to load)

This commit is contained in:
Petr Baudis 2025-04-23 16:14:35 +02:00
parent fadf064387
commit 3f45f52f60
3 changed files with 23 additions and 24 deletions

View file

@ -33,8 +33,8 @@ IF brmbar_privileged.has_exact_schema_version(8) THEN
-- return negative number on rate not found -- return negative number on rate not found
CREATE OR REPLACE FUNCTION public.find_buy_rate( CREATE OR REPLACE FUNCTION public.find_buy_rate(
IN i_item_id public.accounts.id%TYPE; IN i_item_id public.accounts.id%TYPE,
IN i_other_id public.accounts.id%TYPE; IN i_other_id public.accounts.id%TYPE
) RETURNS NUMERIC ) RETURNS NUMERIC
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $$
@ -42,7 +42,7 @@ DECLARE
v_rate public.exchange_rates.rate%TYPE; v_rate public.exchange_rates.rate%TYPE;
v_rate_dir public.exchange_rates.rate_dir%TYPE; v_rate_dir public.exchange_rates.rate_dir%TYPE;
BEGIN BEGIN
SELECT rate INTO STRICT v_rate, rate_dir INTO STRICT v_rate_dir FROM public.exchange_rates WHERE target = i_item_id AND source = i_other_id AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1; SELECT rate, rate_dir INTO STRICT v_rate, v_rate_dir FROM public.exchange_rates WHERE target = i_item_id AND source = i_other_id AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1;
IF v_rate_dir = 'target_to_source'::public.exchange_rate_direction THEN IF v_rate_dir = 'target_to_source'::public.exchange_rate_direction THEN
RETURN v_rate; RETURN v_rate;
ELSE ELSE
@ -52,13 +52,12 @@ EXCEPTION
WHEN NO_DATA_FOUND THEN WHEN NO_DATA_FOUND THEN
RETURN -1; RETURN -1;
END; END;
$$ $$;
-- return negative number on rate not found -- return negative number on rate not found
CREATE OR REPLACE FUNCTION public.find_sell_rate( CREATE OR REPLACE FUNCTION public.find_sell_rate(
IN i_item_id public.accounts.id%TYPE; IN i_item_id public.accounts.id%TYPE,
IN i_other_id public.accounts.id%TYPE; IN i_other_id public.accounts.id%TYPE
) RETURNS NUMERIC ) RETURNS NUMERIC
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $$
@ -66,24 +65,24 @@ DECLARE
v_rate public.exchange_rates.rate%TYPE; v_rate public.exchange_rates.rate%TYPE;
v_rate_dir public.exchange_rates.rate_dir%TYPE; v_rate_dir public.exchange_rates.rate_dir%TYPE;
BEGIN BEGIN
SELECT rate INTO STRICT v_rate, rate_dir INTO STRICT v_rate_dir FROM public.exchange_rates WHERE target = i_other_id AND source = i_item_id AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1; SELECT rate, rate_dir INTO STRICT v_rate, v_rate_dir FROM public.exchange_rates WHERE source = i_item_id AND target = i_other_id AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1;
IF v_rate_dir = 'source_to_target'::public.exchange_rate_direction THEN IF v_rate_dir = 'target_to_source'::public.exchange_rate_direction THEN
RETURN v_rate;
ELSE
RETURN 1/v_rate; RETURN 1/v_rate;
ELSE
RETURN v_rate;
END IF; END IF;
EXCEPTION EXCEPTION
WHEN NO_DATA_FOUND THEN WHEN NO_DATA_FOUND THEN
RETURN -1; RETURN -1;
END; END;
$$ $$;
CREATE OR REPLACE FUNCTION public.create_transaction( CREATE OR REPLACE FUNCTION public.create_transaction(
i_responsible_id public.accounts.id%TYPE, i_responsible_id public.accounts.id%TYPE,
i_description public.transactions.description%TYPE i_description public.transactions.description%TYPE
) RETURNS public.transactions.id%TYPE AS $$ ) RETURNS public.transactions.id%TYPE AS $$
DECLARE DECLARE
new_transaction_id public.transactions%TYPE; new_transaction_id public.transactions.id%TYPE;
BEGIN BEGIN
-- Create a new transaction -- Create a new transaction
INSERT INTO public.transactions (responsible, description) INSERT INTO public.transactions (responsible, description)
@ -123,17 +122,17 @@ BEGIN
-- the item (decrease stock) -- the item (decrease stock)
INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) INSERT INTO public.transaction_splits (transaction, side, account, amount, memo)
VALUES (i_transaction_id, 'credit', i_item_id, i_amount, VALUES (v_transaction_id, 'credit', i_item_id, i_amount,
(SELECT "name" FROM public.accounts WHERE id = i_user_id)); (SELECT "name" FROM public.accounts WHERE id = i_user_id));
-- the user -- the user
INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) INSERT INTO public.transaction_splits (transaction, side, account, amount, memo)
VALUES (i_transaction_id, 'debit', i_user_id, v_cost, VALUES (v_transaction_id, 'debit', i_user_id, v_cost,
(SELECT "name" FROM public.accounts WHERE id = i_item_id)); (SELECT "name" FROM public.accounts WHERE id = i_item_id));
-- the profit -- the profit
INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) INSERT INTO public.transaction_splits (transaction, side, account, amount, memo)
VALUES (i_transaction_id, 'debit', (SELECT account_id FROM accounts WHERE name = 'BrmBar Profits'), v_profit, (SELECT 'Margin on ' || "name" FROM public.accounts WHERE id = i_item_id)); VALUES (v_transaction_id, 'debit', (SELECT account_id FROM accounts WHERE name = 'BrmBar Profits'), v_profit, (SELECT 'Margin on ' || "name" FROM public.accounts WHERE id = i_item_id));
-- Return the cost -- Return the cost
RETURN v_cost; RETURN v_cost;

View file

@ -36,7 +36,7 @@ CREATE OR REPLACE FUNCTION brmbar_privileged.create_transaction(
i_description public.transactions.description%TYPE i_description public.transactions.description%TYPE
) RETURNS public.transactions.id%TYPE AS $$ ) RETURNS public.transactions.id%TYPE AS $$
DECLARE DECLARE
new_transaction_id public.transactions%TYPE; new_transaction_id public.transactions.id%TYPE;
BEGIN BEGIN
-- Create a new transaction -- Create a new transaction
INSERT INTO public.transactions (responsible, description) INSERT INTO public.transactions (responsible, description)
@ -77,17 +77,17 @@ BEGIN
-- the item (decrease stock) -- the item (decrease stock)
INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) INSERT INTO public.transaction_splits (transaction, side, account, amount, memo)
VALUES (i_transaction_id, 'credit', i_item_id, i_amount, VALUES (v_transaction_id, 'credit', i_item_id, i_amount,
i_other_memo); i_other_memo);
-- the user -- the user
INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) INSERT INTO public.transaction_splits (transaction, side, account, amount, memo)
VALUES (i_transaction_id, 'debit', i_user_id, v_cost, VALUES (v_transaction_id, 'debit', i_user_id, v_cost,
(SELECT "name" FROM public.accounts WHERE id = i_item_id)); (SELECT "name" FROM public.accounts WHERE id = i_item_id));
-- the profit -- the profit
INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) INSERT INTO public.transaction_splits (transaction, side, account, amount, memo)
VALUES (i_transaction_id, 'debit', (SELECT account_id FROM accounts WHERE name = 'BrmBar Profits'), v_profit, (SELECT 'Margin on ' || "name" FROM public.accounts WHERE id = i_item_id)); VALUES (v_transaction_id, 'debit', (SELECT account_id FROM accounts WHERE name = 'BrmBar Profits'), v_profit, (SELECT 'Margin on ' || "name" FROM public.accounts WHERE id = i_item_id));
-- Return the cost -- Return the cost
RETURN v_cost; RETURN v_cost;
@ -106,7 +106,7 @@ AS $$
BEGIN BEGIN
RETURN brmbar_privileged.sell_item_internal(i_item_id, RETURN brmbar_privileged.sell_item_internal(i_item_id,
i_amount, i_amount,
i_other_id, i_user_id,
i_target_currency_id, i_target_currency_id,
(SELECT "name" FROM public.accounts WHERE id = i_user_id), (SELECT "name" FROM public.accounts WHERE id = i_user_id),
i_description); i_description);
@ -125,7 +125,7 @@ AS $$
BEGIN BEGIN
RETURN brmbar_privileged.sell_item_internal(i_item_id, RETURN brmbar_privileged.sell_item_internal(i_item_id,
i_amount, i_amount,
i_other_id, i_user_id,
i_target_currency_id, i_target_currency_id,
'Cash', 'Cash',
i_description); i_description);

View file

@ -32,8 +32,8 @@ BEGIN
IF brmbar_privileged.has_exact_schema_version(17) THEN IF brmbar_privileged.has_exact_schema_version(17) THEN
CREATE OR REPLACE FUNCTION public.fix_cash( CREATE OR REPLACE FUNCTION public.fix_cash(
IN i_excess_id public.acounts.id%TYPE, IN i_excess_id public.accounts.id%TYPE,
IN i_deficit_id public.acounts.id%TYPE, IN i_deficit_id public.accounts.id%TYPE,
IN i_shop_currency_id public.currencies.id%TYPE, IN i_shop_currency_id public.currencies.id%TYPE,
IN i_amount_in_reality NUMERIC IN i_amount_in_reality NUMERIC
) RETURNS BOOLEAN ) RETURNS BOOLEAN