Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(db): remove dead code #1999

Merged
merged 1 commit into from
Oct 16, 2024
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
2 changes: 2 additions & 0 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"path"
"slices"

Expand Down Expand Up @@ -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 (
Expand Down
71 changes: 17 additions & 54 deletions services/manifest-repo-export-service/pkg/service/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading