feat(db): migrate from MySQL to PostgreSQL
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Replace MySQL driver with pgx/v5 and pgxpool: - Update sqlc to use postgresql engine - Convert query.sql to PostgreSQL syntax ($1 params, CASE WHEN, ANY() arrays) - Replace sql.DB with pgxpool.Pool throughout - Change nullable types from sql.Null* to pgtype.* - Update ID types from uint32 to int64 for PostgreSQL compatibility - Delete MySQL-specific dynamic_connect.go - Add opentelemetry.gowrap template for tracing
This commit is contained in:
@@ -2,17 +2,16 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/labstack/echo-contrib/echoprometheus"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
@@ -36,7 +35,7 @@ import (
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
db *sql.DB
|
||||
db *pgxpool.Pool
|
||||
ch *chdb.ClickHouse
|
||||
config *config.Config
|
||||
|
||||
@@ -55,7 +54,7 @@ func NewServer(ctx context.Context, configFile string) (*Server, error) {
|
||||
}
|
||||
db, err := ntpdb.OpenDB(ctx, configFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mysql open: %w", err)
|
||||
return nil, fmt.Errorf("postgres open: %w", err)
|
||||
}
|
||||
|
||||
conf := config.New()
|
||||
@@ -303,22 +302,9 @@ func healthHandler(srv *Server, log *slog.Logger) func(w http.ResponseWriter, re
|
||||
defer cancel()
|
||||
g, ctx := errgroup.WithContext(ctx)
|
||||
|
||||
stats := srv.db.Stats()
|
||||
if stats.OpenConnections > 3 {
|
||||
log.InfoContext(ctx, "health requests", "url", req.URL.String(), "stats", stats)
|
||||
}
|
||||
|
||||
if resetParam := req.URL.Query().Get("reset"); resetParam != "" {
|
||||
reset, err := strconv.ParseBool(resetParam)
|
||||
log.InfoContext(ctx, "db reset request", "err", err, "reset", reset)
|
||||
|
||||
if err == nil && reset {
|
||||
// this feature was to debug some specific problem
|
||||
log.InfoContext(ctx, "setting idle db conns to zero")
|
||||
srv.db.SetConnMaxLifetime(30 * time.Second)
|
||||
srv.db.SetMaxIdleConns(0)
|
||||
srv.db.SetMaxIdleConns(4)
|
||||
}
|
||||
stats := srv.db.Stat()
|
||||
if stats.TotalConns() > 3 {
|
||||
log.InfoContext(ctx, "health requests", "url", req.URL.String(), "total_conns", stats.TotalConns())
|
||||
}
|
||||
|
||||
g.Go(func() error {
|
||||
@@ -340,7 +326,7 @@ func healthHandler(srv *Server, log *slog.Logger) func(w http.ResponseWriter, re
|
||||
})
|
||||
|
||||
g.Go(func() error {
|
||||
err := srv.db.PingContext(ctx)
|
||||
err := srv.db.Ping(ctx)
|
||||
if err != nil {
|
||||
log.WarnContext(ctx, "db ping", "err", err)
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user