Skip to content

Commit ea268b2

Browse files
authored
examples: Port ondeck to MySQL (#680)
* examples: Port ondeck to MySQL * ondeck: Specify length of key column * sqltest: Create a random MySQL database
1 parent b84f31c commit ea268b2

29 files changed

+873
-11
lines changed

examples/ondeck/mysql/city.sql.go

+86
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

examples/ondeck/mysql/db_test.go

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// +build examples
2+
3+
package ondeck
4+
5+
import (
6+
"context"
7+
"database/sql"
8+
"strings"
9+
"testing"
10+
11+
"github.com/kyleconroy/sqlc/internal/sqltest"
12+
13+
"github.com/google/go-cmp/cmp"
14+
)
15+
16+
func join(vals ...string) sql.NullString {
17+
if len(vals) == 0 {
18+
return sql.NullString{}
19+
}
20+
return sql.NullString{
21+
Valid: true,
22+
String: strings.Join(vals, ","),
23+
}
24+
}
25+
26+
func runOnDeckQueries(t *testing.T, q *Queries) {
27+
ctx := context.Background()
28+
29+
_, err := q.CreateCity(ctx, CreateCityParams{
30+
Slug: "san-francisco",
31+
Name: "San Francisco",
32+
})
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
37+
city, err := q.GetCity(ctx, "san-francisco")
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
42+
venueResult, err := q.CreateVenue(ctx, CreateVenueParams{
43+
Slug: "the-fillmore",
44+
Name: "The Fillmore",
45+
City: city.Slug,
46+
SpotifyPlaylist: "spotify:uri",
47+
Status: StatusOpen,
48+
Statuses: join(string(StatusOpen), string(StatusClosed)),
49+
Tags: join("rock", "punk"),
50+
})
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
venueID, err := venueResult.LastInsertId()
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
59+
venue, err := q.GetVenue(ctx, GetVenueParams{
60+
Slug: "the-fillmore",
61+
City: city.Slug,
62+
})
63+
if err != nil {
64+
t.Fatal(err)
65+
}
66+
67+
if diff := cmp.Diff(venue.ID, venueID); diff != "" {
68+
t.Errorf("venue ID mismatch:\n%s", diff)
69+
}
70+
71+
{
72+
actual, err := q.VenueCountByCity(ctx)
73+
if err != nil {
74+
t.Error(err)
75+
}
76+
if diff := cmp.Diff(actual, []VenueCountByCityRow{
77+
{city.Slug, int64(1)},
78+
}); diff != "" {
79+
t.Errorf("venue count mismatch:\n%s", diff)
80+
}
81+
}
82+
83+
{
84+
actual, err := q.ListCities(ctx)
85+
if err != nil {
86+
t.Error(err)
87+
}
88+
if diff := cmp.Diff(actual, []City{city}); diff != "" {
89+
t.Errorf("list city mismatch:\n%s", diff)
90+
}
91+
}
92+
93+
{
94+
actual, err := q.ListVenues(ctx, city.Slug)
95+
if err != nil {
96+
t.Error(err)
97+
}
98+
if diff := cmp.Diff(actual, []Venue{venue}); diff != "" {
99+
t.Errorf("list venue mismatch:\n%s", diff)
100+
}
101+
}
102+
103+
{
104+
err := q.UpdateCityName(ctx, UpdateCityNameParams{
105+
Slug: city.Slug,
106+
Name: "SF",
107+
})
108+
if err != nil {
109+
t.Error(err)
110+
}
111+
}
112+
113+
{
114+
expected := "Fillmore"
115+
err := q.UpdateVenueName(ctx, UpdateVenueNameParams{
116+
Slug: venue.Slug,
117+
Name: expected,
118+
})
119+
if err != nil {
120+
t.Error(err)
121+
}
122+
fresh, err := q.GetVenue(ctx, GetVenueParams{
123+
Slug: venue.Slug,
124+
City: city.Slug,
125+
})
126+
if diff := cmp.Diff(expected, fresh.Name); diff != "" {
127+
t.Errorf("update venue mismatch:\n%s", diff)
128+
}
129+
}
130+
131+
{
132+
err := q.DeleteVenue(ctx, DeleteVenueParams{
133+
Slug: venue.Slug,
134+
Slug_2: venue.Slug,
135+
})
136+
if err != nil {
137+
t.Error(err)
138+
}
139+
}
140+
}
141+
142+
func TestPrepared(t *testing.T) {
143+
t.Parallel()
144+
145+
sdb, cleanup := sqltest.MySQL(t, []string{"schema"})
146+
defer cleanup()
147+
148+
q, err := Prepare(context.Background(), sdb)
149+
if err != nil {
150+
t.Fatal(err)
151+
}
152+
153+
runOnDeckQueries(t, q)
154+
}
155+
156+
func TestQueries(t *testing.T) {
157+
t.Parallel()
158+
159+
sdb, cleanup := sqltest.MySQL(t, []string{"schema"})
160+
defer cleanup()
161+
162+
runOnDeckQueries(t, New(sdb))
163+
}

examples/ondeck/mysql/models.go

+49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/ondeck/mysql/querier.go

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/ondeck/mysql/query/city.sql

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* name: ListCities :many */
2+
SELECT *
3+
FROM city
4+
ORDER BY name;
5+
6+
/* name: GetCity :one */
7+
SELECT *
8+
FROM city
9+
WHERE slug = ?;
10+
11+
/* name: CreateCity :execresult */
12+
INSERT INTO city (
13+
name,
14+
slug
15+
) VALUES (
16+
?,
17+
?
18+
);
19+
20+
/* name: UpdateCityName :exec */
21+
UPDATE city
22+
SET name = ?
23+
WHERE slug = ?;

examples/ondeck/mysql/query/venue.sql

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* name: ListVenues :many */
2+
SELECT *
3+
FROM venue
4+
WHERE city = ?
5+
ORDER BY name;
6+
7+
/* name: DeleteVenue :exec */
8+
DELETE FROM venue
9+
WHERE slug = ? AND slug = ?;
10+
11+
/* name: GetVenue :one */
12+
SELECT *
13+
FROM venue
14+
WHERE slug = ? AND city = ?;
15+
16+
/* name: CreateVenue :execresult */
17+
INSERT INTO venue (
18+
slug,
19+
name,
20+
city,
21+
created_at,
22+
spotify_playlist,
23+
status,
24+
statuses,
25+
tags
26+
) VALUES (
27+
?,
28+
?,
29+
?,
30+
NOW(),
31+
?,
32+
?,
33+
?,
34+
?
35+
);
36+
37+
/* name: UpdateVenueName :exec */
38+
UPDATE venue
39+
SET name = ?
40+
WHERE slug = ?;
41+
42+
/* name: VenueCountByCity :many */
43+
SELECT
44+
city,
45+
count(*)
46+
FROM venue
47+
GROUP BY 1
48+
ORDER BY 1;

0 commit comments

Comments
 (0)