fix(db): use a normal pgx.Conn first
This commit is contained in:
		
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@ -35,7 +35,6 @@ require (
 | 
			
		||||
	github.com/inconshreveable/mousetrap v1.1.0 // indirect
 | 
			
		||||
	github.com/jackc/pgpassfile v1.0.0 // indirect
 | 
			
		||||
	github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
 | 
			
		||||
	github.com/jackc/puddle/v2 v2.2.2 // indirect
 | 
			
		||||
	github.com/json-iterator/go v1.1.12 // indirect
 | 
			
		||||
	github.com/klauspost/cpuid/v2 v2.2.7 // indirect
 | 
			
		||||
	github.com/leodido/go-urn v1.4.0 // indirect
 | 
			
		||||
@ -61,7 +60,6 @@ require (
 | 
			
		||||
	golang.org/x/arch v0.8.0 // indirect
 | 
			
		||||
	golang.org/x/crypto v0.27.0 // indirect
 | 
			
		||||
	golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
 | 
			
		||||
	golang.org/x/sync v0.8.0 // indirect
 | 
			
		||||
	golang.org/x/sys v0.25.0 // indirect
 | 
			
		||||
	golang.org/x/text v0.18.0 // indirect
 | 
			
		||||
	google.golang.org/protobuf v1.34.1 // indirect
 | 
			
		||||
 | 
			
		||||
@ -37,6 +37,7 @@ import (
 | 
			
		||||
	"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
 | 
			
		||||
	"git.vinchent.xyz/vinchent/howmuch/pkg/version/verflag"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"github.com/jackc/pgx/v5"
 | 
			
		||||
	"github.com/spf13/cobra"
 | 
			
		||||
	"github.com/spf13/viper"
 | 
			
		||||
	"golang.org/x/net/context"
 | 
			
		||||
@ -105,24 +106,27 @@ func run() error {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Init DB
 | 
			
		||||
	dbPool, err := datastore.NewDB(
 | 
			
		||||
		fmt.Sprintf(
 | 
			
		||||
			"host=%s port=%d dbname=%s user=%s password=%s sslmode=%s",
 | 
			
		||||
			viper.GetString("db.host"),
 | 
			
		||||
			viper.GetInt("db.port"),
 | 
			
		||||
			viper.GetString("db.database"),
 | 
			
		||||
			viper.GetString("db.username"),
 | 
			
		||||
			viper.GetString("db.password"),
 | 
			
		||||
			viper.GetString("db.sslmode"),
 | 
			
		||||
		),
 | 
			
		||||
	dbConfString := fmt.Sprintf(
 | 
			
		||||
		"host=%s port=%d dbname=%s user=%s password=%s sslmode=%s",
 | 
			
		||||
		viper.GetString("db.host"),
 | 
			
		||||
		viper.GetInt("db.port"),
 | 
			
		||||
		viper.GetString("db.database"),
 | 
			
		||||
		viper.GetString("db.username"),
 | 
			
		||||
		viper.GetString("db.password"),
 | 
			
		||||
		viper.GetString("db.sslmode"),
 | 
			
		||||
	)
 | 
			
		||||
	dbConf, err := pgx.ParseConfig(dbConfString)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.FatalLog("DB connection config failure", "err", err, "cfg string", dbConfString)
 | 
			
		||||
	}
 | 
			
		||||
	dbConn, err := datastore.NewDB(dbConf)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.FatalLog("DB connection failure", "err", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer dbPool.Close()
 | 
			
		||||
	defer dbConn.Close(context.Background())
 | 
			
		||||
 | 
			
		||||
	// Register the core service
 | 
			
		||||
	r := registry.NewRegistry(dbPool)
 | 
			
		||||
	r := registry.NewRegistry(dbConn)
 | 
			
		||||
 | 
			
		||||
	engine := gin.Default()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -25,12 +25,12 @@ package datastore
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
 | 
			
		||||
	"github.com/jackc/pgx/v5/pgxpool"
 | 
			
		||||
	"github.com/jackc/pgx/v5"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// NewDB creates a new database for the application
 | 
			
		||||
func NewDB(dsn string) (*pgxpool.Pool, error) {
 | 
			
		||||
	conn, err := pgxpool.New(context.Background(), dsn)
 | 
			
		||||
func NewDB(connConfig *pgx.ConnConfig) (*pgx.Conn, error) {
 | 
			
		||||
	conn, err := pgx.ConnectConfig(context.Background(), connConfig)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user