Skip to content

Commit

Permalink
Fix group get after switch to native pgx.
Browse files Browse the repository at this point in the history
Native pgx return string from db, not bytes.
  • Loading branch information
kak-tus committed Dec 30, 2023
1 parent dc5a4af commit 7a3b667
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2023-12-31 v1.8.1
- Fix group get after switch to native pgx.

2023-12-30 v1.8.0
- Fix bot stop.
- Use versioned generators.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/go-chi/cors v1.2.0
github.com/go-redis/redis/v8 v8.11.4
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/goccy/go-json v0.9.7
github.com/goccy/go-json v0.10.2
github.com/jackc/pgx/v5 v5.5.1
github.com/json-iterator/go v1.1.12
github.com/kak-tus/nan v0.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGi
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gocql/gocql v0.0.0-20211222173705-d73e6b1002a7 h1:jmIMM+nEO+vjz9xaRIg9sZNtNLq5nsSbsxwe1OtRwv4=
github.com/gocql/gocql v0.0.0-20211222173705-d73e6b1002a7/go.mod h1:3gM2c4D3AnkISwBxGnMMsS8Oy4y2lhbPRsH4xnJrHG8=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
}

modelOpts := model.Options{
Log: oldLog,
Log: log,
URL: cnf.DB.Addr,
}

Expand Down
10 changes: 5 additions & 5 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import (

"github.com/jackc/pgx/v5/pgxpool"
"github.com/kak-tus/irma_bot/model/queries"
"go.uber.org/zap"
"github.com/rs/zerolog"
)

type Options struct {
Log *zap.SugaredLogger
Log zerolog.Logger
URL string
}

type Model struct {
conn *pgxpool.Pool
log *zap.SugaredLogger
log zerolog.Logger
Queries *queries.Queries
url string
}
Expand All @@ -42,9 +42,9 @@ func (hdl *Model) Start(ctx context.Context) error {
}

func (hdl *Model) Stop() {
hdl.log.Info("stop model")
hdl.log.Info().Msg("stop model")

hdl.conn.Close()

hdl.log.Info("stopped model")
hdl.log.Info().Msg("stopped model")
}
48 changes: 48 additions & 0 deletions model/model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package model

import (
"context"
"os"
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)

func TestGetGroup(t *testing.T) {
hdl, err := NewTestModel()
require.NoError(t, err)

defer hdl.Stop()

group, err := hdl.Queries.GetGroup(context.Background(), -1001175783418)
require.NoError(t, err)
require.True(t, group.BanQuestion.Bool)
}

func NewTestModel() (*Model, error) {
addr := os.Getenv("IRMA_DB_ADDR")
if addr == "" {
return nil, nil
}

log := zerolog.New(os.Stdout).With().Timestamp().Logger()

modelOpts := Options{
Log: log,
URL: addr,
}

modelHdl, err := New(modelOpts)
if err != nil {
return nil, err
}

ctx := context.Background()

if err := modelHdl.Start(ctx); err != nil {
log.Panic().Err(err).Msg("fail start model")
}

return modelHdl, nil
}
6 changes: 3 additions & 3 deletions model/queries_types/questions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ func (container *QuestionsDB) Scan(value any) error {
return nil
}

converted, ok := value.([]byte)
converted, ok := value.(string)
if !ok {
return fmt.Errorf("incorrect type %T for scan", value)
return fmt.Errorf("incorrect type '%T' for scan", value)
}

var vals Questions

if err := json.Unmarshal(converted, &vals); err != nil {
if err := json.Unmarshal([]byte(converted), &vals); err != nil {
return err
}

Expand Down

0 comments on commit 7a3b667

Please sign in to comment.