Skip to content

Commit 02fb746

Browse files
author
Viktor Pentyukhov
committed
Rewrited some examples & internal/sqltest/local/ydb logic to be a bit more trivial
1 parent 4acf1a0 commit 02fb746

File tree

4 files changed

+34
-37
lines changed

4 files changed

+34
-37
lines changed

examples/authors/ydb/db_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,11 @@ func TestAuthors(t *testing.T) {
169169
t.Fatalf("expected no authors, got %d", len(authors))
170170
}
171171
})
172+
173+
t.Run("Drop Table Authors", func(t *testing.T) {
174+
err := q.DropTable(ctx)
175+
if err != nil {
176+
t.Fatal(err)
177+
}
178+
})
172179
}

examples/authors/ydb/query.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ UPSERT INTO authors (id, name, bio) VALUES ($p0, $p1, $p2) RETURNING bio;
2929
DELETE FROM authors WHERE id = $p0;
3030

3131
-- name: UpdateAuthorByID :queryrows
32-
UPDATE authors SET name = $p0, bio = $p1 WHERE id = $p2 RETURNING *;
32+
UPDATE authors SET name = $p0, bio = $p1 WHERE id = $p2 RETURNING *;
33+
34+
-- name: DropTable :exec
35+
DROP TABLE IF EXISTS authors;

examples/authors/ydb/query.sql.go

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

internal/sqltest/local/ydb.go

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package local
33
import (
44
"context"
55
"fmt"
6-
"hash/fnv"
76
"math/rand"
87
"os"
98
"testing"
@@ -13,7 +12,6 @@ import (
1312
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
1413
"github.com/ydb-platform/ydb-go-sdk/v3"
1514
"github.com/ydb-platform/ydb-go-sdk/v3/query"
16-
"github.com/ydb-platform/ydb-go-sdk/v3/sugar"
1715
)
1816

1917
func init() {
@@ -37,11 +35,11 @@ func ReadOnlyYDBTLS(t *testing.T, migrations []string) *ydb.Driver {
3735
}
3836

3937
func link_YDB(t *testing.T, migrations []string, rw bool, tls bool) *ydb.Driver {
40-
t.Helper()
41-
4238
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
4339
defer cancel()
4440

41+
t.Helper()
42+
4543
dbuiri := os.Getenv("YDB_SERVER_URI")
4644
if dbuiri == "" {
4745
t.Skip("YDB_SERVER_URI is empty")
@@ -52,34 +50,12 @@ func link_YDB(t *testing.T, migrations []string, rw bool, tls bool) *ydb.Driver
5250
baseDB = "/local"
5351
}
5452

55-
var seed []string
56-
files, err := sqlpath.Glob(migrations)
57-
if err != nil {
58-
t.Fatal(err)
59-
}
60-
h := fnv.New64()
61-
for _, f := range files {
62-
blob, err := os.ReadFile(f)
63-
if err != nil {
64-
t.Fatal(err)
65-
}
66-
h.Write(blob)
67-
seed = append(seed, migrate.RemoveRollbackStatements(string(blob)))
68-
}
69-
70-
var name string
71-
if rw {
72-
name = fmt.Sprintf("sqlc_test_%s", "test_new")
73-
} else {
74-
name = fmt.Sprintf("sqlc_test_%x", h.Sum(nil))
75-
}
7653
var connectionString string
7754
if tls {
7855
connectionString = fmt.Sprintf("grpcs://%s%s", dbuiri, baseDB)
7956
} else {
8057
connectionString = fmt.Sprintf("grpc://%s%s", dbuiri, baseDB)
8158
}
82-
t.Logf("→ Opening YDB connection: %s", connectionString)
8359

8460
db, err := ydb.Open(ctx, connectionString,
8561
ydb.WithInsecure(),
@@ -89,20 +65,19 @@ func link_YDB(t *testing.T, migrations []string, rw bool, tls bool) *ydb.Driver
8965
t.Fatalf("failed to open YDB connection: %s", err)
9066
}
9167

92-
prefix := fmt.Sprintf("%s/%s", baseDB, name)
93-
t.Logf("→ Using prefix: %s", prefix)
94-
95-
err = sugar.RemoveRecursive(ctx, db, prefix)
68+
files, err := sqlpath.Glob(migrations)
9669
if err != nil {
97-
t.Logf("Warning: failed to remove old data: %s", err)
70+
t.Fatal(err)
9871
}
9972

100-
t.Log("→ Applying migrations to prefix: ", prefix)
73+
for _, f := range files {
74+
blob, err := os.ReadFile(f)
75+
if err != nil {
76+
t.Fatal(err)
77+
}
78+
stmt := migrate.RemoveRollbackStatements(string(blob))
10179

102-
for _, stmt := range seed {
103-
err := db.Query().Exec(ctx, stmt,
104-
query.WithTxControl(query.EmptyTxControl()),
105-
)
80+
err = db.Query().Exec(ctx, stmt, query.WithTxControl(query.EmptyTxControl()))
10681
if err != nil {
10782
t.Fatalf("failed to apply migration: %s\nSQL: %s", err, stmt)
10883
}

0 commit comments

Comments
 (0)