2024-11-10 09:22:33 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"contrib.go.opencensus.io/integrations/ocsql"
|
|
|
|
"entgo.io/ent/dialect"
|
|
|
|
entsql "entgo.io/ent/dialect/sql"
|
|
|
|
pgx "github.com/jackc/pgx/v5/stdlib"
|
|
|
|
"gitserver.in/patialtech/rano/config"
|
|
|
|
"gitserver.in/patialtech/rano/db/ent"
|
2024-11-15 16:12:15 +00:00
|
|
|
"gitserver.in/patialtech/rano/util/logger"
|
2024-11-10 09:22:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type connector struct {
|
|
|
|
dsn string
|
|
|
|
}
|
|
|
|
|
|
|
|
// New *sql.DB instance
|
|
|
|
func New() *sql.DB {
|
|
|
|
databaseUrl := config.Read().DbURL
|
|
|
|
db := sql.OpenDB(connector{dsn: databaseUrl})
|
|
|
|
db.SetMaxIdleConns(5)
|
|
|
|
db.SetMaxOpenConns(50)
|
|
|
|
db.SetConnMaxLifetime(time.Minute)
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c connector) Connect(context.Context) (driver.Conn, error) {
|
|
|
|
return c.Driver().Open(c.dsn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (connector) Driver() driver.Driver {
|
|
|
|
return ocsql.Wrap(
|
|
|
|
pgx.GetDefaultDriver(),
|
|
|
|
ocsql.WithAllTraceOptions(),
|
|
|
|
ocsql.WithRowsClose(false),
|
|
|
|
ocsql.WithRowsNext(false),
|
|
|
|
ocsql.WithDisableErrSkip(true),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client for pgx
|
|
|
|
//
|
|
|
|
// https://entgo.io/docs/sql-integration
|
|
|
|
func Client() *ent.Client {
|
|
|
|
// Create an ent.Driver from `db`.
|
|
|
|
drv := entsql.OpenDB(dialect.Postgres, New())
|
|
|
|
cl := ent.NewClient(ent.Driver(drv))
|
|
|
|
cl.Use(AuditHook)
|
|
|
|
return cl
|
|
|
|
}
|
|
|
|
|
|
|
|
// A AuditHook is an example for audit-log hook.
|
|
|
|
func AuditHook(next ent.Mutator) ent.Mutator {
|
2024-11-15 16:12:15 +00:00
|
|
|
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (v ent.Value, err error) {
|
2024-11-10 09:22:33 +00:00
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
2024-11-15 16:12:15 +00:00
|
|
|
saveAudit(ctx, m.Type(), m.Op().String(), time.Since(start), err)
|
2024-11-10 09:22:33 +00:00
|
|
|
}()
|
2024-11-15 16:12:15 +00:00
|
|
|
|
|
|
|
v, err = next.Mutate(ctx, m)
|
|
|
|
logger.Info("** %v", v)
|
|
|
|
return
|
2024-11-10 09:22:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-15 16:12:15 +00:00
|
|
|
func saveAudit(_ context.Context, entT, op string, d time.Duration, err error) {
|
|
|
|
logger.Info("audit: %s %s %s %v", entT, op, d, err)
|
2024-11-10 09:22:33 +00:00
|
|
|
// ml.SetCreatedAt(time.Now())
|
|
|
|
// if usr := auth.CtxUser(ctx); usr != nil {
|
|
|
|
// ml.SetByID(usr.ID)
|
|
|
|
// ml.SetBy(usr.DisplayName)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// switch op := m.Op(); {
|
|
|
|
// case op.Is(ent.OpCreate):
|
|
|
|
// ml.SetOperation("Create")
|
|
|
|
|
|
|
|
// case op.Is(ent.OpUpdate):
|
|
|
|
// ml.SetOperation("Update")
|
|
|
|
// case op.Is(ent.OpUpdateOne):
|
|
|
|
// ml.SetOperation("UpdateOne")
|
|
|
|
|
|
|
|
// case op.Is(ent.OpDelete):
|
|
|
|
// ml.SetOperation("Delete")
|
|
|
|
// case op.Is(ent.OpDeleteOne):
|
|
|
|
// ml.SetOperation("DeleteOne")
|
|
|
|
// }
|
|
|
|
}
|