File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ -- Function to atomically create (if needed) and set the default image by name.
2
+ -- Usage: select app.update_default_image('supabase/etl-replicator:1.2.3');
3
+
4
+ create or replace function app .update_default_image(new_image_name text )
5
+ returns void
6
+ language plpgsql
7
+ as $$
8
+ declare
9
+ new_image_id bigint ;
10
+ begin
11
+ -- Serialize concurrent default switches using an advisory transaction lock.
12
+ perform pg_advisory_xact_lock((' app.images' ::regclass::oid )::bigint );
13
+
14
+ -- Insert if missing; don't set default yet to respect the partial unique index.
15
+ insert into app .images (name, is_default)
16
+ values (new_image_name, false)
17
+ on conflict (name) do nothing
18
+ returning id into new_image_id;
19
+
20
+ -- If the image already existed, fetch its id.
21
+ if new_image_id is null then
22
+ select id into new_image_id from app .images where name = new_image_name;
23
+ end if;
24
+
25
+ -- Unset any existing default that is not the requested image.
26
+ update app .images
27
+ set is_default = false, updated_at = now()
28
+ where is_default = true
29
+ and id <> new_image_id;
30
+
31
+ -- Set the requested image as default.
32
+ update app .images
33
+ set is_default = true, updated_at = now()
34
+ where id = new_image_id;
35
+ end
36
+ $$;
You can’t perform that action at this time.
0 commit comments