Skip to content

Commit

Permalink
Handle uint64 primary key.
Browse files Browse the repository at this point in the history
  • Loading branch information
raven-chen committed Aug 5, 2021
1 parent bfbb16e commit fbd8df4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
15 changes: 14 additions & 1 deletion resource/meta.go
Expand Up @@ -293,8 +293,21 @@ func switchRecordToNewVersionIfNeeded(context *qor.Context, record interface{})
recordValue = recordValue.Elem()
}

// Handle situation when the primary key is a uint64 not general uint
var id uint64
idUint, ok := recordValue.FieldByName("ID").Interface().(uint)
if !ok {
id64, ok := recordValue.FieldByName("ID").Interface().(uint64)
if !ok {
panic("ID filed must be uint or uint64")
}
id = id64
} else {
id = uint64(idUint)
}

// if currentVersionName is blank, we consider it is creating a new version
if recordValue.FieldByName("ID").Interface().(uint) != 0 && currentVersionName == "" {
if id != 0 && currentVersionName == "" {
arguments := []reflect.Value{reflect.ValueOf(context.GetDB())}

// Handle the situation when record is NOT a pointer
Expand Down
56 changes: 55 additions & 1 deletion resource/meta_test.go
Expand Up @@ -293,13 +293,39 @@ type CollectionWithVersion struct {
Manager Manager
}

type CollectionWithVersionAndUint64PrimaryKey struct {
ID uint64 `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`

publish2.Version
publish2.Schedule

Name string

Products []ProductWithVersion `gorm:"many2many:collection_with_version_product_with_versions;association_autoupdate:false"`
ProductsSorter sorting.SortableCollection

ManagerID uint
ManagerVersionName string
Manager Manager
}

func (coll *CollectionWithVersion) AssignVersionName(db *gorm.DB) {
var count int
name := time.Now().Format("2006-01-02")
db.Model(&CollectionWithVersion{}).Where("id = ? AND version_name like ?", coll.ID, name+"%").Count(&count)
coll.VersionName = fmt.Sprintf("%s-v%v", name, count+1)
}

func (coll *CollectionWithVersionAndUint64PrimaryKey) AssignVersionName(db *gorm.DB) {
var count int
name := time.Now().Format("2006-01-02")
db.Model(&CollectionWithVersionAndUint64PrimaryKey{}).Where("id = ? AND version_name like ?", coll.ID, name+"%").Count(&count)
coll.VersionName = fmt.Sprintf("%s-v%v", name, count+1)
}

type ProductWithVersion struct {
gorm.Model

Expand Down Expand Up @@ -867,7 +893,17 @@ func registerVersionNameCallback(db *gorm.DB) {
name := time.Now().Format("2006-01-02")

idField, _ := scope.FieldByName("ID")
id := idField.Field.Interface().(uint)
var id uint64
idUint, ok := idField.Field.Interface().(uint)
if !ok {
id64, ok := idField.Field.Interface().(uint64)
if !ok {
panic("ID filed must be uint or uint64")
}
id = id64
} else {
id = uint64(idUint)
}

var count int
scope.DB().Table(scope.TableName()).Unscoped().Scopes(WithoutVersion).Where("id = ? AND version_name like ?", id, name+"%").Count(&count)
Expand Down Expand Up @@ -944,6 +980,24 @@ func TestSwitchRecordToNewVersionIfNeeded(t *testing.T) {
t.Error("new version name is not assigned to record")
}
}

func TestSwitchRecordToNewVersionIfNeeded_Uint64ID(t *testing.T) {
db := testutils.GetTestDB()
testutils.ResetDBTables(db, &CollectionWithVersionAndUint64PrimaryKey{})
registerVersionNameCallback(db)

record := CollectionWithVersionAndUint64PrimaryKey{Name: "test"}
testutils.AssertNoErr(t, db.Save(&record).Error)
oldVersionName := record.VersionName

formValues := map[string][]string{"QorResource.VersionName": {}}
ctx := &qor.Context{DB: db, Request: &http.Request{Form: formValues}}

newRecord := resource.SwitchRecordToNewVersionIfNeeded(ctx, record)
if newRecord.(CollectionWithVersionAndUint64PrimaryKey).VersionName == oldVersionName {
t.Error("new version name is not assigned to record")
}
}
func TestSwitchRecordToNewVersionIfNeeded_EditExistingVersion(t *testing.T) {
db := testutils.GetTestDB()
testutils.ResetDBTables(db, &CollectionWithVersion{})
Expand Down

0 comments on commit fbd8df4

Please sign in to comment.