Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ jobs:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
mysql:
image: mysql:8
env:
MYSQL_ROOT_PASSWORD: mysecretpassword
MYSQL_DATABASE: mysql
ports:
- 3306:3306

steps:
- uses: actions/checkout@v2
Expand All @@ -37,3 +44,8 @@ jobs:
PG_DATABASE: postgres
PG_PASSWORD: postgres
PG_PORT: ${{ job.services.postgres.ports['5432'] }}
MYSQL_DATABASE: mysql
MYSQL_HOST: localhost
MYSQL_PORT: ${{ job.services.mysql.ports['5432'] }}
MYSQL_ROOT_PASSWORD: mysecretpassword

167 changes: 167 additions & 0 deletions examples/booktest/mysql/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// +build examples

package booktest

import (
"context"
"testing"
"time"

"github.com/kyleconroy/sqlc/internal/sqltest"
)

func TestBooks(t *testing.T) {
db, cleanup := sqltest.MySQL(t, []string{"schema.sql"})
defer cleanup()

ctx := context.Background()
dq := New(db)

// create an author
result, err := dq.CreateAuthor(ctx, "Unknown Master")
if err != nil {
t.Fatal(err)
}
authorID, err := result.LastInsertId()
if err != nil {
t.Fatal(err)
}

// create transaction
tx, err := db.Begin()
if err != nil {
t.Fatal(err)
}

tq := dq.WithTx(tx)

// save first book
now := time.Now()
_, err = tq.CreateBook(ctx, CreateBookParams{
AuthorID: int32(authorID),
Isbn: "1",
Title: "my book title",
BookType: "FICTION",
Yr: 2016,
Available: now,
})
if err != nil {
t.Fatal(err)
}

// save second book
result, err = tq.CreateBook(ctx, CreateBookParams{
AuthorID: int32(authorID),
Isbn: "2",
Title: "the second book",
BookType: "FICTION",
Yr: 2016,
Available: now,
Tags: "cool,unique",
})
if err != nil {
t.Fatal(err)
}
bookOneID, err := result.LastInsertId()
if err != nil {
t.Fatal(err)
}

// update the title and tags
err = tq.UpdateBook(ctx, UpdateBookParams{
BookID: int32(bookOneID),
Title: "changed second title",
Tags: "cool,disastor",
})
if err != nil {
t.Fatal(err)
}

// save third book
_, err = tq.CreateBook(ctx, CreateBookParams{
AuthorID: int32(authorID),
Isbn: "3",
Title: "the third book",
BookType: "FICTION",
Yr: 2001,
Available: now,
Tags: "cool",
})
if err != nil {
t.Fatal(err)
}

// save fourth book
result, err = tq.CreateBook(ctx, CreateBookParams{
AuthorID: int32(authorID),
Isbn: "4",
Title: "4th place finisher",
BookType: "NONFICTION",
Yr: 2011,
Available: now,
Tags: "other",
})
if err != nil {
t.Fatal(err)
}
bookThreeID, err := result.LastInsertId()
if err != nil {
t.Fatal(err)
}

// tx commit
err = tx.Commit()
if err != nil {
t.Fatal(err)
}

// upsert, changing ISBN and title
err = dq.UpdateBookISBN(ctx, UpdateBookISBNParams{
BookID: int32(bookThreeID),
Isbn: "NEW ISBN",
Title: "never ever gonna finish, a quatrain",
Tags: "someother",
})
if err != nil {
t.Fatal(err)
}

// retrieve first book
books0, err := dq.BooksByTitleYear(ctx, BooksByTitleYearParams{
Title: "my book title",
Yr: 2016,
})
if err != nil {
t.Fatal(err)
}
for _, book := range books0 {
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Format(time.RFC822Z))
author, err := dq.GetAuthor(ctx, book.AuthorID)
if err != nil {
t.Fatal(err)
}
t.Logf("Book %d author: %s\n", book.BookID, author.Name)
}

// find a book with either "cool" or "other" tag
t.Logf("---------\nTag search results:\n")
res, err := dq.BooksByTags(ctx, "cool")
if err != nil {
t.Fatal(err)
}
for _, ab := range res {
t.Logf("Book %d: '%s', Author: '%s', ISBN: '%s' Tags: '%v'\n", ab.BookID, ab.Title, ab.Name, ab.Isbn, ab.Tags)
}

// TODO: call say_hello(varchar)

// get book 4 and delete
b5, err := dq.GetBook(ctx, int32(bookThreeID))
if err != nil {
t.Fatal(err)
}
if err := dq.DeleteBook(ctx, b5.BookID); err != nil {
t.Fatal(err)
}

}
30 changes: 5 additions & 25 deletions examples/booktest/mysql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 18 additions & 5 deletions examples/booktest/mysql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,25 @@ WHERE book_id = ?;
SELECT * FROM books
WHERE title = ? AND yr = ?;

/* name: CreateAuthor :exec */
/* name: BooksByTags :many */
SELECT
book_id,
title,
name,
isbn,
tags
FROM books
LEFT JOIN authors ON books.author_id = authors.author_id
WHERE tags = ?;

/* name: CreateAuthor :execresult */
INSERT INTO authors (name) VALUES (?);

/* name: CreateBook :exec */
/* name: CreateBook :execresult */
INSERT INTO books (
author_id,
isbn,
booktype,
book_type,
title,
yr,
available,
Expand All @@ -32,6 +43,7 @@ INSERT INTO books (
?,
?,
?,
?,
?
);

Expand All @@ -42,9 +54,10 @@ WHERE book_id = ?;

/* name: UpdateBookISBN :exec */
UPDATE books
SET title = ?, tags = :book_tags, isbn = ?
SET title = ?, tags = ?, isbn = ?
WHERE book_id = ?;

/* name: DeleteAuthorBeforeYear :exec */
DELETE FROM books
WHERE yr < sqlc.arg(min_publish_year) AND author_id = ?;
WHERE yr < ? AND author_id = ?;
-- WHERE yr < sqlc.arg(min_publish_year) AND author_id = ?;
Loading