dipankarmaikap
03/17/2022, 11:57 AMdipankarmaikap
03/17/2022, 11:58 AM-- inserts a row into public.users
create OR REPLACE function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = public
as $$
begin
insert into public.profiles (id, raw_user_meta_data)
values (new.id, new.raw_user_meta_data);
return new;
end;
$$;
-- trigger the function every time a user is created
DROP TRIGGER IF EXISTS on_auth_user_created on auth.users;
create trigger on_auth_user_created
after insert on auth.users
for each row execute procedure public.handle_new_user();
dipankarmaikap
03/17/2022, 12:00 PMawait supabase.auth.update({
data: { account_deleted: new Date() },
});
garyaustin
03/17/2022, 1:21 PMak4zh
03/18/2022, 7:18 AMtransactions
atleast 2 record will be inserted in journals
- for each record in journals
against a transaction_id
the sum of amount for is_credit=true
must be equal to is_credit=false
otherwise all records roll back
- I also do not want any record in transactions
without any corresponding records in journals
How to achieve above ?
I will be using supabase.js to push new records.
sql
CREATE TABLE IF NOT EXISTS public.accounts (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
created_at timestamp with time zone DEFAULT now(),
name text COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT accounts_pkey PRIMARY KEY (id),
);
CREATE TABLE transactions (
id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ),
created_at timestamp with time zone DEFAULT now(),
transaction_date date not null default current_date,
description text NOT NULL,
CONSTRAINT transactions_pkey PRIMARY KEY (id),
);
CREATE TABLE journals(
id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ),
transaction_id bigint NOT NULL,
account_id uuid NOT NULL,
amount NUMERIC(20, 2) NOT NULL,
is_credit boolean NOT NULL,
CONSTRAINT journals_pkey PRIMARY KEY (id),
CONSTRAINT journals_transaction_id_fkey FOREIGN KEY (transaction_id)
REFERENCES public.transactions (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
CONSTRAINT journals_account_id_fkey FOREIGN KEY (account_id)
REFERENCES public.accounts (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
);
ak4zh
03/18/2022, 9:27 AMAmusedGrape
03/18/2022, 1:40 PMsyntax error at or near "insert"
sql
create function commands_inc (x int, row_id text)
returns void as
$$
insert into command_logs (id, commands) values (row_id, x) on conflict do update
$$
language plpgsql volatile;
what am i doing wrong? thanks!d33pu
03/21/2022, 2:27 AMcreate or replace function latestOilFee()
return array
IS
Begin
select * from oil_tracker order by ts desc fetch first 1 row only;
End;
Error: syntax error at or near "return"Scott P
03/21/2022, 2:39 AMTMShader
03/22/2022, 1:29 PMchipilov
03/22/2022, 1:40 PMMDobs
03/22/2022, 2:43 PMSELECT * FROM users
AmusedGrape
03/22/2022, 5:54 PMTo insert:
{ "a": 1, "b": 2 }
Already there:
{ "a": 2, "b": 3, "c": 1}
What I want:
{ "a": 3, "b": 5, "c": 1 }
what's the best way to do this with SQL?TMShader
03/23/2022, 8:24 PMgaryaustin
03/23/2022, 8:37 PMsseppola
03/24/2022, 8:23 AMCREATE OR REPLACE FUNCTION public.set_username(new_username text)
RETURNS public.profile
LANGUAGE 'plpgsql'
SECURITY INVOKER
SET SEARCH_PATH = public, auth
AS
$$
declare
updatedProfile public.profile;
begin
insert into public.profile(id, username)
select auth.uid(), new_username
returning * into updatedProfile;
return updatedProfile;
end
$$;
I get "permission denied for schema auth", but I thought with security invoker
and setting the search_path
auth should be available. It's called using the supabase.rpc function.
Fyi, this is a contrived example of what I'm trying to do.
Edit: SECURITY DEFINER
does the trick, so I guess the invoker does not and should not have access to auth.uid?garyaustin
03/24/2022, 1:44 PMsseppola
03/24/2022, 2:55 PMtruby
03/26/2022, 7:40 AM(if
auth.role() = 'authenticated'
and
is_draft = false
then
true;
elsif
auth.uid() = user_id
then
true;
else
false;
end if;
)
ak4zh
03/26/2022, 7:57 AMsql
CREATE FUNCTION node_cte(node_ids int[], org_id uuid)
...
WHERE table.id IN node_ids
How can I use the function arg to find all rows whose id is in the arg array.
The above gives array at or near INScott P
03/26/2022, 4:33 PMKirdes
03/29/2022, 6:37 PMsql
create function increment (row_id int)
returns void as
$$
update "Song"
set listening = listening + 1
where id = row_id;
$$
language sql volatile;
So this is working only for the listening column, is it possible to dynamically set the column ? I have an other column that I need to increment. or I need to make an other function ? Thxfernandolguevara
03/29/2022, 7:00 PMEXECUTE
fernandolguevara
03/29/2022, 7:01 PMEXECUTE format('SELECT * from result where id=%s', '"42"');
Kirdes
03/29/2022, 7:29 PMkbanta11
03/31/2022, 12:32 AMgaryaustin
03/31/2022, 12:45 AMNARCISO
04/01/2022, 2:22 PMMessage
table:
CREATE TABLE IF NOT EXISTS public.message
(
id uuid NOT NULL,
sender_user_id uuid NOT NULL,
recipient_user_id uuid NOT NULL,
conversation_id uuid NOT NULL,
content text COLLATE pg_catalog."default" NOT NULL,
read boolean DEFAULT false,
message_warning boolean DEFAULT false,
CONSTRAINT message_pkey PRIMARY KEY (id),
CONSTRAINT message_conversation_id_fkey FOREIGN KEY (conversation_id)
REFERENCES public.conversation (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT,
CONSTRAINT message_sender_user_id_fkey FOREIGN KEY (sender_user_id)
REFERENCES public."user" (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT,
)
The table is pretty simple but I have some the need of some "custom" Column Level Security
.
For example, I would like to let the user, through a RLS Policy
, to only UPDATE the content
and the read
column.
As far as I know this is not possible through a RLS Policy
.
Is there any other way to do this? or what's the best way to do this? maybe without using a db function
?
Thanks!garyaustin
04/01/2022, 2:59 PMBoni
04/02/2022, 12:50 AMis identity
. When inserting new rows the auto increment starts at 1. I tried setting the auto increment to begin at the end of the current max value of the ID following this github issue https://github.com/supabase/supabase/issues/1804 but when i try to access the sequence using SELECT PG_GET_SERIAL_SEQUENCE('mytable', 'id');
i get relation "mytable" does not exist
. Any idea how i can add the sequence to my table ? Thanks