Transactions: DuckDB and SQLite
Every file of the DuckDB and SQLite versions side by side, with the lines that differ marked. 4 parameter or column types differ in Go.
Configuration
sgl.yamldiffers
DuckDB
SQLite
1
version: 11
version: 12
2
3
databases:3
databases:4
- name: bank4
- name: bank5
dialect: duckdb5
dialect: sqlite6
migrations: db/migrations6
migrations: db/migrations7
queries: db/queries7
queries: db/queries8
go:8
go:9
package: store9
package: store10
out: internal/store10
out: internal/storeMigrations
db/migrations/0001_accounts.up.sqldiffers
DuckDB
SQLite
1
CREATE SEQUENCE account_ids;2
3
CREATE TABLE accounts (1
CREATE TABLE accounts (4
id BIGINT PRIMARY KEY DEFAULT nextval('account_ids'),2
id INTEGER PRIMARY KEY,5
owner VARCHAR NOT NULL,3
owner TEXT NOT NULL,6
balance INTEGER NOT NULL CHECK (balance >= 0)4
balance INTEGER NOT NULL CHECK (balance >= 0)7
);5
);db/migrations/0001_accounts.down.sqldiffers
DuckDB
SQLite
1
DROP TABLE accounts;1
DROP TABLE accounts;2
DROP SEQUENCE account_ids;Queries
OpenAccountdiffers
DuckDB · db/queries/accounts.sql
SQLite · db/queries/accounts.sql
1
-- sgl.query(OpenAccount, one)1
-- sgl.query(OpenAccount, one)2
INSERT INTO accounts (owner, balance) VALUES ($1, $2) RETURNING id;2
INSERT INTO accounts (owner, balance) VALUES (?, ?) RETURNING id;Depositidentical
DuckDB · db/queries/accounts.sql
SQLite · db/queries/accounts.sql
1
-- sgl.query(Deposit, exec)1
-- sgl.query(Deposit, exec)2
UPDATE accounts SET balance = balance + :amount WHERE id = :id;2
UPDATE accounts SET balance = balance + :amount WHERE id = :id;Withdrawidentical
DuckDB · db/queries/accounts.sql
SQLite · db/queries/accounts.sql
1
-- sgl.query(Withdraw, exec)1
-- sgl.query(Withdraw, exec)2
UPDATE accounts SET balance = balance - :amount WHERE id = :id;2
UPDATE accounts SET balance = balance - :amount WHERE id = :id;ListAccountsidentical
DuckDB · db/queries/accounts.sql
SQLite · db/queries/accounts.sql
1
-- sgl.query(ListAccounts, many)1
-- sgl.query(ListAccounts, many)2
SELECT id, owner, balance FROM accounts ORDER BY id;2
SELECT id, owner, balance FROM accounts ORDER BY id;What sgl generates
OpenAccountdiffers
DuckDB
SQLite
1
func (queries *Queries) OpenAccount(ctx context.Context, params OpenAccountParams) (OpenAccountRow, error)1
func (queries *Queries) OpenAccount(ctx context.Context, params OpenAccountParams) (OpenAccountRow, error)2
2
3
type OpenAccountParams struct {3
type OpenAccountParams struct {4
Owner string4
Owner string5
Balance int325
Balance int646
}6
}7
7
8
type OpenAccountRow struct {8
type OpenAccountRow struct {9
ID int64 `json:"id"`9
ID int64 `json:"id"`10
}10
}Depositdiffers
DuckDB
SQLite
1
func (queries *Queries) Deposit(ctx context.Context, params DepositParams) error1
func (queries *Queries) Deposit(ctx context.Context, params DepositParams) error2
2
3
type DepositParams struct {3
type DepositParams struct {4
Amount int324
Amount int645
ID int645
ID int646
}6
}Withdrawdiffers
DuckDB
SQLite
1
func (queries *Queries) Withdraw(ctx context.Context, params WithdrawParams) error1
func (queries *Queries) Withdraw(ctx context.Context, params WithdrawParams) error2
2
3
type WithdrawParams struct {3
type WithdrawParams struct {4
Amount int324
Amount int645
ID int645
ID int646
}6
}ListAccountsdiffers
DuckDB
SQLite
1
func (queries *Queries) ListAccounts(ctx context.Context) ([]ListAccountsRow, error)1
func (queries *Queries) ListAccounts(ctx context.Context) ([]ListAccountsRow, error)2
2
3
type ListAccountsRow struct {3
type ListAccountsRow struct {4
ID int64 `json:"id"`4
ID int64 `json:"id"`5
Owner string `json:"owner"`5
Owner string `json:"owner"`6
Balance int32 `json:"balance"`6
Balance int64 `json:"balance"`7
}7
}The program
example.godiffers
DuckDB
SQLite
1
package main1
package main2
2
3
import (3
import (4
"context"4
"context"5
"database/sql"5
"database/sql"6
"errors"6
"errors"7
"fmt"7
"fmt"8
"io"8
"io"9
9
10
"seagull.sh/seagull/examples/duckdb/023_transactions/internal/store"10
"seagull.sh/seagull/examples/sqlite/023_transactions/internal/store"11
)11
)12
12
13
// run opens two accounts in a transaction it manages itself, then makes two transfers13
// run opens two accounts in a transaction it manages itself, then makes two transfers14
// with RunInTx: one commits, and one breaks the balance check part-way and rolls back.14
// with RunInTx: one commits, and one breaks the balance check part-way and rolls back.15
//15
//16
// Takes database (*sql.DB) which is a migrated database.16
// Takes database (*sql.DB) which is a migrated database.17
// Takes output (io.Writer) which receives each step and the balances.17
// Takes output (io.Writer) which receives each step and the balances.18
//18
//19
// Returns error when a query fails other than as the example expects.19
// Returns error when a query fails other than as the example expects.20
func run(ctx context.Context, database *sql.DB, output io.Writer) error {20
func run(ctx context.Context, database *sql.DB, output io.Writer) error {21
queries := store.New(database)21
queries := store.New(database)22
22
23
pip, gull, err := openAccounts(ctx, database, queries)23
pip, gull, err := openAccounts(ctx, database, queries)24
if err != nil {24
if err != nil {25
return fmt.Errorf("opening accounts: %w", err)25
return fmt.Errorf("opening accounts: %w", err)26
}26
}27
fmt.Fprintln(output, "opened accounts for Pip and Gull: committed")27
fmt.Fprintln(output, "opened accounts for Pip and Gull: committed")28
if err := printBalances(ctx, queries, output); err != nil {28
if err := printBalances(ctx, queries, output); err != nil {29
return err29
return err30
}30
}31
31
32
if err := transfer(ctx, database, queries, pip, gull, 1500); err != nil {32
if err := transfer(ctx, database, queries, pip, gull, 1500); err != nil {33
return fmt.Errorf("first transfer: %w", err)33
return fmt.Errorf("first transfer: %w", err)34
}34
}35
fmt.Fprintln(output, "transferred 1500 from Pip to Gull: committed")35
fmt.Fprintln(output, "transferred 1500 from Pip to Gull: committed")36
if err := printBalances(ctx, queries, output); err != nil {36
if err := printBalances(ctx, queries, output); err != nil {37
return err37
return err38
}38
}39
39
40
// Gull holds 3500, so this transfer credits Pip and then fails the CHECK constraint when40
// Gull holds 3500, so this transfer credits Pip and then fails the CHECK constraint when41
// it debits Gull. RunInTx rolls back, and the credit to Pip that had already run is41
// it debits Gull. RunInTx rolls back, and the credit to Pip that had already run is42
// undone with it.42
// undone with it.43
err = transfer(ctx, database, queries, gull, pip, 9000)43
err = transfer(ctx, database, queries, gull, pip, 9000)44
if err == nil {44
if err == nil {45
return errors.New("an overdrawing transfer committed")45
return errors.New("an overdrawing transfer committed")46
}46
}47
fmt.Fprintf(output, "transferred 9000 from Gull to Pip: rolled back (%v)\n", err)47
fmt.Fprintf(output, "transferred 9000 from Gull to Pip: rolled back (%v)\n", err)48
return printBalances(ctx, queries, output)48
return printBalances(ctx, queries, output)49
}49
}50
50
51
// openAccounts opens both accounts in one transaction that it begins and commits itself.51
// openAccounts opens both accounts in one transaction that it begins and commits itself.52
// WithTx returns a copy of queries bound to that transaction, which suits a transaction52
// WithTx returns a copy of queries bound to that transaction, which suits a transaction53
// begun with options or shared with code that does not use Seagull.53
// begun with options or shared with code that does not use Seagull.54
//54
//55
// Takes database (*sql.DB) which begins the transaction.55
// Takes database (*sql.DB) which begins the transaction.56
// Takes queries (*store.Queries) which are bound to it.56
// Takes queries (*store.Queries) which are bound to it.57
//57
//58
// Returns pipID (int64) which is Pip's account ID.58
// Returns pipID (int64) which is Pip's account ID.59
// Returns gullID (int64) which is Gull's account ID.59
// Returns gullID (int64) which is Gull's account ID.60
// Returns err (error) when an account cannot be opened or the commit fails.60
// Returns err (error) when an account cannot be opened or the commit fails.61
func openAccounts(ctx context.Context, database *sql.DB, queries *store.Queries) (pipID, gullID int64, err error) {61
func openAccounts(ctx context.Context, database *sql.DB, queries *store.Queries) (pipID, gullID int64, err error) {62
transaction, err := database.BeginTx(ctx, nil)62
transaction, err := database.BeginTx(ctx, nil)63
if err != nil {63
if err != nil {64
return 0, 0, err64
return 0, 0, err65
}65
}66
// Rollback after a successful Commit does nothing, so deferring it covers every early66
// Rollback after a successful Commit does nothing, so deferring it covers every early67
// return.67
// return.68
defer func() { _ = transaction.Rollback() }()68
defer func() { _ = transaction.Rollback() }()69
69
70
inTransaction := queries.WithTx(transaction)70
inTransaction := queries.WithTx(transaction)71
pip, err := inTransaction.OpenAccount(ctx, store.OpenAccountParams{Owner: "Pip", Balance: 5000})71
pip, err := inTransaction.OpenAccount(ctx, store.OpenAccountParams{Owner: "Pip", Balance: 5000})72
if err != nil {72
if err != nil {73
return 0, 0, err73
return 0, 0, err74
}74
}75
gull, err := inTransaction.OpenAccount(ctx, store.OpenAccountParams{Owner: "Gull", Balance: 2000})75
gull, err := inTransaction.OpenAccount(ctx, store.OpenAccountParams{Owner: "Gull", Balance: 2000})76
if err != nil {76
if err != nil {77
return 0, 0, err77
return 0, 0, err78
}78
}79
return pip.ID, gull.ID, transaction.Commit()79
return pip.ID, gull.ID, transaction.Commit()80
}80
}81
81
82
// transfer moves an amount between two accounts with RunInTx, which begins a transaction,82
// transfer moves an amount between two accounts with RunInTx, which begins a transaction,83
// passes fn a Queries bound to it, and commits when fn returns nil or rolls back when it83
// passes fn a Queries bound to it, and commits when fn returns nil or rolls back when it84
// returns an error.84
// returns an error.85
//85
//86
// Takes database (*sql.DB) which begins the transaction.86
// Takes database (*sql.DB) which begins the transaction.87
// Takes queries (*store.Queries) which run the transfer.87
// Takes queries (*store.Queries) which run the transfer.88
// Takes from (int64) which is the account to debit.88
// Takes from (int64) which is the account to debit.89
// Takes to (int64) which is the account to credit.89
// Takes to (int64) which is the account to credit.90
// Takes amount (int32) which is the sum to move, in pence.90
// Takes amount (int64) which is the sum to move, in pence.91
//91
//92
// Returns error when either update fails; the transaction is then rolled back.92
// Returns error when either update fails; the transaction is then rolled back.93
func transfer(ctx context.Context, database *sql.DB, queries *store.Queries, from, to int64, amount int32) error {93
func transfer(ctx context.Context, database *sql.DB, queries *store.Queries, from, to, amount int64) error {94
return queries.RunInTx(ctx, database, func(inTransaction *store.Queries) error {94
return queries.RunInTx(ctx, database, func(inTransaction *store.Queries) error {95
if err := inTransaction.Deposit(ctx, store.DepositParams{Amount: amount, ID: to}); err != nil {95
if err := inTransaction.Deposit(ctx, store.DepositParams{Amount: amount, ID: to}); err != nil {96
return err96
return err97
}97
}98
return inTransaction.Withdraw(ctx, store.WithdrawParams{Amount: amount, ID: from})98
return inTransaction.Withdraw(ctx, store.WithdrawParams{Amount: amount, ID: from})99
})99
})100
}100
}101
101
102
// printBalances writes every account's balance on one line.102
// printBalances writes every account's balance on one line.103
//103
//104
// Takes queries (*store.Queries) which read the accounts.104
// Takes queries (*store.Queries) which read the accounts.105
// Takes output (io.Writer) which receives the line.105
// Takes output (io.Writer) which receives the line.106
//106
//107
// Returns error when the accounts cannot be listed.107
// Returns error when the accounts cannot be listed.108
func printBalances(ctx context.Context, queries *store.Queries, output io.Writer) error {108
func printBalances(ctx context.Context, queries *store.Queries, output io.Writer) error {109
accounts, err := queries.ListAccounts(ctx)109
accounts, err := queries.ListAccounts(ctx)110
if err != nil {110
if err != nil {111
return fmt.Errorf("listing accounts: %w", err)111
return fmt.Errorf("listing accounts: %w", err)112
}112
}113
fmt.Fprint(output, " balances:")113
fmt.Fprint(output, " balances:")114
for _, account := range accounts {114
for _, account := range accounts {115
fmt.Fprintf(output, " %s %d", account.Owner, account.Balance)115
fmt.Fprintf(output, " %s %d", account.Owner, account.Balance)116
}116
}117
fmt.Fprintln(output)117
fmt.Fprintln(output)118
return nil118
return nil119
}119
}Go type differences
The same SQL types map to different Go types, because each database reports them differently.
| Query | Field | DuckDB | SQLite |
|---|---|---|---|
| OpenAccount | parameter balance | int32 | int64 |
| Deposit | parameter amount | int32 | int64 |
| Withdraw | parameter amount | int32 | int64 |
| ListAccounts | column balance | int32 | int64 |