Muezz
04/27/2022, 4:58 PMtourdownunder
04/27/2022, 5:13 PMMuezz
04/27/2022, 5:32 PMxephyr
04/27/2022, 10:40 PMsupabase db remote commit
and it generates a {date}_remote_migration.sql
, do you all rename the remote_migration
part of the name? Kind of annoying that it doesn't let you give it a name in CLI like the local equivalentMuezz
04/28/2022, 10:05 AMsql
CREATE OR REPLACE FUNCTION update_row ()
RETURNS trigger AS $$
BEGIN
NEW."random_field" = 'text';
RETURN NEW;
END;
$$ LANGUAGE plpgsql security definer;
And I create a trigger for ON INSERT
and make it run BEFORE INSERT
. I know that the new row will have 'text' in its random_field
.
But if the trigger is made to run AFTER INSERT
, would the new row still get the updated random_field
?Vinzent
04/28/2022, 11:33 AMMuezz
04/28/2022, 11:36 AMMuezz
04/29/2022, 7:33 AMSTILLWATER;
04/29/2022, 10:09 AMtourdownunder
04/29/2022, 10:32 AMMuezz
04/30/2022, 10:55 AMsql
CREATE OR REPLACE FUNCTION get_account_balances (account_names text[])
RETURNS JSON AS $$
DECLARE
t_row db_transactions;
account TEXT;
balances TEXT[];
BEGIN
FOREACH account IN ARRAY account_names
LOOP
SELECT *
INTO t_row
FROM public.db_transactions
WHERE (deb_acc = account
OR cred_acc = account)
ORDER BY t_date DESC, updated_at DESC
FETCH FIRST ROW ONLY;
RAISE LOG 'Row with the account % is %', account, t_row;
IF t_row.deb_acc=account THEN
balances = array_cat(balances, ARRAY[account, COALESCE(t_row.deb_acc_bal,0)::TEXT]);
ELSE
balances = array_cat(balances, ARRAY[account, COALESCE(t_row.cred_acc_bal,0)::TEXT]);
END IF;
RAISE LOG 'Balances so far: %', balances;
END LOOP;
RAISE LOG 'Balances length: %', array_length(balances, 1);
RETURN json_build_object(balances);
END;
$$ LANGUAGE plpgsql security definer;
I am getting this error in the logs: argument list must have even number of elements
but I know for a fact that cant be true as I am only adding two values to the balances
array with each loop where one should be the key and the other should be the value. What am I doing wrong here?Muezz
04/30/2022, 12:48 PMjson_build_object
function is taking the balances
array as a single element. That is why the argument is odd. So is there a way to make it so the json_build_object
takes the internal elements of the balances
array?garyaustin
04/30/2022, 2:22 PMMuezz
04/30/2022, 2:29 PMjson_object
create something like this: {'a':'b'}
? If so, I could "add" some to an array like balances
here like this:`[{'a':'b'},{'c':'d'},{'e':'f'}]`. How would I go from this to this --> {'a':'b','c':'d','e':'f'}
which is a single json object.garyaustin
04/30/2022, 2:34 PMSTILLWATER;
05/02/2022, 12:48 PMSTILLWATER;
05/02/2022, 2:21 PMSteve
05/02/2022, 5:33 PMRValle
05/02/2022, 5:50 PMpgsql
CREATE OR REPLACE FUNCTION get_user_role(uid uuid) RETURNS VARCHAR AS $$
DECLARE user_role varchar(255);
BEGIN
SELECT role INTO user_role FROM public.user WHERE id = uid;
RETURN user_role;
END;
$$ LANGUAGE plpgsql
And here's the policy:
pgsql
ALTER POLICY "Enable insert staff users only" ON public.company WITH CHECK (get_user_role(uid()) = 'staff');
I'm receiving row lever security violation when I'm trying to insert as a staff user.Muezz
05/04/2022, 12:41 PMsql
CREATE OR REPLACE FUNCTION get_transactions (start_date TIMESTAMP, end_date TIMESTAMP, t_cat TEXT, t_account TEXT)
RETURNS db_transactions AS $$
DECLARE
BEGIN
RETURN QUERY
SELECT *
FROM PUBLIC.db_transactions
WHERE (t_date >= start_date OR t_date < end_date) AND category = t_cat AND (deb_acc = t_account OR cred_acc = t_account)
ORDER BY t_date DESC, updated_at DESC;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
This would show some rows from a table where the user can filter and search for rows depending on their criteria. By default, I want to show all transactions. Is there a way to make it so that if the arguments are lets say NULL
or anyother placeholder string, the whole WHERE
clause is ignored?
Furthermore, it is also possible that the user may enter no dates but other arguments are entered. In that case, I also want relevant rows.tourdownunder
05/04/2022, 1:04 PMMuezz
05/04/2022, 1:05 PMNULL
? That is unlikely for my case but just to be safe, is that possible?Muezz
05/04/2022, 1:19 PMSacha
05/04/2022, 2:21 PMSacha
05/04/2022, 2:25 PMgaryaustin
05/04/2022, 2:54 PMbenten
05/07/2022, 2:20 AMMuezz
05/07/2022, 8:17 AMBlindWebDev
05/07/2022, 2:57 PMjon.m
05/07/2022, 3:10 PM