Skip to content

Commit

Permalink
Merge pull request cockroachdb#8261 from cockroachdb/gorm-sample-updates
Browse files Browse the repository at this point in the history
Updates to GORM hello world sample and tutorial
  • Loading branch information
Eric Harmeling authored Sep 16, 2020
2 parents 5bd180f + 2f17598 commit c7ab8b5
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 523 deletions.
41 changes: 0 additions & 41 deletions _includes/v20.2/app/gorm-basic-sample.go

This file was deleted.

122 changes: 122 additions & 0 deletions _includes/v20.2/app/gorm-basic-sample/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package main

import (
"context"
"fmt"
"log"
"time"

// Import GORM-related packages.
"github.com/cockroachdb/cockroach-go/crdb/crdbgorm"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)

// Account is our model, which corresponds to the "accounts" database
// table.
type Account struct {
ID int `gorm:"primary_key"`
Balance int
}

// Functions of type `txnFunc` are passed as arguments to the
// `ExecuteTx` wrapper that handles transaction retries
type txnFunc func(*gorm.DB) error

func transferFunds(db *gorm.DB, fromID int, toID int, amount int) error {
var fromAccount Account
var toAccount Account

db.First(&fromAccount, fromID)
db.First(&toAccount, toID)

if fromAccount.Balance < amount {
return fmt.Errorf("account %d balance %d is lower than transfer amount %d", fromAccount.ID, fromAccount.Balance, amount)
}

fromAccount.Balance -= amount
toAccount.Balance += amount

if err := db.Save(&fromAccount).Error; err != nil {
return err
}
if err := db.Save(&toAccount).Error; err != nil {
return err
}
return nil
}

func main() {
// Connect to the "bank" database as the "maxroach" user.
const addr = "postgresql://maxroach@localhost:26257/bank?ssl=true&sslmode=require&sslrootcert=certs/ca.crt&sslkey=certs/client.maxroach.key&sslcert=certs/client.maxroach.crt"
db, err := gorm.Open("postgres", addr)
if err != nil {
log.Fatal(err)
}
defer db.Close()

// Set to `true` and GORM will print out all DB queries.
db.LogMode(false)

// Automatically create the "accounts" table based on the Account
// model.
db.AutoMigrate(&Account{})

// Insert two rows into the "accounts" table.
var fromID = 1
var toID = 2
db.Create(&Account{ID: fromID, Balance: 1000})
db.Create(&Account{ID: toID, Balance: 250})

// The sequence of steps in this section is:
// 1. Print account balances.
// 2. Set up some Accounts and transfer funds between them inside
// a transaction.
// 3. Print account balances again to verify the transfer occurred.

// Print balances before transfer.
printBalances(db)

// The amount to be transferred between the accounts.
var amount = 100

// Transfer funds between accounts. To handle potential
// transaction retry errors, we wrap the call to `transferFunds`
// in `crdbgorm.ExecuteTx`, a helper function for GORM which
// implements a retry loop
if err := crdbgorm.ExecuteTx(context.Background(), db, nil,
func(*gorm.DB) error {
return transferFunds(db, fromID, toID, amount)
},
); err != nil {
// For information and reference documentation, see:
// https://www.cockroachlabs.com/docs/stable/error-handling-and-troubleshooting.html
fmt.Println(err)
}

// Print balances after transfer to ensure that it worked.
printBalances(db)

// Delete accounts so we can start fresh when we want to run this
// program again.
deleteAccounts(db)
}

func printBalances(db *gorm.DB) {
var accounts []Account
db.Find(&accounts)
fmt.Printf("Balance at '%s':\n", time.Now())
for _, account := range accounts {
fmt.Printf("%d %d\n", account.ID, account.Balance)
}
}

func deleteAccounts(db *gorm.DB) error {
// Used to tear down the accounts table so we can re-run this
// program.
err := db.Exec("DELETE from accounts where ID > 0").Error
if err != nil {
return err
}
return nil
}
206 changes: 0 additions & 206 deletions _includes/v20.2/app/gorm-sample.go

This file was deleted.

Loading

0 comments on commit c7ab8b5

Please sign in to comment.