forked from fabric8-services/fabric8-wit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Random test failures because of work item creation error (fabric8-ser…
…vices#1676) Make sure that a sequence number initialized at `0` is created when the space itself is created, in order to allow for locking of the row in the `work_item_sequence_numbers` table. Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
- Loading branch information
Showing
4 changed files
with
69 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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,44 @@ | ||
package number | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jinzhu/gorm" | ||
errs "github.com/pkg/errors" | ||
uuid "github.com/satori/go.uuid" | ||
) | ||
|
||
// WorkItemNumberSequenceRepository the interface for the work item number sequence repository | ||
type WorkItemNumberSequenceRepository interface { | ||
NextVal(ctx context.Context, spaceID uuid.UUID) (*WorkItemNumberSequence, error) | ||
} | ||
|
||
// NewWorkItemSequenceNumberRepository creates a GormWorkItemNumberSequenceRepository | ||
func NewWorkItemSequenceNumberRepository(db *gorm.DB) *GormWorkItemNumberSequenceRepository { | ||
repository := &GormWorkItemNumberSequenceRepository{db} | ||
return repository | ||
} | ||
|
||
// GormWorkItemNumberSequenceRepository implements WorkItemNumberSequenceRepository using gorm | ||
type GormWorkItemNumberSequenceRepository struct { | ||
db *gorm.DB | ||
} | ||
|
||
// NextVal returns the next work item sequence number for the given space ID. Creates an entry in the DB if none was found before | ||
func (r *GormWorkItemNumberSequenceRepository) NextVal(ctx context.Context, spaceID uuid.UUID) (*WorkItemNumberSequence, error) { | ||
// retrieve the current issue number in the given space | ||
r.db.LogMode(true) | ||
defer r.db.LogMode(false) | ||
numberSequence := WorkItemNumberSequence{} | ||
tx := r.db.Model(&WorkItemNumberSequence{}).Set("gorm:query_option", "FOR UPDATE").Where("space_id = ?", spaceID).First(&numberSequence) | ||
if tx.RecordNotFound() { | ||
numberSequence.SpaceID = spaceID | ||
numberSequence.CurrentVal = 1 | ||
} else { | ||
numberSequence.CurrentVal++ | ||
} | ||
if err := r.db.Save(&numberSequence).Error; err != nil { | ||
return nil, errs.Wrapf(err, "failed to create work item with sequence number: `%s`", numberSequence.String()) | ||
} | ||
return &numberSequence, nil | ||
} |
This file contains 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