Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing pgx db connection to a pool connection #26

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/course/courseParticipation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package courseParticipation

import (
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
db "github.com/niclasheun/prompt2.0/db/sqlc"
)

func InitCourseParticipationModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgx.Conn) {
func InitCourseParticipationModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgxpool.Pool) {
setupCourseParticipationRouter(routerGroup)
CourseParticipationServiceSingleton = &CourseParticipationService{
queries: queries,
Expand Down
4 changes: 2 additions & 2 deletions server/course/courseParticipation/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"

"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/niclasheun/prompt2.0/course/courseParticipation/courseParticipationDTO"
db "github.com/niclasheun/prompt2.0/db/sqlc"
)

type CourseParticipationService struct {
queries db.Queries
conn *pgx.Conn
conn *pgxpool.Pool
}

var CourseParticipationServiceSingleton *CourseParticipationService
Expand Down
4 changes: 2 additions & 2 deletions server/course/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package course

import (
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
db "github.com/niclasheun/prompt2.0/db/sqlc"
"github.com/niclasheun/prompt2.0/keycloak"
"github.com/niclasheun/prompt2.0/permissionValidation"
)

func InitCourseModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgx.Conn) {
func InitCourseModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgxpool.Pool) {

setupCourseRouter(routerGroup, keycloak.KeycloakMiddleware, permissionValidation.CheckAccessControlByRole, checkAccessControlByIDWrapper)
CourseServiceSingleton = &CourseService{
Expand Down
4 changes: 2 additions & 2 deletions server/course/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/niclasheun/prompt2.0/course/courseDTO"
"github.com/niclasheun/prompt2.0/coursePhase/coursePhaseDTO"
db "github.com/niclasheun/prompt2.0/db/sqlc"
Expand All @@ -14,7 +14,7 @@ import (

type CourseService struct {
queries db.Queries
conn *pgx.Conn
conn *pgxpool.Pool
// use dependency injection for keycloak to allow mocking
createCourseGroupsAndRoles func(ctx context.Context, courseName, iterationName string) error
addUserToGroup func(ctx context.Context, userID, groupName string) error
Expand Down
4 changes: 2 additions & 2 deletions server/coursePhase/coursePhaseParticipation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package coursePhaseParticipation

import (
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
db "github.com/niclasheun/prompt2.0/db/sqlc"
)

func InitCoursePhaseParticipationModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgx.Conn) {
func InitCoursePhaseParticipationModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgxpool.Pool) {
setupCoursePhaseParticipationRouter(routerGroup)
CoursePhaseParticipationServiceSingleton = &CoursePhaseParticipationService{
queries: queries,
Expand Down
4 changes: 2 additions & 2 deletions server/coursePhase/coursePhaseParticipation/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"

"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/niclasheun/prompt2.0/coursePhase/coursePhaseParticipation/coursePhaseParticipationDTO"
db "github.com/niclasheun/prompt2.0/db/sqlc"
)

type CoursePhaseParticipationService struct {
queries db.Queries
conn *pgx.Conn
conn *pgxpool.Pool
}

var CoursePhaseParticipationServiceSingleton *CoursePhaseParticipationService
Expand Down
4 changes: 2 additions & 2 deletions server/coursePhase/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package coursePhase

import (
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
db "github.com/niclasheun/prompt2.0/db/sqlc"
)

func InitCoursePhaseModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgx.Conn) {
func InitCoursePhaseModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgxpool.Pool) {

setupCoursePhaseRouter(routerGroup)
CoursePhaseServiceSingleton = &CoursePhaseService{
Expand Down
4 changes: 2 additions & 2 deletions server/coursePhase/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"

"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/niclasheun/prompt2.0/coursePhase/coursePhaseDTO"
db "github.com/niclasheun/prompt2.0/db/sqlc"
)

type CoursePhaseService struct {
queries db.Queries
conn *pgx.Conn
conn *pgxpool.Pool
}

var CoursePhaseServiceSingleton *CoursePhaseService
Expand Down
2 changes: 2 additions & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.1 // 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/compress v1.17.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
Expand Down Expand Up @@ -87,6 +88,7 @@ require (
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.21.0 // 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
Expand Down
8 changes: 4 additions & 4 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os/exec"

"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/niclasheun/prompt2.0/course"
"github.com/niclasheun/prompt2.0/course/courseParticipation"
"github.com/niclasheun/prompt2.0/coursePhase"
Expand Down Expand Up @@ -62,12 +62,12 @@ func main() {
runMigrations(databaseURL)

// establish db connection
conn, err := pgx.Connect(context.Background(), databaseURL)
conn, err := pgxpool.New(context.Background(), databaseURL)
if err != nil {
log.Fatalf("Unable to connect to database: %v\n", err)
log.Fatalf("Unable to create connection pool: %v\n", err)
os.Exit(1)
}
defer conn.Close(context.Background())
defer conn.Close()

query := db.New(conn)

Expand Down
6 changes: 3 additions & 3 deletions server/permissionValidation/main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package permissionValidation

import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
db "github.com/niclasheun/prompt2.0/db/sqlc"
)

type ValidationService struct {
queries db.Queries
conn *pgx.Conn
conn *pgxpool.Pool
}

var ValidationServiceSingleton *ValidationService

func InitValidationService(queries db.Queries, conn *pgx.Conn) {
func InitValidationService(queries db.Queries, conn *pgxpool.Pool) {
ValidationServiceSingleton = &ValidationService{
queries: queries,
conn: conn,
Expand Down
4 changes: 2 additions & 2 deletions server/student/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package student

import (
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
db "github.com/niclasheun/prompt2.0/db/sqlc"
)

func InitStudentModule(api *gin.RouterGroup, queries db.Queries, conn *pgx.Conn) {
func InitStudentModule(api *gin.RouterGroup, queries db.Queries, conn *pgxpool.Pool) {

setupStudentRouter(api)
StudentServiceSingleton = &StudentService{
Expand Down
4 changes: 2 additions & 2 deletions server/student/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"

"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
db "github.com/niclasheun/prompt2.0/db/sqlc"
"github.com/niclasheun/prompt2.0/student/studentDTO"
)

type StudentService struct {
queries db.Queries
conn *pgx.Conn
conn *pgxpool.Pool
}

var StudentServiceSingleton *StudentService
Expand Down
14 changes: 7 additions & 7 deletions server/testutils/db_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"os"
"time"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
db "github.com/niclasheun/prompt2.0/db/sqlc"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

type TestDB struct {
Conn *pgx.Conn
Conn *pgxpool.Pool
Queries *db.Queries
}

Expand Down Expand Up @@ -41,18 +41,18 @@ func SetupTestDB(ctx context.Context, sqlDumpPath string) (*TestDB, func(), erro
// Get container's host and port
host, _ := container.Host(ctx)
port, _ := container.MappedPort(ctx, "5432/tcp")
dbURL := fmt.Sprintf("postgres://testuser:testpass@%s:%s/prompt", host, port.Port())
dbURL := fmt.Sprintf("postgres://testuser:testpass@%s:%s/prompt?sslmode=disable", host, port.Port())

// Connect to the database
conn, err := pgx.Connect(ctx, dbURL)
conn, err := pgxpool.New(ctx, dbURL)
if err != nil {
container.Terminate(ctx)
return nil, nil, fmt.Errorf("failed to connect to the database: %w", err)
}

// Run the SQL dump
if err := runSQLDump(conn, sqlDumpPath); err != nil {
conn.Close(ctx)
conn.Close()
container.Terminate(ctx)
return nil, nil, fmt.Errorf("failed to run SQL dump: %w", err)
}
Expand All @@ -62,7 +62,7 @@ func SetupTestDB(ctx context.Context, sqlDumpPath string) (*TestDB, func(), erro

// Return the TestDB and a cleanup function
cleanup := func() {
conn.Close(ctx)
conn.Close()
container.Terminate(ctx)
}

Expand All @@ -72,7 +72,7 @@ func SetupTestDB(ctx context.Context, sqlDumpPath string) (*TestDB, func(), erro
}, cleanup, nil
}

func runSQLDump(conn *pgx.Conn, path string) error {
func runSQLDump(conn *pgxpool.Pool, path string) error {
dump, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("could not read SQL dump file: %w", err)
Expand Down
Loading