Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions internal/pkg/store/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,29 @@ func (s *Store) FindDeploymentByUID(ctx context.Context, uid int64) (*ent.Deploy
}

func (s *Store) GetNextDeploymentNumberOfRepo(ctx context.Context, r *ent.Repo) (int, error) {
cnt, err := s.c.Deployment.Query().
Where(
deployment.RepoID(r.ID),
).
Count(ctx)
var v []struct {
RepoID int64 `json:"repo_id"`
Max int `json:"max"`
}
err := s.c.Deployment.Query().
Where(deployment.RepoID(r.ID)).
GroupBy(deployment.FieldRepoID).
Aggregate(ent.Max(deployment.FieldNumber)).
Scan(ctx, &v)
if err != nil {
return 0, err
return 0, e.NewError(e.ErrorCodeInternalError, err)
}

if len(v) >= 2 {
return 0, e.NewErrorWithMessage(e.ErrorCodeInternalError, "The result format of query is invalid.", err)
}

// The return value must be one when there is no deployment record.
if len(v) == 0 {
return 1, nil
}

return cnt + 1, nil
return v[0].Max + 1, nil
}

// FindPrevRunningDeployment find a deployment of which the status is created, queued, or running.
Expand Down
16 changes: 5 additions & 11 deletions internal/pkg/store/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,6 @@ func TestStore_GetNextDeploymentNumberOfRepo(t *testing.T) {
})

t.Run("Return two when there is a single deployment.", func(t *testing.T) {
const (
u1 = 1
r1 = 1
r2 = 2
)

client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1",
enttest.WithMigrateOptions(migrate.WithForeignKeys(false)),
)
Expand All @@ -309,8 +303,8 @@ func TestStore_GetNextDeploymentNumberOfRepo(t *testing.T) {
SetType("branch").
SetRef("main").
SetEnv("local").
SetUserID(u1).
SetRepoID(r1).
SetUserID(1).
SetRepoID(1).
SetStatus(deployment.StatusCreated).
SaveX(ctx)

Expand All @@ -320,14 +314,14 @@ func TestStore_GetNextDeploymentNumberOfRepo(t *testing.T) {
SetType("branch").
SetRef("main").
SetEnv("prod").
SetUserID(u1).
SetRepoID(r2).
SetUserID(1).
SetRepoID(2).
SetStatus(deployment.StatusCreated).
SaveX(ctx)

s := NewStore(client)

number, err := s.GetNextDeploymentNumberOfRepo(ctx, &ent.Repo{ID: r1})
number, err := s.GetNextDeploymentNumberOfRepo(ctx, &ent.Repo{ID: 1})
if err != nil {
t.Fatalf("GetNextDeploymentNumberOfRepo returns an error: %s", err)
t.FailNow()
Expand Down
4 changes: 2 additions & 2 deletions model/ent/migrate/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion model/ent/schema/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (Deployment) Indexes() []ent.Index {
index.Fields("repo_id", "env", "created_at"),
index.Fields("repo_id", "created_at"),
// The deployment number is unique for the repo.
index.Fields("number", "repo_id").
index.Fields("repo_id", "number").
Unique(),
// Find by UID when the hook is coming.
index.Fields("uid"),
Expand Down