Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
931 additions
and
490 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
Oops, something went wrong.