-
Notifications
You must be signed in to change notification settings - Fork 41
fix: make work item IDs incremental #71
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ package store | |||||
|
|
||||||
| import ( | ||||||
| "context" | ||||||
| "encoding/binary" | ||||||
|
|
||||||
| "github.com/Devlaner/devlane/api/internal/model" | ||||||
| "github.com/google/uuid" | ||||||
|
|
@@ -17,6 +18,29 @@ func (s *IssueStore) Create(ctx context.Context, i *model.Issue) error { | |||||
| return s.db.WithContext(ctx).Create(i).Error | ||||||
| } | ||||||
|
|
||||||
| // Transaction runs fn inside a DB transaction (same connection). | ||||||
| func (s *IssueStore) Transaction(ctx context.Context, fn func(tx *gorm.DB) error) error { | ||||||
| return s.db.WithContext(ctx).Transaction(fn) | ||||||
| } | ||||||
|
|
||||||
| // NextSequenceID returns the next per-project issue number (1-based), serialized with an advisory lock. | ||||||
| func (s *IssueStore) NextSequenceID(ctx context.Context, tx *gorm.DB, projectID uuid.UUID) (int, error) { | ||||||
| k1 := int32(binary.BigEndian.Uint32(projectID[0:4])) | ||||||
| k2 := int32(binary.BigEndian.Uint32(projectID[4:8])) | ||||||
| if err := tx.Exec("SELECT pg_advisory_xact_lock(?, ?)", k1, k2).Error; err != nil { | ||||||
| return 0, err | ||||||
| } | ||||||
| var max int | ||||||
| err := tx.WithContext(ctx).Raw( | ||||||
| `SELECT COALESCE(MAX(sequence_id), 0) FROM issues WHERE project_id = ? AND deleted_at IS NULL`, | ||||||
|
||||||
| `SELECT COALESCE(MAX(sequence_id), 0) FROM issues WHERE project_id = ? AND deleted_at IS NULL`, | |
| `SELECT COALESCE(MAX(sequence_id), 0) FROM issues WHERE project_id = ?`, |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new transaction only covers sequence assignment + the
issuesinsert; assignee/label mutations happen after the transaction and their errors are ignored, so a partially-created issue can be returned without its requested relationships and the sequence number can be consumed even though the overall create request effectively failed. To match the stated atomicity goal, perform relationship writes inside the same transaction and propagate any errors (returning a failure so the whole create rolls back).