-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
39 lines (36 loc) · 965 Bytes
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package db
import (
"context"
"fmt"
"log"
"time"
"github.com/eriner/burr/internal/ent"
_ "github.com/eriner/burr/internal/ent/runtime"
"github.com/eriner/burr/internal/policy"
)
// initDB sets runtime hooks, runs migrations, and other #dbthings
func initDB(c *ent.Client) error {
if c == nil {
return fmt.Errorf("nil client")
}
if err := c.Schema.Create(context.Background()); err != nil {
return fmt.Errorf("failed creating schema: %w", err)
}
// Global runtime logging hook
c.Use(func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
start := time.Now()
defer func() {
var name string
if actor := policy.ActorFromContext(ctx); actor != nil {
name = actor.Name
}
log.Printf("Op=%s\tType=%s\tTime=%s\tConcreteType=%T\tActor=%s\n",
m.Op(), m.Type(), time.Since(start), m, name,
)
}()
return next.Mutate(ctx, m)
})
})
return nil
}