Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/scheduling-algorith…
Browse files Browse the repository at this point in the history
…m' into scheduling-algorithm
  • Loading branch information
funkybooboo committed Jun 20, 2024
2 parents 9570433 + 3c4a609 commit 18c2797
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
23 changes: 11 additions & 12 deletions backend/internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/jak103/powerplay/internal/utils/log"
"gorm.io/driver/postgres"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
gorm_logger "gorm.io/gorm/logger"
)

var dbConnection *gorm.DB
Expand All @@ -35,23 +35,23 @@ func Init() error {
config.Vars.Db.DbName,
config.Vars.Db.Port)

dsnRedacted := fmt.Sprintf("host=%s user=%s password=<REDACTED> dbname=%s port=%s sslmode=disable TimeZone=UTC",
dsn_redacted := fmt.Sprintf("host=%s user=%s password=<REDACTED> dbname=%s port=%s sslmode=disable TimeZone=UTC",
config.Vars.Db.Host,
config.Vars.Db.Username,
config.Vars.Db.DbName,
config.Vars.Db.Port)

log.Info("DB DSN: %s", dsnRedacted)
log.Info("DB DSN: %s", dsn_redacted)

// TODO replace GORM logger with our logger
newLogger := gormlogger.New(
newLogger := gorm_logger.New(
stdLog.New(os.Stdout, "\r\n", stdLog.LstdFlags), // io writer
gormlogger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: gormlogger.Silent, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
ParameterizedQueries: true, // Don't include params in the SQL log
Colorful: false, // Disable color
gorm_logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: gorm_logger.Silent, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
ParameterizedQueries: true, // Don't include params in the SQL log
Colorful: false, // Disable color
},
)

Expand All @@ -60,7 +60,7 @@ func Init() error {
log.WithErr(err).Alert("Failed to connect to DB")
return err
}
log.Info("Database connected")
log.Info("Databse connected")

return nil
}
Expand All @@ -82,7 +82,6 @@ func GetDB() *gorm.DB {
}

func GetSession(c *fiber.Ctx) session {

logger := log.TheLogger
if c != nil {
logger = locals.Logger(c)
Expand Down
20 changes: 10 additions & 10 deletions backend/internal/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ func TestDatabase(t *testing.T) {
suite.Run(t, new(dbTestingSuite))
}

func (s *dbTestingSuite) SetupSuite() {
func (suite *dbTestingSuite) SetupSuite() {
log.Info("Starting db container")
start := time.Now()
dbConnection = s.startDb()
dbConnection = suite.startDb()
if dbConnection == nil {
s.FailNow("Failed to start db")
suite.FailNow("Failed to start db")
}

s.session = session{
suite.session = session{
DB: dbConnection.Session(&gorm.Session{
Logger: logger.Default.LogMode(logger.Silent),
}),
}
log.Info("Done! (%v)", time.Since(start))
}

func (s *dbTestingSuite) TearDownSuite() {
if err := s.pool.Purge(s.db); err != nil {
func (suite *dbTestingSuite) TearDownSuite() {
if err := suite.pool.Purge(suite.db); err != nil {
log.Error("Could not purge resource: %s", err)
}
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func (s *dbTestingSuite) startDb() *gorm.DB {
time.Sleep(10 * time.Second) // Let db start
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
if err := s.pool.Retry(func() error {
dsn := fmt.Sprintf("host=localhost user=postgres password=password dbname=powerplay port=%s sslmode=disable", s.db.GetPort("5432/tcp"))
dsn := fmt.Sprintf("host=localhost user=postgres password=password dbname=powerplay port=%s sslmode=disable", s.db.GetPort(("5432/tcp")))

log.Info("Trying to ping db at %s", dsn)
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
Expand All @@ -147,7 +147,7 @@ func TestResultOrErrorNominal(t *testing.T) {
Error: nil,
}

var record = &models.KeyRecord{
var record *models.KeyRecord = &models.KeyRecord{
UserId: 99,
}

Expand All @@ -162,7 +162,7 @@ func TestResultOrErrorNoResult(t *testing.T) {
Error: gorm.ErrRecordNotFound,
}

var record = &models.KeyRecord{
var record *models.KeyRecord = &models.KeyRecord{
UserId: 99,
}

Expand All @@ -177,7 +177,7 @@ func TestResultOrErrorError(t *testing.T) {
Error: gorm.ErrDuplicatedKey,
}

var record = &models.KeyRecord{
var record *models.KeyRecord = &models.KeyRecord{
UserId: 99,
}

Expand Down
3 changes: 2 additions & 1 deletion backend/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"fmt"
"github.com/jak103/powerplay/internal/utils/log"

"github.com/gofiber/fiber/v2"
"github.com/jak103/powerplay/internal/config"
Expand Down Expand Up @@ -33,7 +34,7 @@ func Run() {

err := app.Listen(fmt.Sprintf(":%s", config.Vars.Port))
if err != nil {
return
log.WithErr(err).Error("Server error: %v", err.Error())
}
}

Expand Down

0 comments on commit 18c2797

Please sign in to comment.