1- -- name: UpsertSnapshotEnvAndBuild :one
2- -- Try to update an existing snapshot
3- WITH updated_snapshot AS (
4- UPDATE " public" ." snapshots" s
5- SET
6- metadata = @metadata,
7- sandbox_started_at = @started_at,
8- origin_node_id = @origin_node_id,
9- auto_pause = @auto_pause,
10- config = @config
11- FROM " public" ." envs" e
12- WHERE
13- s .sandbox_id = @sandbox_id
14- AND e .id = s .env_id
15- AND e .team_id = @team_id
16- RETURNING s .env_id
17- ),
18- -- otherwise insert a new one with a new env
19- inserted_env AS (
20- INSERT INTO " public" ." envs" (id, public, created_by, team_id, updated_at)
21- SELECT @template_id, FALSE, NULL , @team_id, now()
22- WHERE NOT EXISTS (SELECT 1 FROM updated_snapshot)
23- ON CONFLICT (id) DO NOTHING
24- RETURNING id AS env_id
1+ -- name: UpsertSnapshot :one
2+ WITH new_template AS (
3+ INSERT INTO " public" ." envs" (id, public, created_by, team_id, updated_at)
4+ SELECT @template_id, FALSE, NULL , @team_id, now()
5+ WHERE NOT EXISTS (
6+ SELECT id
7+ FROM " public" ." snapshots" s
8+ WHERE s .sandbox_id = @sandbox_id
9+ ) RETURNING id
2510),
26- inserted_snapshot AS (
27- INSERT INTO " public" ." snapshots" (
11+
12+ -- Create a new snapshot or update an existing one
13+ snapshot as (
14+ INSERT INTO " public" ." snapshots" (
2815 sandbox_id,
2916 base_env_id,
3017 team_id,
@@ -36,32 +23,28 @@ WITH updated_snapshot AS (
3623 origin_node_id,
3724 auto_pause,
3825 config
39- )
40- SELECT
41- @sandbox_id,
42- @base_template_id,
43- @team_id,
44- COALESCE(
45- (SELECT env_id FROM inserted_env LIMIT 1 ),
46- @template_id -- env already existed
47- ) AS env_id,
48- @metadata,
49- @started_at,
50- @secure,
51- @allow_internet_access,
52- @origin_node_id,
53- @auto_pause,
54- @config
55- WHERE NOT EXISTS (SELECT 1 FROM updated_snapshot)
56- RETURNING env_id
57- ),
58- -- If we updated an existing snapshot, use that env_id.
59- -- Otherwise use env_id from the newly inserted snapshot.
60- final_env AS (
61- SELECT env_id FROM updated_snapshot
62- UNION ALL
63- SELECT env_id FROM inserted_snapshot
64- )
26+ )
27+ VALUES (
28+ @sandbox_id,
29+ @base_template_id,
30+ @team_id,
31+ COALESCE((SELECT id FROM new_template), ' ' ),
32+ @metadata,
33+ @started_at,
34+ @secure,
35+ @allow_internet_access,
36+ @origin_node_id,
37+ @auto_pause,
38+ @config
39+ )
40+ ON CONFLICT (sandbox_id) DO UPDATE SET
41+ metadata = @metadata,
42+ sandbox_started_at = @started_at,
43+ origin_node_id = @origin_node_id,
44+ auto_pause = @auto_pause,
45+ config = @config
46+ RETURNING env_id as template_id
47+ )
6548
6649-- Create a new build for the snapshot
6750INSERT INTO " public" ." env_builds" (
@@ -76,9 +59,8 @@ INSERT INTO "public"."env_builds" (
7659 cluster_node_id,
7760 total_disk_size_mb,
7861 updated_at
79- )
80- SELECT
81- (SELECT env_id FROM final_env LIMIT 1 ),
62+ ) VALUES (
63+ (SELECT template_id FROM snapshot),
8264 @vcpu,
8365 @ram_mb,
8466 0 ,
8971 @origin_node_id,
9072 @total_disk_size_mb,
9173 now()
92- RETURNING id as build_id, env_id as template_id;
74+ ) RETURNING id as build_id, env_id as template_id;
0 commit comments