Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ import (
"github.com/jackc/pgx/v4"
)

func initTable(ctx context.Context, tx pgx.Tx) error {
// Dropping existing table if it exists
log.Println("Drop existing accounts table if necessary.")
if _, err := tx.Exec(ctx, "DROP TABLE IF EXISTS accounts"); err != nil {
return err
}

// Create the accounts table
log.Println("Creating accounts table.")
if _, err := tx.Exec(ctx,
"CREATE TABLE accounts (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), balance INT8)"); err != nil {
return err
}
return nil
}

func insertRows(ctx context.Context, tx pgx.Tx, accts [4]uuid.UUID) error {
// Insert four rows into the "accounts" table.
log.Println("Creating new rows...")
Expand Down Expand Up @@ -85,6 +101,11 @@ func main() {
}
defer conn.Close(context.Background())

// Set up table
err = crdbpgx.ExecuteTx(context.Background(), conn, pgx.TxOptions{}, func(tx pgx.Tx) error {
return initTable(context.Background(), tx)
})

// Insert initial rows
var accounts [4]uuid.UUID
for i := 0; i < len(accounts); i++ {
Expand Down