Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 582edcb

Browse files
author
Noah Lee
authored
Fix the bug of returning the next deployment number. (#409)
* Reorder columns for (repo_id, number) index * Fix the bug of returning the next number.
1 parent 2fd1726 commit 582edcb

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

Diff for: internal/pkg/store/deployment.go

+20-7
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,29 @@ func (s *Store) FindDeploymentByUID(ctx context.Context, uid int64) (*ent.Deploy
196196
}
197197

198198
func (s *Store) GetNextDeploymentNumberOfRepo(ctx context.Context, r *ent.Repo) (int, error) {
199-
cnt, err := s.c.Deployment.Query().
200-
Where(
201-
deployment.RepoID(r.ID),
202-
).
203-
Count(ctx)
199+
var v []struct {
200+
RepoID int64 `json:"repo_id"`
201+
Max int `json:"max"`
202+
}
203+
err := s.c.Deployment.Query().
204+
Where(deployment.RepoID(r.ID)).
205+
GroupBy(deployment.FieldRepoID).
206+
Aggregate(ent.Max(deployment.FieldNumber)).
207+
Scan(ctx, &v)
204208
if err != nil {
205-
return 0, err
209+
return 0, e.NewError(e.ErrorCodeInternalError, err)
210+
}
211+
212+
if len(v) >= 2 {
213+
return 0, e.NewErrorWithMessage(e.ErrorCodeInternalError, "The result format of query is invalid.", err)
214+
}
215+
216+
// The return value must be one when there is no deployment record.
217+
if len(v) == 0 {
218+
return 1, nil
206219
}
207220

208-
return cnt + 1, nil
221+
return v[0].Max + 1, nil
209222
}
210223

211224
// FindPrevRunningDeployment find a deployment of which the status is created, queued, or running.

Diff for: internal/pkg/store/deployment_test.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,6 @@ func TestStore_GetNextDeploymentNumberOfRepo(t *testing.T) {
290290
})
291291

292292
t.Run("Return two when there is a single deployment.", func(t *testing.T) {
293-
const (
294-
u1 = 1
295-
r1 = 1
296-
r2 = 2
297-
)
298-
299293
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1",
300294
enttest.WithMigrateOptions(migrate.WithForeignKeys(false)),
301295
)
@@ -309,8 +303,8 @@ func TestStore_GetNextDeploymentNumberOfRepo(t *testing.T) {
309303
SetType("branch").
310304
SetRef("main").
311305
SetEnv("local").
312-
SetUserID(u1).
313-
SetRepoID(r1).
306+
SetUserID(1).
307+
SetRepoID(1).
314308
SetStatus(deployment.StatusCreated).
315309
SaveX(ctx)
316310

@@ -320,14 +314,14 @@ func TestStore_GetNextDeploymentNumberOfRepo(t *testing.T) {
320314
SetType("branch").
321315
SetRef("main").
322316
SetEnv("prod").
323-
SetUserID(u1).
324-
SetRepoID(r2).
317+
SetUserID(1).
318+
SetRepoID(2).
325319
SetStatus(deployment.StatusCreated).
326320
SaveX(ctx)
327321

328322
s := NewStore(client)
329323

330-
number, err := s.GetNextDeploymentNumberOfRepo(ctx, &ent.Repo{ID: r1})
324+
number, err := s.GetNextDeploymentNumberOfRepo(ctx, &ent.Repo{ID: 1})
331325
if err != nil {
332326
t.Fatalf("GetNextDeploymentNumberOfRepo returns an error: %s", err)
333327
t.FailNow()

Diff for: model/ent/migrate/schema.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: model/ent/schema/deployment.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (Deployment) Indexes() []ent.Index {
107107
index.Fields("repo_id", "env", "created_at"),
108108
index.Fields("repo_id", "created_at"),
109109
// The deployment number is unique for the repo.
110-
index.Fields("number", "repo_id").
110+
index.Fields("repo_id", "number").
111111
Unique(),
112112
// Find by UID when the hook is coming.
113113
index.Fields("uid"),

0 commit comments

Comments
 (0)