Skip to content

Commit 055e78b

Browse files
authored
Merge pull request #729 from ydb-platform/examples-basic-on-ci
ci: run examples/basic on CI
2 parents ab5ac0a + 8d852ec commit 055e78b

File tree

7 files changed

+107
-176
lines changed

7 files changed

+107
-176
lines changed

.github/workflows/breaking.yml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,3 @@ jobs:
2929
path: changes.txt
3030
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3131
header: gorelease
32-
examples:
33-
concurrency:
34-
group: examples-${{ github.ref }}
35-
cancel-in-progress: true
36-
runs-on: ubuntu-latest
37-
steps:
38-
- name: Install Go
39-
uses: actions/setup-go@v3
40-
with:
41-
go-version: "1.20"
42-
- name: Checkout code
43-
uses: actions/checkout@v3
44-
- name: Build
45-
run: |
46-
cd examples
47-
go build ./...

.github/workflows/examples.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: examples
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- release-*
7+
pull_request:
8+
workflow_dispatch:
9+
jobs:
10+
examples:
11+
concurrency:
12+
group: examples-${{ github.ref }}-${{ matrix.go-version }}-${{ matrix.ydb-version }}-${{ matrix.driver }}
13+
cancel-in-progress: true
14+
runs-on: ubuntu-latest
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
go-version: [ 1.17.x, 1.18.x, 1.19.x, 1.20.x ]
19+
ydb-version: [ 22.5, 23.1 ]
20+
driver: [ native, database_sql, gorm ]
21+
exclude:
22+
- driver: database_sql
23+
go-version: 1.17.x
24+
- driver: gorm
25+
go-version: 1.17.x
26+
services:
27+
ydb:
28+
image: cr.yandex/yc/yandex-docker-local-ydb:${{ matrix.ydb-version }}-slim
29+
ports:
30+
- 2135:2135
31+
- 2136:2136
32+
- 8765:8765
33+
volumes:
34+
- /tmp/ydb_certs:/ydb_certs
35+
env:
36+
YDB_LOCAL_SURVIVE_RESTART: true
37+
YDB_USE_IN_MEMORY_PDISKS: true
38+
options: '-h localhost'
39+
env:
40+
OS: ubuntu-latest
41+
GO: ${{ matrix.go-version }}
42+
YDB_CONNECTION_STRING: grpc://localhost:2136/local
43+
YDB_ANONYMOUS_CREDENTIALS: 1
44+
steps:
45+
- name: Checkout code
46+
uses: actions/checkout@v3
47+
- name: Install Go
48+
uses: actions/setup-go@v3
49+
with:
50+
go-version: ${{ matrix.go-version }}
51+
cache: true
52+
- name: Run examples for ${{ matrix.driver }}
53+
working-directory: ./examples/basic/${{ matrix.driver }}
54+
run: go run .

examples/basic/database_sql/main.go

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,40 @@ package main
33
import (
44
"context"
55
"database/sql"
6-
"flag"
76
"fmt"
87
"os"
8+
"path"
99
"time"
1010

11+
environ "github.com/ydb-platform/ydb-go-sdk-auth-environ"
12+
1113
"github.com/ydb-platform/ydb-go-sdk/v3"
1214
"github.com/ydb-platform/ydb-go-sdk/v3/sugar"
1315
)
1416

15-
var dsn string
16-
17-
func init() {
18-
required := []string{"ydb"}
19-
flagSet := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
20-
flagSet.Usage = func() {
21-
out := flagSet.Output()
22-
_, _ = fmt.Fprintf(out, "Usage:\n%s [options]\n", os.Args[0])
23-
_, _ = fmt.Fprintf(out, "\nOptions:\n")
24-
flagSet.PrintDefaults()
25-
}
26-
flagSet.StringVar(&dsn,
27-
"ydb", "",
28-
"YDB connection string",
29-
)
30-
if err := flagSet.Parse(os.Args[1:]); err != nil {
31-
flagSet.Usage()
32-
os.Exit(1)
33-
}
34-
flagSet.Visit(func(f *flag.Flag) {
35-
for i, arg := range required {
36-
if arg == f.Name {
37-
required = append(required[:i], required[i+1:]...)
38-
}
39-
}
40-
})
41-
if len(required) > 0 {
42-
fmt.Printf("\nSome required options not defined: %v\n\n", required)
43-
flagSet.Usage()
44-
os.Exit(1)
45-
}
46-
}
47-
4817
func main() {
4918
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
5019
defer cancel()
5120

52-
cc, err := ydb.Open(ctx, dsn)
21+
dsn, exists := os.LookupEnv("YDB_CONNECTION_STRING")
22+
if !exists {
23+
panic("YDB_CONNECTION_STRING environment variable not defined")
24+
}
25+
26+
cc, err := ydb.Open(ctx,
27+
dsn,
28+
environ.WithEnvironCredentials(ctx),
29+
)
5330
if err != nil {
5431
panic(fmt.Errorf("connect error: %w", err))
5532
}
5633
defer func() { _ = cc.Close(ctx) }()
5734

35+
prefix := path.Join(cc.Name(), "database_sql")
36+
5837
c, err := ydb.Connector(cc,
5938
ydb.WithAutoDeclare(),
39+
ydb.WithTablePathPrefix(prefix),
6040
)
6141
if err != nil {
6242
panic(err)
@@ -70,7 +50,7 @@ func main() {
7050
db.SetMaxIdleConns(50)
7151
db.SetConnMaxIdleTime(time.Second)
7252

73-
err = sugar.RemoveRecursive(ctx, cc, "")
53+
err = sugar.RemoveRecursive(ctx, cc, prefix)
7454
if err != nil {
7555
panic(fmt.Errorf("remove recursive failed: %w", err))
7656
}

examples/basic/gorm/main.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
package main
22

33
import (
4-
"errors"
4+
"context"
55
"log"
66
"os"
7+
"time"
78

89
ydb "github.com/ydb-platform/gorm-driver"
9-
"gorm.io/driver/postgres"
10-
"gorm.io/driver/sqlite"
10+
environ "github.com/ydb-platform/ydb-go-sdk-auth-environ"
1111
"gorm.io/gorm"
1212
"gorm.io/gorm/clause"
1313
"gorm.io/gorm/logger"
1414
)
1515

16-
//nolint:lll
17-
func initDB(cfg *gorm.Config) (*gorm.DB, error) {
18-
// docker run -it postgres psql -h 127.0.0.1 -p 5432 -U postgres -d postgres
19-
// POSTGRES_CONNECTION_STRING="user=postgres password=mysecretpassword dbname=postgres host=127.0.0.1 port=5432 sslmode=disable"
20-
if dsn, has := os.LookupEnv("POSTGRES_CONNECTION_STRING"); has {
21-
return gorm.Open(postgres.Open(dsn), cfg)
22-
}
23-
// SQLITE_CONNECTION_STRING=./test.db
24-
if dsn, has := os.LookupEnv("SQLITE_CONNECTION_STRING"); has {
25-
return gorm.Open(sqlite.Open(dsn), cfg)
26-
}
27-
if dsn, has := os.LookupEnv("YDB_CONNECTION_STRING"); has {
28-
return gorm.Open(ydb.Open(dsn, ydb.WithTablePathPrefix("gorm")), cfg)
29-
}
30-
return nil, errors.New("cannot initialize DB")
31-
}
32-
3316
func main() {
17+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
18+
defer cancel()
19+
3420
// connect
35-
db, err := initDB(&gorm.Config{
36-
Logger: logger.Default.LogMode(logger.Info),
37-
})
21+
dsn, exists := os.LookupEnv("YDB_CONNECTION_STRING")
22+
if !exists {
23+
panic("YDB_CONNECTION_STRING environment variable not defined")
24+
}
25+
26+
db, err := gorm.Open(
27+
ydb.Open(
28+
dsn,
29+
ydb.WithTablePathPrefix("gorm"),
30+
ydb.With(
31+
environ.WithEnvironCredentials(ctx),
32+
),
33+
),
34+
&gorm.Config{
35+
Logger: logger.Default.LogMode(logger.Error),
36+
},
37+
)
3838
if err != nil {
3939
panic(err)
4040
}

examples/basic/native/main.go

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,42 @@ package main
22

33
import (
44
"context"
5-
"flag"
65
"fmt"
76
"os"
87
"path"
8+
"time"
99

1010
environ "github.com/ydb-platform/ydb-go-sdk-auth-environ"
1111

12-
ydb "github.com/ydb-platform/ydb-go-sdk/v3"
12+
"github.com/ydb-platform/ydb-go-sdk/v3"
1313
"github.com/ydb-platform/ydb-go-sdk/v3/sugar"
1414
)
1515

16-
var (
17-
dsn string
18-
prefix string
19-
)
16+
func main() {
17+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
18+
defer cancel()
2019

21-
func init() {
22-
required := []string{"ydb"}
23-
flagSet := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
24-
flagSet.Usage = func() {
25-
out := flagSet.Output()
26-
_, _ = fmt.Fprintf(out, "Usage:\n%s [options]\n", os.Args[0])
27-
_, _ = fmt.Fprintf(out, "\nOptions:\n")
28-
flagSet.PrintDefaults()
29-
}
30-
flagSet.StringVar(&dsn,
31-
"ydb", "",
32-
"YDB connection string",
33-
)
34-
flagSet.StringVar(&prefix,
35-
"prefix", "",
36-
"tables prefix",
37-
)
38-
if err := flagSet.Parse(os.Args[1:]); err != nil {
39-
flagSet.Usage()
40-
os.Exit(1)
20+
dsn, exists := os.LookupEnv("YDB_CONNECTION_STRING")
21+
if !exists {
22+
panic("YDB_CONNECTION_STRING environment variable not defined")
4123
}
42-
flagSet.Visit(func(f *flag.Flag) {
43-
for i, arg := range required {
44-
if arg == f.Name {
45-
required = append(required[:i], required[i+1:]...)
46-
}
47-
}
48-
})
49-
if len(required) > 0 {
50-
fmt.Printf("\nSome required options not defined: %v\n\n", required)
51-
flagSet.Usage()
52-
os.Exit(1)
53-
}
54-
}
5524

56-
func main() {
57-
ctx, cancel := context.WithCancel(context.Background())
58-
defer cancel()
59-
opts := []ydb.Option{
25+
db, err := ydb.Open(ctx,
26+
dsn,
6027
environ.WithEnvironCredentials(ctx),
61-
}
62-
db, err := ydb.Open(ctx, dsn, opts...)
28+
)
6329
if err != nil {
6430
panic(fmt.Errorf("connect error: %w", err))
6531
}
6632
defer func() { _ = db.Close(ctx) }()
6733

68-
prefix = path.Join(db.Name(), prefix)
34+
prefix := path.Join(db.Name(), "native")
6935

7036
err = sugar.RemoveRecursive(ctx, db, prefix)
7137
if err != nil {
7238
panic(err)
7339
}
7440

75-
err = sugar.MakeRecursive(ctx, db, prefix)
76-
if err != nil {
77-
panic(err)
78-
}
79-
8041
err = describeTableOptions(ctx, db.Table())
8142
if err != nil {
8243
panic(fmt.Errorf("describe table options error: %w", err))
@@ -87,9 +48,7 @@ func main() {
8748
panic(fmt.Errorf("create tables error: %w", err))
8849
}
8950

90-
err = describeTable(ctx, db.Table(), path.Join(
91-
prefix, "series",
92-
))
51+
err = describeTable(ctx, db.Table(), path.Join(prefix, "series"))
9352
if err != nil {
9453
panic(fmt.Errorf("describe table error: %w", err))
9554
}
@@ -109,9 +68,7 @@ func main() {
10968
panic(fmt.Errorf("scan query select error: %w", err))
11069
}
11170

112-
err = readTable(ctx, db.Table(), path.Join(
113-
prefix, "series",
114-
))
71+
err = readTable(ctx, db.Table(), path.Join(prefix, "series"))
11572
if err != nil {
11673
panic(fmt.Errorf("read table error: %w", err))
11774
}

examples/go.mod

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ require (
1414
github.com/ydb-platform/ydb-go-sdk/v3 v3.44.0
1515
github.com/ydb-platform/ydb-go-yc v0.10.1
1616
google.golang.org/genproto v0.0.0-20220822174746-9e6da59bd2fc
17-
gorm.io/driver/postgres v1.4.6
18-
gorm.io/driver/sqlite v1.4.4
1917
gorm.io/gorm v1.24.5
2018
)
2119

@@ -25,15 +23,11 @@ require (
2523
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
2624
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
2725
github.com/golang/protobuf v1.5.2 // indirect
28-
github.com/jackc/pgpassfile v1.0.0 // indirect
29-
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
30-
github.com/jackc/pgx/v5 v5.2.0 // indirect
3126
github.com/jinzhu/inflection v1.0.0 // indirect
3227
github.com/jinzhu/now v1.1.5 // indirect
3328
github.com/jonboulle/clockwork v0.4.0 // indirect
3429
github.com/mattn/go-colorable v0.1.13 // indirect
3530
github.com/mattn/go-isatty v0.0.16 // indirect
36-
github.com/mattn/go-sqlite3 v1.14.15 // indirect
3731
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
3832
github.com/prometheus/client_model v0.2.0 // indirect
3933
github.com/prometheus/common v0.37.0 // indirect
@@ -42,7 +36,6 @@ require (
4236
github.com/ydb-platform/ydb-go-genproto v0.0.0-20221215182650-986f9d10542f // indirect
4337
github.com/ydb-platform/ydb-go-sdk-metrics v0.16.3 // indirect
4438
github.com/ydb-platform/ydb-go-yc-metadata v0.5.4 // indirect
45-
golang.org/x/crypto v0.4.0 // indirect
4639
golang.org/x/net v0.7.0 // indirect
4740
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7 // indirect
4841
golang.org/x/sys v0.5.0 // indirect

0 commit comments

Comments
 (0)