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:
@@ -1,85 +1,15 @@
|
||||
package ntpdb
|
||||
|
||||
//go:generate go tool github.com/hexdigest/gowrap/cmd/gowrap gen -t ./opentelemetry.gowrap -g -i QuerierTx -p . -o otel.go
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"go.ntppool.org/common/logger"
|
||||
"gopkg.in/yaml.v3"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"go.ntppool.org/common/database/pgdb"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
MySQL DBConfig `yaml:"mysql"`
|
||||
}
|
||||
|
||||
type DBConfig struct {
|
||||
DSN string `default:"" flag:"dsn" usage:"Database DSN"`
|
||||
User string `default:"" flag:"user"`
|
||||
Pass string `default:"" flag:"pass"`
|
||||
}
|
||||
|
||||
func OpenDB(ctx context.Context, configFile string) (*sql.DB, error) {
|
||||
log := logger.FromContext(ctx)
|
||||
|
||||
dbconn := sql.OpenDB(Driver{CreateConnectorFunc: createConnector(ctx, configFile)})
|
||||
|
||||
dbconn.SetConnMaxLifetime(time.Minute * 3)
|
||||
dbconn.SetMaxOpenConns(8)
|
||||
dbconn.SetMaxIdleConns(3)
|
||||
|
||||
err := dbconn.Ping()
|
||||
if err != nil {
|
||||
log.DebugContext(ctx, "could not connect to database: %s", "err", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return dbconn, nil
|
||||
}
|
||||
|
||||
func createConnector(ctx context.Context, configFile string) CreateConnectorFunc {
|
||||
log := logger.FromContext(ctx)
|
||||
return func() (driver.Connector, error) {
|
||||
log.DebugContext(ctx, "opening db config file", "filename", configFile)
|
||||
|
||||
dbFile, err := os.Open(configFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dec := yaml.NewDecoder(dbFile)
|
||||
|
||||
cfg := Config{}
|
||||
|
||||
err = dec.Decode(&cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// log.Printf("db cfg: %+v", cfg)
|
||||
|
||||
dsn := cfg.MySQL.DSN
|
||||
if len(dsn) == 0 {
|
||||
return nil, fmt.Errorf("--database.dsn flag or DATABASE_DSN environment variable required")
|
||||
}
|
||||
|
||||
dbcfg, err := mysql.ParseDSN(dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if user := cfg.MySQL.User; len(user) > 0 {
|
||||
dbcfg.User = user
|
||||
}
|
||||
|
||||
if pass := cfg.MySQL.Pass; len(pass) > 0 {
|
||||
dbcfg.Passwd = pass
|
||||
}
|
||||
|
||||
return mysql.NewConnector(dbcfg)
|
||||
}
|
||||
// OpenDB opens a PostgreSQL connection pool using the specified config file
|
||||
func OpenDB(ctx context.Context, configFile string) (*pgxpool.Pool, error) {
|
||||
return pgdb.OpenPoolWithConfigFile(ctx, configFile)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user