Files
budget-backend/internal/storage/dao/postgresql/database_postgresql.go
kratisto f81c902ca6
Some checks failed
golangci-lint / lint (push) Successful in 1m33s
Test / test (push) Failing after 2m16s
chore: migrate to gitea
2026-01-27 01:56:53 +01:00

45 lines
1019 B
Go
Executable File

package postgresql
import (
"database/sql"
"fmt"
"gitea.frenchtouch.duckdns.org/kratisto/budget-backend/internal/storage/dao"
"gitea.frenchtouch.duckdns.org/kratisto/budget-backend/internal/utils"
"github.com/lib/pq"
)
const (
pgCodeUniqueViolation = "23505"
pgCodeForeingKeyViolation = "23503"
)
func HandlePgError(e *pq.Error) error {
if e.Code == pgCodeUniqueViolation {
return dao.NewDAOError(dao.ErrTypeDuplicate, e)
}
if e.Code == pgCodeForeingKeyViolation {
return dao.NewDAOError(dao.ErrTypeForeignKeyViolation, e)
}
return e
}
type DatabasePostgreSQL struct {
Session *sql.DB
}
func NewDatabasePostgreSQL(connectionURI string) *DatabasePostgreSQL {
db, err := sql.Open("postgres", connectionURI)
if err != nil {
utils.GetLogger().WithError(err).Fatal("Unable to get a connection to the postgres db")
}
err = db.Ping()
if err != nil {
fmt.Println(err)
utils.GetLogger().WithError(err).Fatal("Unable to ping the postgres db")
}
return &DatabasePostgreSQL{Session: db}
}