mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
fix: standardize timestamps and database sessions on UTC
This commit is contained in:
parent
6f49fd3fc5
commit
0b4726efce
@ -2,14 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"net"
|
||||
"net/http"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
@ -21,6 +19,7 @@ import (
|
||||
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
|
||||
|
||||
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/config"
|
||||
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/postgresutc"
|
||||
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/idgen"
|
||||
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/observability"
|
||||
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/repository"
|
||||
@ -59,7 +58,7 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
db, err := sql.Open("pgx", cfg.PostgresDSN)
|
||||
db, err := postgresutc.Open(cfg.PostgresDSN)
|
||||
if err != nil {
|
||||
logger.Fatal("open postgres failed", zap.Error(err))
|
||||
}
|
||||
@ -135,6 +134,8 @@ func main() {
|
||||
|
||||
func newLogger() (*zap.Logger, error) {
|
||||
cfg := zap.NewProductionConfig()
|
||||
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
cfg.EncoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
||||
enc.AppendString(t.UTC().Format(time.RFC3339Nano))
|
||||
}
|
||||
return cfg.Build()
|
||||
}
|
||||
|
||||
37
internal/postgresutc/open.go
Normal file
37
internal/postgresutc/open.go
Normal file
@ -0,0 +1,37 @@
|
||||
package postgresutc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
// Open returns a *sql.DB that forces TIME ZONE UTC on every physical connection.
|
||||
// RuntimeParams and AfterConnect both set UTC; callers must not rely on POSTGRES_TIME_ZONE.
|
||||
func Open(dsn string) (*sql.DB, error) {
|
||||
cfg, err := parseConfig(dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db := stdlib.OpenDB(*cfg, stdlib.OptionAfterConnect(func(ctx context.Context, conn *pgx.Conn) error {
|
||||
_, err := conn.Exec(ctx, "SET TIME ZONE 'UTC'")
|
||||
return err
|
||||
}))
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func parseConfig(dsn string) (*pgx.ConnConfig, error) {
|
||||
cfg, err := pgx.ParseConfig(dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse postgres dsn: %w", err)
|
||||
}
|
||||
if cfg.RuntimeParams == nil {
|
||||
cfg.RuntimeParams = make(map[string]string)
|
||||
}
|
||||
cfg.RuntimeParams["timezone"] = "UTC"
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user