Skip to content

Commit

Permalink
Restore hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed May 7, 2021
1 parent 656760c commit 32916a9
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 195 deletions.
50 changes: 24 additions & 26 deletions bun.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package bun

import (
"context"

"github.com/uptrace/bun/schema"
"github.com/uptrace/bun/sqlfmt"
)
Expand All @@ -28,36 +26,36 @@ type (
AfterDeleteHook = schema.AfterDeleteHook
)

type BeforeSelectQueryHook interface {
BeforeSelectQuery(ctx context.Context, query *SelectQuery) error
}
// type BeforeSelectQueryHook interface {
// BeforeSelectQuery(ctx context.Context, query *SelectQuery) error
// }

type AfterSelectQueryHook interface {
AfterSelectQuery(ctx context.Context, query *SelectQuery) error
}
// type AfterSelectQueryHook interface {
// AfterSelectQuery(ctx context.Context, query *SelectQuery) error
// }

type BeforeInsertQueryHook interface {
BeforeInsertQuery(ctx context.Context, query *InsertQuery) error
}
// type BeforeInsertQueryHook interface {
// BeforeInsertQuery(ctx context.Context, query *InsertQuery) error
// }

type AfterInsertQueryHook interface {
AfterInsertQuery(ctx context.Context, query *InsertQuery) error
}
// type AfterInsertQueryHook interface {
// AfterInsertQuery(ctx context.Context, query *InsertQuery) error
// }

type BeforeUpdateQueryHook interface {
BeforeUpdateQuery(ctx context.Context, query *UpdateQuery) error
}
// type BeforeUpdateQueryHook interface {
// BeforeUpdateQuery(ctx context.Context, query *UpdateQuery) error
// }

type AfterUpdateQueryHook interface {
AfterUpdateQuery(ctx context.Context, query *UpdateQuery) error
}
// type AfterUpdateQueryHook interface {
// AfterUpdateQuery(ctx context.Context, query *UpdateQuery) error
// }

type BeforeDeleteQueryHook interface {
BeforeDeleteQuery(ctx context.Context, query *DeleteQuery) error
}
// type BeforeDeleteQueryHook interface {
// BeforeDeleteQuery(ctx context.Context, query *DeleteQuery) error
// }

type AfterDeleteQueryHook interface {
AfterDeleteQuery(ctx context.Context, query *DeleteQuery) error
}
// type AfterDeleteQueryHook interface {
// AfterDeleteQuery(ctx context.Context, query *DeleteQuery) error
// }

type BaseTable struct{}
164 changes: 81 additions & 83 deletions internal/dbtest/model_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,17 @@ func testModelHook(t *testing.T, db *bun.DB) {
hook := &ModelHookTest{ID: 1}
_, err := db.NewInsert().Model(hook).Exec(ctx)
require.NoError(t, err)
require.Equal(t, []string{"BeforeInsertQuery", "AfterInsertQuery"}, events.Flush())
require.Equal(t, []string{"BeforeInsert", "AfterInsert"}, events.Flush())
}

{
hook := new(ModelHookTest)
err := db.NewSelect().Model(hook).Scan(ctx)
require.NoError(t, err)
require.Equal(t, []string{
"BeforeSelectQuery",
"BeforeScan",
"AfterScan",
"AfterSelectQuery",
"AfterSelect",
}, events.Flush())
}

Expand All @@ -73,31 +72,30 @@ func testModelHook(t *testing.T, db *bun.DB) {
err := db.NewSelect().Model(&hooks).Scan(ctx)
require.NoError(t, err)
require.Equal(t, []string{
"BeforeSelectQuery",
"BeforeScan",
"AfterScan",
"AfterSelectQuery",
"AfterSelect",
}, events.Flush())
}

{
hook := &ModelHookTest{ID: 1}
_, err := db.NewUpdate().Model(hook).WherePK().Exec(ctx)
require.NoError(t, err)
require.Equal(t, []string{"BeforeUpdateQuery", "AfterUpdateQuery"}, events.Flush())
require.Equal(t, []string{"BeforeUpdate", "AfterUpdate"}, events.Flush())
}

{
hook := &ModelHookTest{ID: 1}
_, err := db.NewDelete().Model(hook).WherePK().Exec(ctx)
require.NoError(t, err)
require.Equal(t, []string{"BeforeDeleteQuery", "AfterDeleteQuery"}, events.Flush())
require.Equal(t, []string{"BeforeDelete", "AfterDelete"}, events.Flush())
}

{
_, err := db.NewDelete().Model((*ModelHookTest)(nil)).Where("TRUE").Exec(ctx)
require.NoError(t, err)
require.Equal(t, []string{"BeforeDeleteQuery", "AfterDeleteQuery"}, events.Flush())
require.Nil(t, events.Flush())
}
}

Expand All @@ -120,107 +118,107 @@ func (t *ModelHookTest) AfterScan(c context.Context) error {
return nil
}

var _ bun.BeforeSelectQueryHook = (*ModelHookTest)(nil)
// var _ bun.BeforeSelectQueryHook = (*ModelHookTest)(nil)

func (t *ModelHookTest) BeforeSelectQuery(ctx context.Context, query *bun.SelectQuery) error {
events.Add("BeforeSelectQuery")
return nil
}

var _ bun.AfterSelectQueryHook = (*ModelHookTest)(nil)

func (t *ModelHookTest) AfterSelectQuery(ctx context.Context, query *bun.SelectQuery) error {
events.Add("AfterSelectQuery")
return nil
}
// func (t *ModelHookTest) BeforeSelectQuery(ctx context.Context, query *bun.SelectQuery) error {
// events.Add("BeforeSelectQuery")
// return nil
// }

var _ bun.BeforeUpdateQueryHook = (*ModelHookTest)(nil)
// var _ bun.AfterSelectQueryHook = (*ModelHookTest)(nil)

func (t *ModelHookTest) BeforeUpdateQuery(ctx context.Context, query *bun.UpdateQuery) error {
events.Add("BeforeUpdateQuery")
return nil
}
// func (t *ModelHookTest) AfterSelectQuery(ctx context.Context, query *bun.SelectQuery) error {
// events.Add("AfterSelectQuery")
// return nil
// }

var _ bun.AfterUpdateQueryHook = (*ModelHookTest)(nil)
// var _ bun.BeforeUpdateQueryHook = (*ModelHookTest)(nil)

func (t *ModelHookTest) AfterUpdateQuery(ctx context.Context, query *bun.UpdateQuery) error {
events.Add("AfterUpdateQuery")
return nil
}
// func (t *ModelHookTest) BeforeUpdateQuery(ctx context.Context, query *bun.UpdateQuery) error {
// events.Add("BeforeUpdateQuery")
// return nil
// }

var _ bun.BeforeInsertQueryHook = (*ModelHookTest)(nil)
// var _ bun.AfterUpdateQueryHook = (*ModelHookTest)(nil)

func (t *ModelHookTest) BeforeInsertQuery(ctx context.Context, query *bun.InsertQuery) error {
events.Add("BeforeInsertQuery")
return nil
}
// func (t *ModelHookTest) AfterUpdateQuery(ctx context.Context, query *bun.UpdateQuery) error {
// events.Add("AfterUpdateQuery")
// return nil
// }

var _ bun.AfterInsertQueryHook = (*ModelHookTest)(nil)
// var _ bun.BeforeInsertQueryHook = (*ModelHookTest)(nil)

func (t *ModelHookTest) AfterInsertQuery(ctx context.Context, query *bun.InsertQuery) error {
events.Add("AfterInsertQuery")
return nil
}
// func (t *ModelHookTest) BeforeInsertQuery(ctx context.Context, query *bun.InsertQuery) error {
// events.Add("BeforeInsertQuery")
// return nil
// }

var _ bun.BeforeDeleteQueryHook = (*ModelHookTest)(nil)
// var _ bun.AfterInsertQueryHook = (*ModelHookTest)(nil)

func (t *ModelHookTest) BeforeDeleteQuery(ctx context.Context, query *bun.DeleteQuery) error {
events.Add("BeforeDeleteQuery")
return nil
}
// func (t *ModelHookTest) AfterInsertQuery(ctx context.Context, query *bun.InsertQuery) error {
// events.Add("AfterInsertQuery")
// return nil
// }

var _ bun.AfterDeleteQueryHook = (*ModelHookTest)(nil)
// var _ bun.BeforeDeleteQueryHook = (*ModelHookTest)(nil)

func (t *ModelHookTest) AfterDeleteQuery(ctx context.Context, query *bun.DeleteQuery) error {
events.Add("AfterDeleteQuery")
return nil
}
// func (t *ModelHookTest) BeforeDeleteQuery(ctx context.Context, query *bun.DeleteQuery) error {
// events.Add("BeforeDeleteQuery")
// return nil
// }

// var _ bun.AfterSelectHook = (*ModelHookTest)(nil)
// var _ bun.AfterDeleteQueryHook = (*ModelHookTest)(nil)

// func (t *ModelHookTest) AfterSelect(c context.Context) error {
// t.events = append(t.events, "AfterSelect")
// func (t *ModelHookTest) AfterDeleteQuery(ctx context.Context, query *bun.DeleteQuery) error {
// events.Add("AfterDeleteQuery")
// return nil
// }

// var _ bun.BeforeInsertHook = (*ModelHookTest)(nil)
var _ bun.AfterSelectHook = (*ModelHookTest)(nil)

// func (t *ModelHookTest) BeforeInsert(c context.Context) (context.Context, error) {
// t.events = append(t.events, "BeforeInsert")
// return c, nil
// }
func (t *ModelHookTest) AfterSelect(c context.Context) error {
events.Add("AfterSelect")
return nil
}

// var _ bun.AfterInsertHook = (*ModelHookTest)(nil)
var _ bun.BeforeInsertHook = (*ModelHookTest)(nil)

// func (t *ModelHookTest) AfterInsert(c context.Context) error {
// t.events = append(t.events, "AfterInsert")
// return nil
// }
func (t *ModelHookTest) BeforeInsert(ctx context.Context) error {
events.Add("BeforeInsert")
return nil
}

// var _ bun.BeforeUpdateHook = (*ModelHookTest)(nil)
var _ bun.AfterInsertHook = (*ModelHookTest)(nil)

// func (t *ModelHookTest) BeforeUpdate(c context.Context) (context.Context, error) {
// t.events = append(t.events, "BeforeUpdate")
// return c, nil
// }
func (t *ModelHookTest) AfterInsert(c context.Context) error {
events.Add("AfterInsert")
return nil
}

// var _ bun.AfterUpdateHook = (*ModelHookTest)(nil)
var _ bun.BeforeUpdateHook = (*ModelHookTest)(nil)

// func (t *ModelHookTest) AfterUpdate(c context.Context) error {
// t.events = append(t.events, "AfterUpdate")
// return nil
// }
func (t *ModelHookTest) BeforeUpdate(ctx context.Context) error {
events.Add("BeforeUpdate")
return nil
}

// var _ bun.BeforeDeleteHook = (*ModelHookTest)(nil)
var _ bun.AfterUpdateHook = (*ModelHookTest)(nil)

// func (t *ModelHookTest) BeforeDelete(c context.Context) (context.Context, error) {
// t.events = append(t.events, "BeforeDelete")
// return c, nil
// }
func (t *ModelHookTest) AfterUpdate(c context.Context) error {
events.Add("AfterUpdate")
return nil
}

// var _ bun.AfterDeleteHook = (*ModelHookTest)(nil)
var _ bun.BeforeDeleteHook = (*ModelHookTest)(nil)

// func (t *ModelHookTest) AfterDelete(c context.Context) error {
// t.events = append(t.events, "AfterDelete")
// return nil
// }
func (t *ModelHookTest) BeforeDelete(ctx context.Context) error {
events.Add("BeforeDelete")
return nil
}

var _ bun.AfterDeleteHook = (*ModelHookTest)(nil)

func (t *ModelHookTest) AfterDelete(c context.Context) error {
events.Add("AfterDelete")
return nil
}
Loading

0 comments on commit 32916a9

Please sign in to comment.