You've already forked testing-go-code-with-postgres
mirror of
https://github.com/xorcare/testing-go-code-with-postgres.git
synced 2025-06-30 23:23:40 +02:00
Migrate to sql.DB driver
This is to reduce dependency on a particular driver. In the future this will be used to implement different ways of cleaning the database.
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2023 Vasiliy Vasilyuk. All rights reserved.
|
||||
// Copyright (c) 2023-2024 Vasiliy Vasilyuk. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
@ -6,18 +6,18 @@ package testing_go_code_with_postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func NewUserRepository(db *pgxpool.Pool) *UserRepository {
|
||||
func NewUserRepository(db *sql.DB) *UserRepository {
|
||||
return &UserRepository{db: db}
|
||||
}
|
||||
|
||||
type UserRepository struct {
|
||||
db *pgxpool.Pool
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func (r *UserRepository) ReadUser(ctx context.Context, userID uuid.UUID) (User, error) {
|
||||
@ -25,7 +25,7 @@ func (r *UserRepository) ReadUser(ctx context.Context, userID uuid.UUID) (User,
|
||||
|
||||
user := User{}
|
||||
|
||||
row := r.db.QueryRow(ctx, sql, userID)
|
||||
row := r.db.QueryRowContext(ctx, sql, userID)
|
||||
|
||||
err := row.Scan(&user.ID, &user.Username, &user.CreatedAt)
|
||||
if err != nil {
|
||||
@ -40,7 +40,7 @@ func (r *UserRepository) ReadUser(ctx context.Context, userID uuid.UUID) (User,
|
||||
func (r *UserRepository) CreateUser(ctx context.Context, user User) error {
|
||||
const sql = `INSERT INTO users (user_id, username, created_at) VALUES ($1,$2,$3);`
|
||||
|
||||
_, err := r.db.Exec(
|
||||
_, err := r.db.ExecContext(
|
||||
ctx,
|
||||
sql,
|
||||
user.ID,
|
||||
|
Reference in New Issue
Block a user