Skip to content

Commit 719d6a2

Browse files
committed
Simplify the query
1 parent e4678bf commit 719d6a2

File tree

4 files changed

+104
-143
lines changed

4 files changed

+104
-143
lines changed

packages/api/internal/orchestrator/pause_instance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (o *Orchestrator) pauseSandbox(ctx context.Context, node *nodemanager.Node,
3030
ctx, span := tracer.Start(ctx, "pause-sandbox")
3131
defer span.End()
3232

33-
snapshotConfig := queries.UpsertSnapshotEnvAndBuildParams{
33+
snapshotConfig := queries.UpsertSnapshotParams{
3434
// Used if there's no snapshot for this sandbox yet
3535
TemplateID: id.Generate(),
3636
TeamID: sbx.TeamID,
@@ -55,7 +55,7 @@ func (o *Orchestrator) pauseSandbox(ctx context.Context, node *nodemanager.Node,
5555
Status: string(envbuild.StatusSnapshotting),
5656
}
5757

58-
result, err := o.sqlcDB.UpsertSnapshotEnvAndBuild(ctx, snapshotConfig)
58+
result, err := o.sqlcDB.UpsertSnapshot(ctx, snapshotConfig)
5959
if err != nil {
6060
telemetry.ReportCriticalError(ctx, "error inserting snapshot for env", err)
6161

packages/db/queries/create_new_snapshot.sql.go

Lines changed: 63 additions & 85 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
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
6750
INSERT 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,
@@ -89,4 +71,4 @@ SELECT
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;

packages/shared/pkg/feature-flags/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func NewClient() (*Client, error) {
3333
"",
3434
ldclient.Config{
3535
DataSource: LaunchDarklyOfflineStore,
36+
Offline: true,
3637
},
3738
0)
3839
if err != nil {

0 commit comments

Comments
 (0)