Skip to content

Commit

Permalink
Fix: update state
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Jul 19, 2024
1 parent cb135f8 commit 77018e2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion database/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
testIndex = "test_index"
)

func newDatabase(ctx context.Context, typ string, cfg config.Database) (Database, error) {
func newDatabase(_ context.Context, typ string, _ config.Database) (Database, error) {
switch typ {
case "bun":
return NewBun(), nil
Expand Down
23 changes: 12 additions & 11 deletions database/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ type State struct {
CreatedAt int `gorm:"autoCreateTime" comment:"Created timestamp"`
}

// BeforeInsert -
func (s *State) BeforeInsert(ctx context.Context) (context.Context, error) {
s.UpdatedAt = int(time.Now().Unix())
s.CreatedAt = s.UpdatedAt
return ctx, nil
}

// BeforeUpdate -
func (s *State) BeforeUpdate(ctx context.Context) (context.Context, error) {
s.UpdatedAt = int(time.Now().Unix())
return ctx, nil
var _ bun.BeforeAppendModelHook = (*State)(nil)

func (s *State) BeforeAppendModel(ctx context.Context, query bun.Query) error {
switch query.(type) {
case *bun.InsertQuery:
s.UpdatedAt = int(time.Now().Unix())
s.CreatedAt = s.UpdatedAt

case *bun.UpdateQuery:
s.UpdatedAt = int(time.Now().Unix())
}
return nil
}

// TableName -
Expand Down

0 comments on commit 77018e2

Please sign in to comment.