Skip to content

Commit 199cfac

Browse files
committed
fix: update panic
1 parent 75ee59c commit 199cfac

File tree

15 files changed

+582
-36
lines changed

15 files changed

+582
-36
lines changed

examples/update/noparams/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/update/noparams/db_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//go:build examples
2+
// +build examples
3+
4+
package update
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/kyleconroy/sqlc/internal/sqltest"
11+
)
12+
13+
func TestUpdate(t *testing.T) {
14+
sdb, cleanup := sqltest.MySQL(t, []string{"schema.sql"})
15+
defer cleanup()
16+
17+
ctx := context.Background()
18+
db := New(sdb)
19+
20+
_, err := db.CreateT1(ctx, CreateT1Params{
21+
UserID: int32(2),
22+
Name: "",
23+
})
24+
if err != nil {
25+
t.Fatal(err)
26+
}
27+
28+
// get the data we just inserted
29+
oldData, err := db.GetT1(ctx, int32(2))
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
34+
if oldData.Name != "" {
35+
t.Fatal("create fail")
36+
}
37+
38+
_, err = db.CreateT2(ctx, CreateT2Params{
39+
Email: "test@gmail.com",
40+
Name: "test",
41+
})
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
46+
_, err = db.CreateT3(ctx, CreateT3Params{
47+
UserID: int32(2),
48+
Email: "test@gmail.com",
49+
})
50+
if err != nil {
51+
t.Fatal(err)
52+
}
53+
54+
err = db.UpdateAll(ctx)
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
59+
newData, err := db.GetT1(ctx, int32(2))
60+
if err != nil {
61+
t.Fatal(err)
62+
}
63+
64+
if newData.Name != "test" {
65+
t.Fatal("update fail")
66+
}
67+
}

examples/update/noparams/models.go

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/update/noparams/query.sql

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* name: CreateT1 :execresult */
2+
INSERT INTO
3+
t1 (user_id, name)
4+
VALUES
5+
(?, ?);
6+
7+
/* name: CreateT2 :execresult */
8+
INSERT INTO
9+
t2 (email, name)
10+
VALUES
11+
(?, ?);
12+
13+
/* name: CreateT3 :execresult */
14+
INSERT INTO
15+
t3 (user_id, email)
16+
VALUES
17+
(?, ?);
18+
19+
/* name: UpdateAll :exec */
20+
UPDATE
21+
t1
22+
INNER JOIN t3 ON t3.user_id = t1.user_id
23+
INNER JOIN t2 ON t2.email = t3.email
24+
SET
25+
t1.name = t2.name
26+
WHERE
27+
t1.name = '';
28+
29+
/* name: GetT1 :one */
30+
SELECT
31+
*
32+
FROM
33+
t1
34+
WHERE
35+
user_id = ?
36+
LIMIT
37+
1;

examples/update/noparams/query.sql.go

Lines changed: 93 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/update/noparams/schema.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CREATE TABLE t1 (
2+
user_id int NOT NULL,
3+
name varchar(255) NOT NULL
4+
);
5+
6+
CREATE TABLE t2 (
7+
email varchar(255) NOT NULL,
8+
name varchar(255) NOT NULL
9+
);
10+
11+
CREATE TABLE t3 (
12+
user_id int NOT NULL,
13+
email varchar(255) NOT NULL
14+
);

examples/update/sqlc.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"version": "2",
3+
"sql": [
4+
{
5+
"schema": "withparams/schema.sql",
6+
"queries": "withparams/query.sql",
7+
"engine": "mysql",
8+
"gen": {
9+
"go": {
10+
"package": "update",
11+
"out": "withparams"
12+
}
13+
}
14+
},
15+
{
16+
"schema": "noparams/schema.sql",
17+
"queries": "noparams/query.sql",
18+
"engine": "mysql",
19+
"gen": {
20+
"go": {
21+
"package": "update",
22+
"out": "noparams"
23+
}
24+
}
25+
}
26+
]
27+
}

examples/update/withparams/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/update/withparams/db_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//go:build examples
2+
// +build examples
3+
4+
package update
5+
6+
import (
7+
"context"
8+
"testing"
9+
"time"
10+
11+
"github.com/kyleconroy/sqlc/internal/sqltest"
12+
)
13+
14+
func TestAuthor(t *testing.T) {
15+
sdb, cleanup := sqltest.MySQL(t, []string{"schema.sql"})
16+
defer cleanup()
17+
18+
ctx := context.Background()
19+
db := New(sdb)
20+
21+
// create an author
22+
result, err := db.CreateAuthor(ctx, CreateAuthorParams{
23+
Name: "Brian Kernighan",
24+
DeletedAt: time.Now(),
25+
UpdatedAt: time.Now(),
26+
})
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
authorID, err := result.LastInsertId()
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
t.Log(authorID)
35+
36+
// get the author we just inserted
37+
fetchedAuthor, err := db.GetAuthor(ctx, authorID)
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
42+
// create a book
43+
_, err = db.CreateBook(ctx, true)
44+
if err != nil {
45+
t.Fatal(err)
46+
}
47+
48+
err = db.DeleteAuthor(ctx, "Brian Kernighan")
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
53+
// get the author we just inserted
54+
newFetchedAuthor, err := db.GetAuthor(ctx, authorID)
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
t.Log(fetchedAuthor)
59+
if newFetchedAuthor.DeletedAt.Unix() != fetchedAuthor.DeletedAt.Unix() && newFetchedAuthor.DeletedAt.Unix() != newFetchedAuthor.UpdatedAt.Unix() {
60+
t.Fatal("update fail")
61+
}
62+
}

0 commit comments

Comments
 (0)