-
Notifications
You must be signed in to change notification settings - Fork 0
feat(storage) Adds build store interface and mysql implementation #58
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package storage | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/uber/submitqueue/entity" | ||
| ) | ||
|
|
||
| // BuildStore is an interface that defines methods for managing builds in the database. | ||
| type BuildStore interface { | ||
| // Get retrieves a build by ID. Returns ErrNotFound if the build is not found. | ||
| Get(ctx context.Context, id string) (entity.Build, error) | ||
|
|
||
| // Create creates a new build. The build must have a unique ID already assigned. | ||
| // Returns ErrAlreadyExists if a build with the same ID already exists. | ||
| Create(ctx context.Context, build entity.Build) error | ||
|
|
||
| // UpdateStatus updates the status of a build. | ||
| UpdateStatus(ctx context.Context, id string, newStatus entity.BuildStatus) error | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package mysql | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/go-sql-driver/mysql" | ||
|
|
||
| "github.com/uber/submitqueue/entity" | ||
| "github.com/uber/submitqueue/extension/storage" | ||
| ) | ||
|
|
||
| type buildStore struct { | ||
| db *sql.DB | ||
| } | ||
|
|
||
| // NewBuildStore creates a new MySQL-backed BuildStore. | ||
| func NewBuildStore(db *sql.DB) storage.BuildStore { | ||
| return &buildStore{db: db} | ||
| } | ||
|
|
||
| // Get retrieves a build by ID. Returns ErrNotFound if the build is not found. | ||
| func (s *buildStore) Get(ctx context.Context, id string) (entity.Build, error) { | ||
| var build entity.Build | ||
| var speculationPathJSON []byte | ||
|
|
||
| err := s.db.QueryRowContext(ctx, | ||
| "SELECT id, batch_id, speculation_path, score, status FROM build WHERE id = ?", | ||
| id, | ||
| ).Scan(&build.ID, &build.BatchID, &speculationPathJSON, &build.Score, &build.Status) | ||
|
|
||
| if errors.Is(err, sql.ErrNoRows) { | ||
| return entity.Build{}, storage.WrapNotFound(err) | ||
| } | ||
| if err != nil { | ||
| return entity.Build{}, fmt.Errorf("failed to get build entity id=%s from the database: %w", id, err) | ||
| } | ||
|
|
||
| if err := json.Unmarshal(speculationPathJSON, &build.SpeculationPath); err != nil { | ||
| return entity.Build{}, fmt.Errorf("failed to unmarshal speculation_path for build entity id=%s from the database: %w", id, err) | ||
| } | ||
|
|
||
| return build, nil | ||
| } | ||
|
|
||
| // Create creates a new build. The build must have a unique ID already assigned. Returns ErrAlreadyExists if the build ID already exists. | ||
| func (s *buildStore) Create(ctx context.Context, build entity.Build) error { | ||
| speculationPathJSON, err := json.Marshal(build.SpeculationPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal speculation_path id=%s for Create build entity: %w", build.ID, err) | ||
| } | ||
|
|
||
| _, err = s.db.ExecContext(ctx, | ||
| "INSERT INTO build (id, batch_id, speculation_path, score, status) VALUES (?, ?, ?, ?, ?)", | ||
| build.ID, build.BatchID, speculationPathJSON, build.Score, build.Status, | ||
| ) | ||
| if err != nil { | ||
| var mysqlErr *mysql.MySQLError | ||
| if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 { | ||
| return fmt.Errorf("build entity id=%s: %w", build.ID, storage.ErrAlreadyExists) | ||
| } | ||
| return fmt.Errorf("failed to insert build entity id=%s: %w", build.ID, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // UpdateStatus updates the status of a build. Returns ErrNotFound if the build is not found. | ||
| func (s *buildStore) UpdateStatus(ctx context.Context, id string, newStatus entity.BuildStatus) error { | ||
| result, err := s.db.ExecContext(ctx, | ||
| "UPDATE build SET status = ? WHERE id = ?", | ||
| newStatus, id, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to update build status for id=%q newStatus=%v: %w", id, newStatus, err) | ||
| } | ||
|
|
||
| rowsAffected, err := result.RowsAffected() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get rows affected from update for id=%q newStatus=%v: %w", id, newStatus, err) | ||
| } | ||
|
|
||
| if rowsAffected != 1 { | ||
| return storage.WrapNotFound(fmt.Errorf("build entity id=%s", id)) | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.