Skip to content
Draft
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
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;
126 changes: 126 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)
);
Loading
Loading