Skip to content

Commit

Permalink
v2
Browse files Browse the repository at this point in the history
  • Loading branch information
vcraescu committed Oct 11, 2020
1 parent d56de03 commit 6d8d677
Show file tree
Hide file tree
Showing 9 changed files with 931 additions and 490 deletions.
8 changes: 0 additions & 8 deletions export_test.go

This file was deleted.

5 changes: 4 additions & 1 deletion go.mod
Expand Up @@ -3,9 +3,12 @@ module github.com/vcraescu/gorm-history
go 1.13

require (
github.com/davecgh/go-spew v1.1.1
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a
github.com/jinzhu/gorm v1.9.12
github.com/oklog/ulid/v2 v2.0.2
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.5.1
gopkg.in/yaml.v2 v2.2.8 // indirect
gorm.io/driver/sqlite v1.1.3
gorm.io/gorm v1.20.1
)
12 changes: 12 additions & 0 deletions go.sum
Expand Up @@ -19,10 +19,18 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.14.3 h1:j7a/xn1U6TKA/PHHxqZuzh64CdtRc7rU9M+AvkOl5bA=
github.com/mattn/go-sqlite3 v1.14.3/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw=
github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid/v2 v2.0.2 h1:r4fFzBm+bv0wNKNh5eXTwU7i85y5x+uwkxCUTNVQqLc=
github.com/oklog/ulid/v2 v2.0.2/go.mod h1:mtBL0Qe/0HAx6/a4Z30qxVIAL1eQDweXq5lxOEiwQ68=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
Expand All @@ -48,3 +56,7 @@ gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gorm.io/driver/sqlite v1.1.3 h1:BYfdVuZB5He/u9dt4qDpZqiqDJ6KhPqs5QUqsr/Eeuc=
gorm.io/driver/sqlite v1.1.3/go.mod h1:AKDgRWk8lcSQSw+9kxCJnX/yySj8G3rdwYlU57cB45c=
gorm.io/gorm v1.20.1 h1:+hOwlHDqvqmBIMflemMVPLJH7tZYK4RxFDBHEfJTup0=
gorm.io/gorm v1.20.1/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
98 changes: 78 additions & 20 deletions history.go
@@ -1,37 +1,95 @@
package history

type Action string
import (
"context"
"fmt"
"gorm.io/gorm"
"time"
)

const (
ActionCreate Action = "create"
ActionUpdate Action = "update"
ActionDelete Action = "delete"
ActionCreate Action = "create"
ActionUpdate Action = "update"
ActionDelete Action = "delete"
actorOptionKey actorOptionCtxKey = pluginName + ":actor"
)

var (
_ History = (*Entry)(nil)
_ TimestampableHistory = (*Entry)(nil)
_ BlameableHistory = (*Entry)(nil)
)

type (
Version string

Action string

actorOptionCtxKey string

Recordable interface {
CreateHistory() History
}

TimestampableHistory interface {
SetHistoryCreatedAt(createdAt time.Time)
}

BlameableHistory interface {
SetHistoryActor(actor string)
}

History interface {
SetHistoryVersion(version Version)
SetHistoryObjectID(id interface{})
SetHistoryAction(action Action)
}

Entry struct {
Version Version `gorm:"type:char(26)"`
ObjectID string `gorm:"index"`
Action Action `gorm:"type:varchar(24)"`
Actor string `gorm:"type:varchar(128)"`
CreatedAt time.Time `gorm:"type:datetime"`
}
)

type Recordable interface {
CreateHistory() interface{}
func SetActor(db *gorm.DB, actor string) *gorm.DB {
ctx := context.WithValue(db.Statement.Context, actorOptionKey, actor)

return db.WithContext(ctx).Set(string(actorOptionKey), actor)
}

func GetActor(db *gorm.DB) (string, bool) {
value, ok := db.Get(string(actorOptionKey))
if !ok {
value := db.Statement.Context.Value(actorOptionKey)
actor, ok := value.(string)

return actor, ok
}

actor, ok := value.(string)

return actor, ok
}

type History interface {
SetHistoryVersion(version Version)
SetHistoryObjectID(id interface{})
SetHistoryAction(action Action)
func (e *Entry) SetHistoryVersion(version Version) {
e.Version = version
}

type Entry struct {
Version Version `gorm-history:"version"`
ObjectID uint `gorm:"index" gorm-history:"objectID"`
Action Action `gorm:"type: string" gorm-history:"action"`
func (e *Entry) SetHistoryObjectID(id interface{}) {
e.ObjectID = fmt.Sprintf("%v", id)
}

func (l *Entry) SetHistoryVersion(version Version) {
l.Version = version
func (e *Entry) SetHistoryAction(action Action) {
e.Action = action
}

func (l *Entry) SetHistoryObjectID(id interface{}) {
l.ObjectID = id.(uint)
func (e *Entry) SetHistoryActor(actor string) {
e.Actor = actor
}

func (l *Entry) SetHistoryAction(action Action) {
l.Action = action
func (e *Entry) SetHistoryCreatedAt(createdAt time.Time) {
e.CreatedAt = createdAt
}
34 changes: 34 additions & 0 deletions history_test.go
@@ -0,0 +1,34 @@
package history_test

import (
"context"
"github.com/stretchr/testify/require"
history "github.com/vcraescu/gorm-history"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"testing"
)

func TestSetActor(t *testing.T) {
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
panic(err)
}

db = history.SetActor(db, "foobar")
actor, ok := history.GetActor(db)
require.True(t, ok)
require.Equal(t, "foobar", actor)

db = db.Session(&gorm.Session{})
actor, ok = history.GetActor(db)
require.True(t, ok)

require.Equal(t, "foobar", actor)

db = db.WithContext(context.Background())
actor, ok = history.GetActor(db)
require.True(t, ok)

require.Equal(t, "foobar", actor)
}

0 comments on commit 6d8d677

Please sign in to comment.