From aacdd2eff95d472574b8d7db953d8eb0d6084dee Mon Sep 17 00:00:00 2001 From: Sven Urbanski Date: Thu, 26 Sep 2024 10:20:11 +0200 Subject: [PATCH] fix(db): remove dead code The function `ShouldUseOtherTables` does not make sense in the manifest-repo-export-service, because the whole service is not running, if `ShouldUseOtherTables()==false`. Ref: SRX-2CSAEK --- pkg/db/db.go | 2 + .../pkg/repository/transformer.go | 31 ++------ .../pkg/service/git.go | 71 +++++-------------- 3 files changed, 26 insertions(+), 78 deletions(-) diff --git a/pkg/db/db.go b/pkg/db/db.go index f0fcd8c1e..a295132a9 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -105,6 +105,8 @@ func (h *DBHandler) ShouldUseEslTable() bool { return h != nil } +// ShouldUseOtherTables returns true if the db is enabled and WriteEslOnly=false +// ShouldUseOtherTables should never be used in the manifest-repo-export-service. func (h *DBHandler) ShouldUseOtherTables() bool { return h != nil && !h.WriteEslOnly } diff --git a/services/manifest-repo-export-service/pkg/repository/transformer.go b/services/manifest-repo-export-service/pkg/repository/transformer.go index f2268c880..e70079c47 100644 --- a/services/manifest-repo-export-service/pkg/repository/transformer.go +++ b/services/manifest-repo-export-service/pkg/repository/transformer.go @@ -22,7 +22,6 @@ import ( "encoding/json" "errors" "fmt" - "io" "path" "slices" @@ -329,31 +328,15 @@ func (c *DeployApplicationVersion) Transform( fsys := state.Filesystem // Check that the release exist and fetch manifest - var manifestContent []byte releaseDir := releasesDirectoryWithVersion(fsys, c.Application, c.Version) - if state.DBHandler.ShouldUseOtherTables() { - version, err := state.DBHandler.DBSelectReleaseByVersion(ctx, transaction, c.Application, c.Version, true) - if err != nil { - return "", err - } - if version == nil { - return "", fmt.Errorf("release of app %s with version %v not found", c.Application, c.Version) - } - manifestContent = []byte(version.Manifests.Manifests[c.Environment]) - } else { - // Check that the release exist and fetch manifest - manifest := fsys.Join(releaseDir, "environments", c.Environment, "manifests.yaml") - if file, err := fsys.Open(manifest); err != nil { - return "", wrapFileError(err, manifest, fmt.Sprintf("deployment failed: could not open manifest for app %s with release %d on env %s", c.Application, c.Version, c.Environment)) - } else { - if content, err := io.ReadAll(file); err != nil { - return "", err - } else { - manifestContent = content - } - file.Close() - } + version, err := state.DBHandler.DBSelectReleaseByVersion(ctx, transaction, c.Application, c.Version, true) + if err != nil { + return "", err + } + if version == nil { + return "", fmt.Errorf("release of app %s with version %v not found", c.Application, c.Version) } + var manifestContent = []byte(version.Manifests.Manifests[c.Environment]) if c.LockBehaviour != api.LockBehavior_IGNORE { // Check that the environment is not locked var ( diff --git a/services/manifest-repo-export-service/pkg/service/git.go b/services/manifest-repo-export-service/pkg/service/git.go index c52e3f0b6..ea9274134 100644 --- a/services/manifest-repo-export-service/pkg/service/git.go +++ b/services/manifest-repo-export-service/pkg/service/git.go @@ -112,20 +112,12 @@ func (s *GitServer) GetCommitInfo(ctx context.Context, in *api.GetCommitInfoRequ sort.Strings(touchedApps) var events []*api.Event loadMore := false - if s.Repository.State().DBHandler.ShouldUseOtherTables() { - events, err = db.WithTransactionMultipleEntriesT(s.Repository.State().DBHandler, ctx, true, func(ctx context.Context, transaction *sql.Tx) ([]*api.Event, error) { - return s.GetEvents(ctx, transaction, fs, commitPath, pageNumber) - }) - if len(events) > int(s.PageSize) { - loadMore = true - events = events[:len(events)-1] - } - } else { - events, err = s.GetEvents(ctx, nil, fs, commitPath, pageNumber) - if len(events) > int(s.PageSize) { - loadMore = true - } - events = events[pageNumber*s.PageSize : min(len(events), int((pageNumber+1)*s.PageSize))] + events, err = db.WithTransactionMultipleEntriesT(s.Repository.State().DBHandler, ctx, true, func(ctx context.Context, transaction *sql.Tx) ([]*api.Event, error) { + return s.GetEvents(ctx, transaction, fs, commitPath, pageNumber) + }) + if len(events) > int(s.PageSize) { + loadMore = true + events = events[:len(events)-1] } if err != nil { return nil, err @@ -147,55 +139,26 @@ func (s *GitServer) GetEvents(ctx context.Context, transaction *sql.Tx, fs billy parts := strings.Split(commitPath, "/") commitID := parts[len(parts)-2] + parts[len(parts)-1] - if s.Config.DBHandler.ShouldUseOtherTables() { - events, err := s.Config.DBHandler.DBSelectAllEventsForCommit(ctx, transaction, commitID, pageNumber, s.PageSize) + events, err := s.Config.DBHandler.DBSelectAllEventsForCommit(ctx, transaction, commitID, pageNumber, s.PageSize) + if err != nil { + return nil, fmt.Errorf("could not read events from DB: %v", err) + } + for _, currEvent := range events { + ev, err := eventmod.UnMarshallEvent(currEvent.EventType, currEvent.EventJson) if err != nil { - return nil, fmt.Errorf("could not read events from DB: %v", err) + return nil, fmt.Errorf("error processing event from DB: %v", err) } - for _, currEvent := range events { - ev, err := eventmod.UnMarshallEvent(currEvent.EventType, currEvent.EventJson) - if err != nil { - return nil, fmt.Errorf("error processing event from DB: %v", err) - } - rawUUID, err := timeuuid.ParseUUID(currEvent.Uuid) - if err != nil { - return nil, fmt.Errorf("could not parse UUID: '%s'. Error: %v", currEvent.Uuid, err) - } - - result = append(result, eventmod.ToProto(rawUUID, ev.EventData)) - } - } else { - allEventsPath := fs.Join(commitPath, "events") - potentialEventDirs, err := fs.ReadDir(allEventsPath) + rawUUID, err := timeuuid.ParseUUID(currEvent.Uuid) if err != nil { - return nil, fmt.Errorf("could not read events directory '%s': %v", allEventsPath, err) + return nil, fmt.Errorf("could not parse UUID: '%s'. Error: %v", currEvent.Uuid, err) } - for i := range potentialEventDirs { - oneEventDir := potentialEventDirs[i] - if oneEventDir.IsDir() { - fileName := oneEventDir.Name() - rawUUID, err := timeuuid.ParseUUID(fileName) - if err != nil { - return nil, fmt.Errorf("could not read event directory '%s' not a UUID: %v", fs.Join(allEventsPath, fileName), err) - } - var event *api.Event - event, err = s.ReadEvent(ctx, fs, fs.Join(allEventsPath, fileName), rawUUID) - if err != nil { - return nil, fmt.Errorf("could not read events %v", err) - } - result = append(result, event) - } - } - // NOTE: We only sort when using the manifest repo because the db already sorts - sort.Slice(result, func(i, j int) bool { - return result[i].CreatedAt.AsTime().UnixNano() < result[j].CreatedAt.AsTime().UnixNano() - }) + result = append(result, eventmod.ToProto(rawUUID, ev.EventData)) } return result, nil } -func (s *GitServer) ReadEvent(ctx context.Context, fs billy.Filesystem, eventPath string, eventId timeuuid.UUID) (*api.Event, error) { +func (s *GitServer) ReadEvent(_ context.Context, fs billy.Filesystem, eventPath string, eventId timeuuid.UUID) (*api.Event, error) { event, err := eventmod.Read(fs, eventPath) if err != nil { return nil, err