Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: build build-endtoend test test-ci test-examples test-endtoend start psql mysqlsh proto
.PHONY: build build-endtoend test test-ci test-examples test-endtoend start psql mysqlsh proto sqlc-dev ydb test-examples-ydb gen-examples-ydb

build:
go build ./...
Expand All @@ -18,13 +18,21 @@ vet:
test-examples:
go test --tags=examples ./...

ydb-examples: sqlc-dev ydb gen-examples-ydb test-examples-ydb

test-examples-ydb:
YDB_SERVER_URI=localhost:2136 go test -v ./examples/authors/ydb/... -count=1

gen-examples-ydb:
cd examples/authors/ && SQLCDEBUG=1 ~/bin/sqlc-dev generate && cd ../..

build-endtoend:
cd ./internal/endtoend/testdata && go build ./...

test-ci: test-examples build-endtoend vet

sqlc-dev:
go build -o ~/bin/sqlc-dev ./cmd/sqlc/
go build -x -v -o ~/bin/sqlc-dev ./cmd/sqlc/

sqlc-pg-gen:
go build -o ~/bin/sqlc-pg-gen ./internal/tools/sqlc-pg-gen
Expand All @@ -38,6 +46,9 @@ test-json-process-plugin:
start:
docker compose up -d

ydb:
docker compose up -d ydb

fmt:
go fmt ./...

Expand Down
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@ services:
POSTGRES_DB: postgres
POSTGRES_PASSWORD: mysecretpassword
POSTGRES_USER: postgres

ydb:
image: ydbplatform/local-ydb:latest
ports:
- "2135:2135"
- "2136:2136"
- "8765:8765"
restart: always
hostname: localhost
environment:
- YDB_USE_IN_MEMORY_PDISKS=true
- GRPC_TLS_PORT=2135
- GRPC_PORT=2136
- MON_PORT=8765

12 changes: 12 additions & 0 deletions examples/authors/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ sql:
go:
package: authors
out: sqlite
- name: ydb
schema: ydb/schema.sql
queries: ydb/query.sql
engine: ydb
gen:
go:
package: authors
out: ydb
emit_json_tags: true
sql_package: ydb-go-sdk


rules:
- name: postgresql-query-too-costly
message: "Too costly"
Expand Down
26 changes: 26 additions & 0 deletions examples/authors/ydb/db.go

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

100 changes: 100 additions & 0 deletions examples/authors/ydb/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package authors

import (
"context"
"testing"

"github.com/sqlc-dev/sqlc/internal/sqltest/local"
_ "github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/query"
)

func ptr(s string) *string {
return &s
}

func TestAuthors(t *testing.T) {
ctx := context.Background()

db := local.YDB(t, []string{"schema.sql"})
defer db.Close(ctx)

q := New(db.Query())

t.Run("InsertAuthors", func(t *testing.T) {
authorsToInsert := []CreateOrUpdateAuthorParams{
{P0: 1, P1: "Leo Tolstoy", P2: ptr("Russian writer, author of \"War and Peace\"")},
{P0: 2, P1: "Alexander Pushkin", P2: ptr("Author of \"Eugene Onegin\"")},
{P0: 3, P1: "Alexander Pushkin", P2: ptr("Russian poet, playwright, and prose writer")},
{P0: 4, P1: "Fyodor Dostoevsky", P2: ptr("Author of \"Crime and Punishment\"")},
{P0: 5, P1: "Nikolai Gogol", P2: ptr("Author of \"Dead Souls\"")},
{P0: 6, P1: "Anton Chekhov", P2: nil},
{P0: 7, P1: "Ivan Turgenev", P2: ptr("Author of \"Fathers and Sons\"")},
{P0: 8, P1: "Mikhail Lermontov", P2: nil},
{P0: 9, P1: "Daniil Kharms", P2: ptr("Absurdist, writer and poet")},
{P0: 10, P1: "Maxim Gorky", P2: ptr("Author of \"At the Bottom\"")},
{P0: 11, P1: "Vladimir Mayakovsky", P2: nil},
{P0: 12, P1: "Sergei Yesenin", P2: ptr("Russian lyric poet")},
{P0: 13, P1: "Boris Pasternak", P2: ptr("Author of \"Doctor Zhivago\"")},
}

for _, author := range authorsToInsert {
if err := q.CreateOrUpdateAuthor(ctx, author, query.WithIdempotent()); err != nil {
t.Fatalf("failed to insert author %q: %v", author.P1, err)
}
}
})

t.Run("ListAuthors", func(t *testing.T) {
authors, err := q.ListAuthors(ctx)
if err != nil {
t.Fatal(err)
}
if len(authors) == 0 {
t.Fatal("expected at least one author, got none")
}
t.Log("Authors:")
for _, a := range authors {
bio := "Null"
if a.Bio != nil {
bio = *a.Bio
}
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
}
})

t.Run("GetAuthor", func(t *testing.T) {
singleAuthor, err := q.GetAuthor(ctx, 10)
if err != nil {
t.Fatal(err)
}
bio := "Null"
if singleAuthor.Bio != nil {
bio = *singleAuthor.Bio
}
t.Logf("- ID: %d | Name: %s | Bio: %s", singleAuthor.ID, singleAuthor.Name, bio)
})

t.Run("Delete All Authors", func(t *testing.T) {
var i uint64
for i = 1; i <= 13; i++ {
if err := q.DeleteAuthor(ctx, i, query.WithIdempotent()); err != nil {
t.Fatalf("failed to delete author: %v", err)
}
}
authors, err := q.ListAuthors(ctx)
if err != nil {
t.Fatal(err)
}
if len(authors) != 0 {
t.Fatalf("expected no authors, got %d", len(authors))
}
})

t.Run("Drop Table Authors", func(t *testing.T) {
err := q.DropTable(ctx)
if err != nil {
t.Fatal(err)
}
})
}
11 changes: 11 additions & 0 deletions examples/authors/ydb/models.go

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

15 changes: 15 additions & 0 deletions examples/authors/ydb/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $p0 LIMIT 1;

-- name: ListAuthors :many
SELECT * FROM authors ORDER BY name;

-- name: CreateOrUpdateAuthor :exec
UPSERT INTO authors (id, name, bio) VALUES ($p0, $p1, $p2);

-- name: DeleteAuthor :exec
DELETE FROM authors WHERE id = $p0;

-- name: DropTable :exec
DROP TABLE IF EXISTS authors;
114 changes: 114 additions & 0 deletions examples/authors/ydb/query.sql.go

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

6 changes: 6 additions & 0 deletions examples/authors/ydb/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE authors (
id Uint64,
name Utf8 NOT NULL,
bio Utf8,
PRIMARY KEY (id)
);
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ require (
github.com/tetratelabs/wazero v1.9.0
github.com/wasilibs/go-pgquery v0.0.0-20250409022910-10ac41983c07
github.com/xeipuuv/gojsonschema v1.2.0
github.com/ydb-platform/ydb-go-sdk/v3 v3.115.3
github.com/ydb-platform/yql-parsers v0.0.0-20250911122629-e8a65d734cbd
golang.org/x/sync v0.16.0
google.golang.org/grpc v1.75.0
google.golang.org/protobuf v1.36.8
Expand All @@ -35,6 +37,7 @@ require (
cel.dev/expr v0.24.0 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/golang-jwt/jwt/v4 v4.5.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
Expand All @@ -45,6 +48,7 @@ require (
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jonboulle/clockwork v0.5.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pingcap/errors v0.11.5-0.20240311024730-e056997136bb // indirect
Expand All @@ -56,6 +60,7 @@ require (
github.com/wasilibs/wazero-helpers v0.0.0-20240620070341-3dff1577cd52 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/ydb-platform/ydb-go-genproto v0.0.0-20241112172322-ea1f63298f77 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
Expand Down
Loading