Skip to content

Fix: update state #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading