Skip to content

Commit

Permalink
wip: more upsert examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jrauh01 committed Dec 12, 2024
1 parent 242c1e9 commit f2b199d
Show file tree
Hide file tree
Showing 2 changed files with 324 additions and 58 deletions.
58 changes: 0 additions & 58 deletions database/example_upsert_streamed_test.go

This file was deleted.

324 changes: 324 additions & 0 deletions database/example_upsert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,324 @@
package database

import (
"context"
"fmt"
"github.com/icinga/icinga-go-library/com"
"golang.org/x/sync/errgroup"
"time"
)

func ExampleUpsertStreamed() {
var (
testEntites = []User{
{Id: 1, Name: "test1", Age: 10, Email: "[email protected]"},
{Id: 2, Name: "test2", Age: 20, Email: "[email protected]"},
}
testSelect = &[]User{}
g, ctx = errgroup.WithContext(context.Background())
entities = make(chan User, len(testEntites))
logs = getTestLogging()
db = getTestDb(logs)
log = logs.GetLogger()
err error
)
initTestDb(db)

g.Go(func() error {
return UpsertStreamed(ctx, db, entities)
})

for _, entity := range testEntites {
entities <- entity
}

close(entities)
time.Sleep(10 * time.Millisecond)

if err = db.Select(testSelect, "SELECT * FROM user"); err != nil {
log.Fatalf("cannot select from db: %v", err)
}

fmt.Println(*testSelect)

if err = g.Wait(); err != nil {
log.Fatalf("error while upserting entities: %v", err)
}

_ = db.Close()

// Output:
// [{1 test1 10 [email protected]} {2 test2 20 [email protected]}]
}

func ExampleUpsertStreamedWithStatement() {

Check failure on line 54 in database/example_upsert_test.go

View workflow job for this annotation

GitHub Actions / go

tests: ExampleUpsertStreamedWithStatement refers to unknown identifier: UpsertStreamedWithStatement (govet)
var (
testEntites = []User{
{Id: 1, Name: "test1"},
{Id: 2, Name: "test2"},
}
testSelect = &[]User{}
g, ctx = errgroup.WithContext(context.Background())
entities = make(chan User, len(testEntites))
logs = getTestLogging()
db = getTestDb(logs)
log = logs.GetLogger()
stmt = NewUpsertStatement(User{}).SetColumns("id", "name")
err error
)
initTestDb(db)

g.Go(func() error {
return UpsertStreamed(ctx, db, entities, WithUpsertStatement(stmt))
})

for _, entity := range testEntites {
entities <- entity
}

close(entities)
time.Sleep(10 * time.Millisecond)

if err = db.Select(testSelect, "SELECT * FROM user"); err != nil {
log.Fatalf("cannot select from db: %v", err)
}

fmt.Println(*testSelect)

if err = g.Wait(); err != nil {
log.Fatalf("error while upserting entities: %v", err)
}

_ = db.Close()

// Output:
// [{1 test1 0 } {2 test2 0 }]
}

func ExampleUpsertStreamedWithOnUpsert() {

Check failure on line 98 in database/example_upsert_test.go

View workflow job for this annotation

GitHub Actions / go

tests: ExampleUpsertStreamedWithOnUpsert refers to unknown identifier: UpsertStreamedWithOnUpsert (govet)
var (
testEntites = []User{
{Id: 1, Name: "test1", Age: 10, Email: "[email protected]"},
{Id: 2, Name: "test2", Age: 20, Email: "[email protected]"},
}
callback = func(ctx context.Context, affectedRows []any) (err error) {
fmt.Printf("number of affected rows: %d\n", len(affectedRows))
return nil
}
testSelect = &[]User{}
g, ctx = errgroup.WithContext(context.Background())
entities = make(chan User, len(testEntites))
logs = getTestLogging()
db = getTestDb(logs)
log = logs.GetLogger()
err error
)
initTestDb(db)

g.Go(func() error {
return UpsertStreamed(ctx, db, entities, WithOnUpsert(callback))
})

for _, entity := range testEntites {
entities <- entity
}

time.Sleep(1 * time.Second)
close(entities)

if err = db.Select(testSelect, "SELECT * FROM user"); err != nil {
log.Fatalf("cannot select from db: %v", err)
}

fmt.Println(*testSelect)

if err = g.Wait(); err != nil {
log.Fatalf("error while upserting entities: %v", err)
}

_ = db.Close()

// Output:
// number of affected rows: 2
// [{1 test1 10 [email protected]} {2 test2 20 [email protected]}]
}

func ExampleNamedBulkUpsert() {

Check failure on line 146 in database/example_upsert_test.go

View workflow job for this annotation

GitHub Actions / go

tests: ExampleNamedBulkUpsert refers to unknown identifier: NamedBulkUpsert (govet)
var (
testEntites = []User{
{Id: 1, Name: "test1", Age: 10, Email: "[email protected]"},
{Id: 2, Name: "test2", Age: 20, Email: "[email protected]"},
}
testSelect = &[]User{}
g, ctx = errgroup.WithContext(context.Background())
entities = make(chan Entity, len(testEntites))
logs = getTestLogging()
db = getTestDb(logs)
log = logs.GetLogger()
sem = db.GetSemaphoreForTable(TableName(User{}))
err error
)
initTestDb(db)

stmt, placeholders, err := db.QueryBuilder().UpsertStatement(NewUpsertStatement(User{}))
if err != nil {
log.Fatalf("error while building upsert statement: %v", err)
}

g.Go(func() error {
return db.NamedBulkExec(ctx, stmt, placeholders, sem, entities, com.NeverSplit)
})

for _, entity := range testEntites {
entities <- entity
}

time.Sleep(1 * time.Second)
close(entities)

if err = db.Select(testSelect, "SELECT * FROM user"); err != nil {
log.Fatalf("cannot select from db: %v", err)
}

fmt.Println(*testSelect)

if err = g.Wait(); err != nil {
log.Fatalf("error while upserting entities: %v", err)
}

_ = db.Close()

// Output:
// [{1 test1 10 [email protected]} {2 test2 20 [email protected]}]
}

func ExampleNamedBulkUpsertWithOnUpsert() {

Check failure on line 195 in database/example_upsert_test.go

View workflow job for this annotation

GitHub Actions / go

tests: ExampleNamedBulkUpsertWithOnUpsert refers to unknown identifier: NamedBulkUpsertWithOnUpsert (govet)
var (
testEntites = []User{
{Id: 1, Name: "test1", Age: 10, Email: "[email protected]"},
{Id: 2, Name: "test2", Age: 20, Email: "[email protected]"},
}
testSelect = &[]User{}
callback = func(ctx context.Context, affectedRows []Entity) (err error) {
fmt.Printf("number of affected rows: %d\n", len(affectedRows))
return nil
}
g, ctx = errgroup.WithContext(context.Background())
entities = make(chan Entity, len(testEntites))
logs = getTestLogging()
db = getTestDb(logs)
log = logs.GetLogger()
sem = db.GetSemaphoreForTable(TableName(User{}))
err error
)
initTestDb(db)

stmt, placeholders, err := db.QueryBuilder().UpsertStatement(NewUpsertStatement(User{}))
if err != nil {
log.Fatalf("error while building upsert statement: %v", err)
}

g.Go(func() error {
return db.NamedBulkExec(ctx, stmt, placeholders, sem, entities, com.NeverSplit, callback)
})

for _, entity := range testEntites {
entities <- entity
}

time.Sleep(1 * time.Second)
close(entities)

if err = db.Select(testSelect, "SELECT * FROM user"); err != nil {
log.Fatalf("cannot select from db: %v", err)
}

fmt.Println(*testSelect)

if err = g.Wait(); err != nil {
log.Fatalf("error while upserting entities: %v", err)
}

_ = db.Close()

// Output:
// number of affected rows: 2
// [{1 test1 10 [email protected]} {2 test2 20 [email protected]}]
}

func ExampleNamedExecUpsert() {

Check failure on line 249 in database/example_upsert_test.go

View workflow job for this annotation

GitHub Actions / go

tests: ExampleNamedExecUpsert refers to unknown identifier: NamedExecUpsert (govet)
var (
testEntites = []User{
{Id: 1, Name: "test1", Age: 10, Email: "[email protected]"},
{Id: 2, Name: "test2", Age: 20, Email: "[email protected]"},
}
testSelect = &[]User{}
ctx = context.Background()
logs = getTestLogging()
db = getTestDb(logs)
log = logs.GetLogger()
err error
)
initTestDb(db)

stmt, _, err := db.QueryBuilder().UpsertStatement(NewUpsertStatement(User{}))
if err != nil {
log.Fatalf("error while building upsert statement: %v", err)
}

for _, entity := range testEntites {
if _, err = db.NamedExecContext(ctx, stmt, entity); err != nil {
log.Fatalf("error while upserting entity: %v", err)
}
}

if err = db.Select(testSelect, "SELECT * FROM user"); err != nil {
log.Fatalf("cannot select from db: %v", err)
}

fmt.Println(*testSelect)

_ = db.Close()

// Output:
// [{1 test1 10 [email protected]} {2 test2 20 [email protected]}]
}

func ExampleExecUpsert() {

Check failure on line 287 in database/example_upsert_test.go

View workflow job for this annotation

GitHub Actions / go

tests: ExampleExecUpsert refers to unknown identifier: ExecUpsert (govet)
var (
testEntites = [][]any{
{1, "test1", 10, "[email protected]"},
{2, "test2", 20, "[email protected]"},
}
testSelect = &[]User{}
stmt = `INSERT INTO user ("id", "name", "age", "email") VALUES (?, ?, ?, ?) ON CONFLICT DO UPDATE SET "name" = EXCLUDED."name", "age" = EXCLUDED."age", "email" = EXCLUDED."email"`
ctx = context.Background()
logs = getTestLogging()
db = getTestDb(logs)
log = logs.GetLogger()
err error
)
initTestDb(db)

//stmt, _, err := db.QueryBuilder().UpsertStatement(NewUpsertStatement(User{}))
//if err != nil {
// log.Fatalf("error while building upsert statement: %v", err)
//}

for _, entity := range testEntites {
if _, err = db.ExecContext(ctx, stmt, entity...); err != nil {
log.Fatalf("error while upserting entity: %v", err)
}
}

if err = db.Select(testSelect, "SELECT * FROM user"); err != nil {
log.Fatalf("cannot select from db: %v", err)
}

fmt.Println(*testSelect)

_ = db.Close()

// Output:
// [{1 test1 10 [email protected]} {2 test2 20 [email protected]}]
}

0 comments on commit f2b199d

Please sign in to comment.