Skip to content

Commit afe04ea

Browse files
Merge pull request #35 from dipdup-io/fix/update-state
Fix: update state
2 parents c1d7411 + 77018e2 commit afe04ea

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

database/db_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const (
1818
testIndex = "test_index"
1919
)
2020

21-
func newDatabase(ctx context.Context, typ string, cfg config.Database) (Database, error) {
21+
func newDatabase(_ context.Context, typ string, _ config.Database) (Database, error) {
2222
switch typ {
2323
case "bun":
2424
return NewBun(), nil

database/state.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@ type State struct {
2222
CreatedAt int `gorm:"autoCreateTime" comment:"Created timestamp"`
2323
}
2424

25-
// BeforeInsert -
26-
func (s *State) BeforeInsert(ctx context.Context) (context.Context, error) {
27-
s.UpdatedAt = int(time.Now().Unix())
28-
s.CreatedAt = s.UpdatedAt
29-
return ctx, nil
30-
}
31-
32-
// BeforeUpdate -
33-
func (s *State) BeforeUpdate(ctx context.Context) (context.Context, error) {
34-
s.UpdatedAt = int(time.Now().Unix())
35-
return ctx, nil
25+
var _ bun.BeforeAppendModelHook = (*State)(nil)
26+
27+
func (s *State) BeforeAppendModel(ctx context.Context, query bun.Query) error {
28+
switch query.(type) {
29+
case *bun.InsertQuery:
30+
s.UpdatedAt = int(time.Now().Unix())
31+
s.CreatedAt = s.UpdatedAt
32+
33+
case *bun.UpdateQuery:
34+
s.UpdatedAt = int(time.Now().Unix())
35+
}
36+
return nil
3637
}
3738

3839
// TableName -

0 commit comments

Comments
 (0)