Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
Switch to ansi sql JSON types, and remove uuid sql type (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shelnutt2 authored and sas1024 committed Nov 20, 2018
1 parent 6d2e41f commit 8eec541
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 72 deletions.
8 changes: 4 additions & 4 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package loggable
import (
"encoding/json"

"github.com/jinzhu/gorm"
"github.com/gofrs/uuid"
"github.com/jinzhu/gorm"
)

func (p *Plugin) addCreated(scope *gorm.Scope) {
Expand Down Expand Up @@ -43,12 +43,12 @@ func addRecord(scope *gorm.Scope, action string) error {
return err
}
cl := ChangeLog{
ID: id.String(),
ID: id,
Action: action,
ObjectID: interfaceToString(scope.PrimaryKeyValue()),
ObjectType: scope.GetModelStruct().ModelType.Name(),
RawObject: rawObject,
RawMeta: fetchChangeLogMeta(scope),
RawObject: string(rawObject),
RawMeta: string(fetchChangeLogMeta(scope)),
}
return scope.DB().Create(&cl).Error
}
60 changes: 0 additions & 60 deletions jsonb.go

This file was deleted.

17 changes: 11 additions & 6 deletions loggable.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"time"

"github.com/gofrs/uuid"
"github.com/jinzhu/gorm"
)

Expand Down Expand Up @@ -34,24 +35,28 @@ func (l LoggableModel) Enable(v bool) { l.Disabled = !v }

// ChangeLog is a main entity, which used to log changes.
type ChangeLog struct {
ID string `gorm:"type:uuid;primary_key;"`
ID uuid.UUID `gorm:"primary_key;"`
CreatedAt time.Time `sql:"DEFAULT:current_timestamp"`
Action string
ObjectID string `gorm:"index"`
ObjectType string `gorm:"index"`
RawObject JSONB `sql:"type:JSONB"`
RawMeta JSONB `sql:"type:JSONB"`
RawObject string `sql:"type:JSON"`
RawMeta string `sql:"type:JSON"`
Object interface{} `sql:"-"`
Meta interface{} `sql:"-"`
}

func (l *ChangeLog) prepareObject(objType reflect.Type) (err error) {
l.Object, err = l.RawObject.unmarshal(objType)
obj := reflect.New(objType).Interface()
err = json.Unmarshal([]byte(l.RawObject), obj)
l.Object = obj
return
}

func (l *ChangeLog) prepareMeta(objType reflect.Type) (err error) {
l.Meta, err = l.RawMeta.unmarshal(objType)
obj := reflect.New(objType).Interface()
err = json.Unmarshal([]byte(l.RawMeta), obj)
l.Meta = obj
return
}

Expand All @@ -64,7 +69,7 @@ func interfaceToString(v interface{}) string {
}
}

func fetchChangeLogMeta(scope *gorm.Scope) JSONB {
func fetchChangeLogMeta(scope *gorm.Scope) []byte {
val, ok := scope.Value.(Interface)
if !ok {
return nil
Expand Down
2 changes: 0 additions & 2 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ func somethingToMapStringInterface(item interface{}) map[string]interface{} {
return nil
}
switch raw := item.(type) {
case JSONB:
return somethingToMapStringInterface([]byte(raw))
case string:
return somethingToMapStringInterface([]byte(raw))
case []byte:
Expand Down

0 comments on commit 8eec541

Please sign in to comment.