Basic error handling test

data-work
Mike Holloway 3 months ago
parent e7140478de
commit ff6de84731
  1. 28
      main.go

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"context" "context"
"errors"
"database/sql" "database/sql"
"github.com/uptrace/bun" "github.com/uptrace/bun"
"github.com/uptrace/bun/extra/bundebug" "github.com/uptrace/bun/extra/bundebug"
@ -10,7 +11,13 @@ import (
"github.com/uptrace/bun/driver/pgdriver" "github.com/uptrace/bun/driver/pgdriver"
) )
var feeds []Feed
var categories []Category
var entries []Entry
var users []User
func main() { func main() {
var err error
ctx := context.Background() ctx := context.Background()
dsn := "postgres://apache:@localhost:5432/miniflux?sslmode=disable" dsn := "postgres://apache:@localhost:5432/miniflux?sslmode=disable"
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432" // dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
@ -22,31 +29,28 @@ func main() {
bundebug.FromEnv("BUNDEBUG"), bundebug.FromEnv("BUNDEBUG"),
)) ))
var feeds []Feed
var categories []Category
var entries []Entry
var users []User
// Turn these into funcs with error handling, logging, return vals etc. // Turn these into funcs with error handling, logging, return vals etc.
feederr := db.NewSelect().Model(&feeds).OrderExpr("title ASC").Limit(10).Scan(ctx) feederr := db.NewSelect().Model(&feeds).OrderExpr("title ASC").Limit(10).Scan(ctx)
if feederr != nil { if feederr != nil {
return errors.Join(feederr, err)
} }
fmt.Printf("%+v\n", feeds)
categoryerr := db.NewSelect().Model(&categories).OrderExpr("title ASC").Scan(ctx) categoryerr := db.NewSelect().Model(&categories).OrderExpr("title ASC").Scan(ctx)
if categoryerr != nil { if categoryerr != nil {
return errors.Join(categoryerr, err)
} }
fmt.Printf("%+v\n", categories)
entryerr := db.NewSelect().Model(&entries).OrderExpr("published_at ASC").Limit(10).Scan(ctx) entryerr := db.NewSelect().Model(&entries).OrderExpr("published_at ASC").Limit(10).Scan(ctx)
if entryerr != nil { if entryerr != nil {
return errors.Join(entryerr, err)
} }
fmt.Printf("%+v\n", entries)
usererr := db.NewSelect().Model(&users).OrderExpr("id ASC").Scan(ctx) usererr := db.NewSelect().Model(&users).OrderExpr("id ASC").Scan(ctx)
if usererr != nil { if usererr != nil {
return errors.Join(usererr, err)
} }
fmt.Printf("%+v\n", entries)
fmt.Printf("%+v\n", categories)
fmt.Printf("%+v\n", feeds)
fmt.Printf("%+v\n", users) fmt.Printf("%+v\n", users)
//
} }

Loading…
Cancel
Save