Skip to content

Commit

Permalink
converts uint64 id's to uuidV7 (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonytheleg authored Nov 11, 2024
1 parent f785a01 commit 39dae37
Show file tree
Hide file tree
Showing 20 changed files with 290 additions and 133 deletions.
20 changes: 18 additions & 2 deletions internal/biz/model/localinventorytoresource.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package model

import "time"
import (
"fmt"
"github.com/google/uuid"
"gorm.io/gorm"
"time"
)

type LocalInventoryToResource struct {
// Our local resource id
ResourceId uint64 `gorm:"primarykey"`
ResourceId uuid.UUID `gorm:"type:uuid;primarykey"`
CreatedAt *time.Time `gorm:"index"`

ReporterResourceId
Expand Down Expand Up @@ -60,3 +65,14 @@ func ReporterRelationshipIdFromRelationship(relationship *Relationship) Reporter
},
}
}

func (r *LocalInventoryToResource) BeforeCreate(db *gorm.DB) error {
var err error
if r.ResourceId == uuid.Nil {
r.ResourceId, err = uuid.NewV7()
if err != nil {
return fmt.Errorf("failed to generate uuid: %w", err)
}
}
return nil
}
28 changes: 22 additions & 6 deletions internal/biz/model/relationshiphistory.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
package model

import "time"
import (
"fmt"
"github.com/google/uuid"
"gorm.io/gorm"
"time"
)

type RelationshipHistory struct {
ID uint64 `gorm:"primarykey"`
OrgId string `gorm:"index"`
ID uuid.UUID `gorm:"type:uuid;primarykey"`
OrgId string `gorm:"index"`
RelationshipData JsonObject
RelationshipType string
SubjectId uint64 `gorm:"index"`
ObjectId uint64 `gorm:"index"`
SubjectId uuid.UUID `gorm:"type:uuid;index"`
ObjectId uuid.UUID `gorm:"type:uuid;index"`
Reporter RelationshipReporter
Timestamp *time.Time `gorm:"autoCreateTime"`

RelationshipId uint64 `gorm:"index"`
RelationshipId uuid.UUID `gorm:"type:uuid;index"`
OperationType OperationType `gorm:"index"`
}

func (*RelationshipHistory) TableName() string {
return "relationship_history"
}

func (r *RelationshipHistory) BeforeCreate(db *gorm.DB) error {
var err error
if r.ID == uuid.Nil {
r.ID, err = uuid.NewV7()
if err != nil {
return fmt.Errorf("failed to generate uuid: %w", err)
}
}
return nil
}
21 changes: 17 additions & 4 deletions internal/biz/model/relationships.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ package model
import (
"database/sql/driver"
"encoding/json"
"fmt"
"github.com/google/uuid"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"time"
)

type Relationship struct {
ID uint64 `gorm:"primarykey"`
OrgId string `gorm:"index"`
ID uuid.UUID `gorm:"type:uuid;primarykey"`
OrgId string `gorm:"index"`
RelationshipData JsonObject
RelationshipType string
SubjectId uint64 `gorm:"index;not null"`
ObjectId uint64 `gorm:"index;not null"`
SubjectId uuid.UUID `gorm:"type:uuid;index;not null"`
ObjectId uuid.UUID `gorm:"type:uuid;index;not null"`
Reporter RelationshipReporter
CreatedAt *time.Time
UpdatedAt *time.Time
Expand Down Expand Up @@ -43,3 +45,14 @@ func (data RelationshipReporter) Value() (driver.Value, error) {
func (data *RelationshipReporter) Scan(value interface{}) error {
return Scan(value, data)
}

func (r *Relationship) BeforeCreate(db *gorm.DB) error {
var err error
if r.ID == uuid.Nil {
r.ID, err = uuid.NewV7()
if err != nil {
return fmt.Errorf("failed to generate resource uuid: %w", err)
}
}
return nil
}
18 changes: 15 additions & 3 deletions internal/biz/model/resourcehistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package model

import (
"fmt"
"github.com/google/uuid"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"time"
)

type ResourceHistory struct {
ID uint64 `gorm:"primarykey"`
OrgId string `gorm:"index"`
ID uuid.UUID `gorm:"type:uuid;primarykey"`
OrgId string `gorm:"index"`
ResourceData JsonObject
ResourceType string
WorkspaceId string
Expand All @@ -19,7 +20,7 @@ type ResourceHistory struct {
Labels Labels
Timestamp *time.Time `gorm:"autoCreateTime"`

ResourceId uint64 `gorm:"index"`
ResourceId uuid.UUID `gorm:"type:uuid;index"`
OperationType OperationType `gorm:"index"`
}

Expand All @@ -40,3 +41,14 @@ func (r *ResourceHistory) ResourceHistory(db *gorm.DB, s *schema.Schema) error {
func (*ResourceHistory) TableName() string {
return "resource_history"
}

func (r *ResourceHistory) BeforeCreate(db *gorm.DB) error {
var err error
if r.ID == uuid.Nil {
r.ID, err = uuid.NewV7()
if err != nil {
return fmt.Errorf("failed to generate uuid: %w", err)
}
}
return nil
}
16 changes: 14 additions & 2 deletions internal/biz/model/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"database/sql/driver"
"encoding/json"
"fmt"
"github.com/google/uuid"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"time"
)

type Resource struct {
ID uint64 `gorm:"primarykey"`
OrgId string `gorm:"index"`
ID uuid.UUID `gorm:"type:uuid;primarykey"`
OrgId string `gorm:"index"`
ResourceData JsonObject
ResourceType string
WorkspaceId string
Expand Down Expand Up @@ -53,3 +54,14 @@ func (data ResourceReporter) Value() (driver.Value, error) {
func (data *ResourceReporter) Scan(value interface{}) error {
return Scan(value, data)
}

func (r *Resource) BeforeCreate(db *gorm.DB) error {
var err error
if r.ID == uuid.Nil {
r.ID, err = uuid.NewV7()
if err != nil {
return fmt.Errorf("failed to generate uuid: %w", err)
}
}
return nil
}
11 changes: 6 additions & 5 deletions internal/biz/relationships/relationships.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"github.com/go-kratos/kratos/v2/log"
"github.com/google/uuid"
"github.com/project-kessel/inventory-api/internal/biz"
"github.com/project-kessel/inventory-api/internal/biz/model"
eventingapi "github.com/project-kessel/inventory-api/internal/eventing/api"
Expand All @@ -13,11 +14,11 @@ import (

type ResourceRepository interface {
Save(ctx context.Context, resource *model.Relationship) (*model.Relationship, error)
Update(context.Context, *model.Relationship, uint64) (*model.Relationship, error)
Delete(context.Context, uint64) (*model.Relationship, error)
FindByID(context.Context, uint64) (*model.Relationship, error)
FindRelationship(ctx context.Context, subjectId, objectId uint64, relationshipType string) (*model.Relationship, error)
FindResourceIdByReporterResourceId(ctx context.Context, id model.ReporterResourceId) (uint64, error)
Update(context.Context, *model.Relationship, uuid.UUID) (*model.Relationship, error)
Delete(context.Context, uuid.UUID) (*model.Relationship, error)
FindByID(context.Context, uuid.UUID) (*model.Relationship, error)
FindRelationship(ctx context.Context, subjectId, objectId uuid.UUID, relationshipType string) (*model.Relationship, error)
FindResourceIdByReporterResourceId(ctx context.Context, id model.ReporterResourceId) (uuid.UUID, error)
ListAll(context.Context) ([]*model.Relationship, error)
}

Expand Down
Loading

0 comments on commit 39dae37

Please sign in to comment.