Skip to content

Commit 4ec6abe

Browse files
charlie-e2be2b-bot[bot]
authored andcommitted
fix(db): set a build's env and team when it is inserted
GitOrigin-RevId: 6adcdd1e7add73fa382d0a3cc7d65ab047873740
1 parent a28962e commit 4ec6abe

6 files changed

Lines changed: 90 additions & 2 deletions

File tree

‎packages/api/internal/template/register_build.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ func registerBuild(
287287
// used in TemplateBuildMetadata and SetFinished overwrites these rows.
288288
err = client.CreateTemplateBuild(ctx, queries.CreateTemplateBuildParams{
289289
BuildID: buildID,
290+
TemplateID: data.TemplateID,
290291
Status: dbtypes.BuildStatusWaiting,
291292
RamMb: ramMB,
292293
Vcpu: cpuCount,
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package tests
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/uuid"
7+
"github.com/jackc/pgx/v5"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/e2b-dev/infra/packages/db/pkg/testutils"
11+
"github.com/e2b-dev/infra/packages/db/pkg/types"
12+
"github.com/e2b-dev/infra/packages/db/queries"
13+
)
14+
15+
// Template builds and snapshot builds set env_builds.env_id and team_id when
16+
// they are inserted, so the build is complete without the backfill triggers
17+
// on env_build_assignments. The triggers are disabled here to prove it.
18+
func TestBuildInsertsSetEnvAndTeamWithoutTriggers(t *testing.T) {
19+
t.Parallel()
20+
21+
db := testutils.SetupDatabase(t)
22+
ctx := t.Context()
23+
require.NoError(t, db.SqlcClient.TestsRawSQL(ctx, `ALTER TABLE public.env_build_assignments DISABLE TRIGGER USER`))
24+
25+
teamID := testutils.CreateTestTeam(t, db)
26+
templateID := testutils.CreateTestTemplate(t, db, teamID)
27+
read := func(t *testing.T, buildID uuid.UUID) (*string, *uuid.UUID) {
28+
t.Helper()
29+
var envID *string
30+
var buildTeamID *uuid.UUID
31+
require.NoError(t, db.SqlcClient.TestsRawSQLQuery(ctx, `SELECT env_id, team_id FROM public.env_builds WHERE id = $1`,
32+
func(rows pgx.Rows) error {
33+
require.True(t, rows.Next())
34+
35+
return rows.Scan(&envID, &buildTeamID)
36+
}, buildID))
37+
38+
return envID, buildTeamID
39+
}
40+
41+
buildID := uuid.New()
42+
require.NoError(t, db.SqlcClient.CreateTemplateBuild(ctx, queries.CreateTemplateBuildParams{
43+
BuildID: buildID,
44+
TemplateID: templateID,
45+
Status: types.BuildStatusWaiting,
46+
RamMb: 512,
47+
Vcpu: 1,
48+
KernelVersion: "6.1.0",
49+
FirecrackerVersion: "1.4.0",
50+
FreeDiskSizeMb: 512,
51+
}))
52+
envID, buildTeamID := read(t, buildID)
53+
require.Equal(t, &templateID, envID)
54+
require.Equal(t, &teamID, buildTeamID)
55+
56+
// The first pause creates the snapshot env; a second pause of the same
57+
// sandbox adds a build to the existing one.
58+
sandboxID := "sbx-" + uuid.NewString()
59+
for range 2 {
60+
snapshot := testutils.UpsertTestSnapshot(t, ctx, db, "snap-"+uuid.NewString(), sandboxID, teamID, templateID)
61+
envID, buildTeamID := read(t, snapshot.BuildID)
62+
require.Equal(t, &snapshot.TemplateID, envID)
63+
require.Equal(t, &teamID, buildTeamID)
64+
}
65+
}

‎packages/db/queries/create_new_snapshot.sql.go‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/db/queries/create_template.sql.go‎

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/db/queries/snapshots/create_new_snapshot.sql‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ snapshot as (
5656
-- the row is always inserted (CPU info is NULL if the source build is missing).
5757
new_build as (
5858
INSERT INTO "public"."env_builds" (
59+
env_id,
60+
team_id,
5961
vcpu,
6062
ram_mb,
6163
free_disk_size_mb,
@@ -73,6 +75,8 @@ new_build as (
7375
cpu_flags
7476
)
7577
VALUES (
78+
(SELECT template_id FROM snapshot),
79+
@team_id,
7680
@vcpu,
7781
@ram_mb,
7882
@free_disk_size_mb,

‎packages/db/queries/templates/create_template.sql‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ WHERE build_id IN (SELECT id FROM invalidated);
3232
-- completes. The template-manager reports the versions it actually used via
3333
-- TemplateBuildMetadata, and FinishTemplateBuild overwrites these fields with
3434
-- the reported values.
35+
-- env_id and team_id are set here rather than left to the trigger on
36+
-- env_build_assignments, so the build is complete without it.
3537
INSERT INTO "public"."env_builds" (
3638
id,
39+
env_id,
40+
team_id,
3741
updated_at,
3842
status,
3943
ram_mb,
@@ -47,6 +51,8 @@ INSERT INTO "public"."env_builds" (
4751
version
4852
) VALUES (
4953
@build_id,
54+
sqlc.arg(template_id)::text,
55+
(SELECT team_id FROM "public"."envs" WHERE id = sqlc.arg(template_id)::text),
5056
NOW(),
5157
@status,
5258
@ram_mb,

0 commit comments

Comments
 (0)