|
| 1 | +package mysql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/go-sql-driver/mysql" |
| 11 | + |
| 12 | + "github.com/uber/submitqueue/entity" |
| 13 | + "github.com/uber/submitqueue/extension/storage" |
| 14 | +) |
| 15 | + |
| 16 | +type buildStore struct { |
| 17 | + db *sql.DB |
| 18 | +} |
| 19 | + |
| 20 | +// NewBuildStore creates a new MySQL-backed BuildStore. |
| 21 | +func NewBuildStore(db *sql.DB) storage.BuildStore { |
| 22 | + return &buildStore{db: db} |
| 23 | +} |
| 24 | + |
| 25 | +// Get retrieves a build by ID. Returns ErrNotFound if the build is not found. |
| 26 | +func (s *buildStore) Get(ctx context.Context, id string) (entity.Build, error) { |
| 27 | + var build entity.Build |
| 28 | + var speculationPathJSON []byte |
| 29 | + |
| 30 | + err := s.db.QueryRowContext(ctx, |
| 31 | + "SELECT id, batch_id, speculation_path, score, status FROM build WHERE id = ?", |
| 32 | + id, |
| 33 | + ).Scan(&build.ID, &build.BatchID, &speculationPathJSON, &build.Score, &build.Status) |
| 34 | + |
| 35 | + if errors.Is(err, sql.ErrNoRows) { |
| 36 | + return entity.Build{}, storage.WrapNotFound(err) |
| 37 | + } |
| 38 | + if err != nil { |
| 39 | + return entity.Build{}, fmt.Errorf("failed to get build entity id=%s from the database: %w", id, err) |
| 40 | + } |
| 41 | + |
| 42 | + if err := json.Unmarshal(speculationPathJSON, &build.SpeculationPath); err != nil { |
| 43 | + return entity.Build{}, fmt.Errorf("failed to unmarshal speculation_path for build entity id=%s from the database: %w", id, err) |
| 44 | + } |
| 45 | + |
| 46 | + return build, nil |
| 47 | +} |
| 48 | + |
| 49 | +// Create creates a new build. The build must have a unique ID already assigned. Returns ErrAlreadyExists if the build ID already exists. |
| 50 | +func (s *buildStore) Create(ctx context.Context, build entity.Build) error { |
| 51 | + speculationPathJSON, err := json.Marshal(build.SpeculationPath) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("failed to marshal speculation_path id=%s for Create build entity: %w", build.ID, err) |
| 54 | + } |
| 55 | + |
| 56 | + _, err = s.db.ExecContext(ctx, |
| 57 | + "INSERT INTO build (id, batch_id, speculation_path, score, status) VALUES (?, ?, ?, ?, ?)", |
| 58 | + build.ID, build.BatchID, speculationPathJSON, build.Score, build.Status, |
| 59 | + ) |
| 60 | + if err != nil { |
| 61 | + var mysqlErr *mysql.MySQLError |
| 62 | + if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 { |
| 63 | + return fmt.Errorf("build entity id=%s: %w", build.ID, storage.ErrAlreadyExists) |
| 64 | + } |
| 65 | + return fmt.Errorf("failed to insert build entity id=%s: %w", build.ID, err) |
| 66 | + } |
| 67 | + |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +// UpdateStatus updates the status of a build. Returns ErrNotFound if the build is not found. |
| 72 | +func (s *buildStore) UpdateStatus(ctx context.Context, id string, newStatus entity.BuildStatus) error { |
| 73 | + result, err := s.db.ExecContext(ctx, |
| 74 | + "UPDATE build SET status = ? WHERE id = ?", |
| 75 | + newStatus, id, |
| 76 | + ) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("failed to update build status for id=%q newStatus=%v: %w", id, newStatus, err) |
| 79 | + } |
| 80 | + |
| 81 | + rowsAffected, err := result.RowsAffected() |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("failed to get rows affected from update for id=%q newStatus=%v: %w", id, newStatus, err) |
| 84 | + } |
| 85 | + |
| 86 | + if rowsAffected != 1 { |
| 87 | + return storage.WrapNotFound(fmt.Errorf("build entity id=%s", id)) |
| 88 | + } |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
0 commit comments