Skip to content

Commit 281255b

Browse files
committed
MVP ok
1 parent af86bda commit 281255b

45 files changed

Lines changed: 4689 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/release.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
jobs:
9+
release:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
14+
- name: Set up Go
15+
uses: actions/setup-go@v5
16+
with:
17+
go-version: '1.24'
18+
19+
- name: Build
20+
run: |
21+
go build -o sqlc-engine-ydb ./cmd/sqlc-engine-ydb
22+
23+
- name: Release
24+
uses: softprops/action-gh-release@v1
25+
with:
26+
files: sqlc-engine-ydb
27+
env:
28+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test.yml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches: [ main, master ]
6+
pull_request:
7+
branches: [ main, master ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
with:
15+
# Если sqlc-engine-ydb в отдельном репозитории, checkout только его
16+
# Если в монорепозитории, checkout весь репозиторий
17+
path: .
18+
19+
- name: Set up Go
20+
uses: actions/setup-go@v5
21+
with:
22+
go-version: '1.24'
23+
24+
- name: Install dependencies
25+
run: go mod download
26+
27+
- name: Run unit tests
28+
run: go test ./internal/...
29+
30+
- name: Build plugins
31+
run: |
32+
go build -o bin/sqlc-engine-ydb ./cmd/sqlc-engine-ydb
33+
make build-plugins
34+
35+
- name: Check if engine-plugin exists
36+
id: check_engine_plugin
37+
run: |
38+
if [ -d "../engine-plugin" ]; then
39+
echo "exists=true" >> $GITHUB_OUTPUT
40+
else
41+
echo "exists=false" >> $GITHUB_OUTPUT
42+
fi
43+
44+
- name: Build sqlc from engine-plugin
45+
if: steps.check_engine_plugin.outputs.exists == 'true'
46+
working-directory: ../engine-plugin
47+
run: |
48+
go mod download
49+
mkdir -p bin
50+
go build -o bin/sqlc ./cmd/sqlc
51+
52+
- name: Run integration tests
53+
if: steps.check_engine_plugin.outputs.exists == 'true'
54+
working-directory: tests
55+
env:
56+
PATH: ${{ github.workspace }}/sqlc-engine-ydb/bin:${{ github.workspace }}/../engine-plugin/bin:${{ env.PATH }}
57+
run: go test -v -run TestIntegration
58+
59+
- name: Check git diff after code generation
60+
if: steps.check_engine_plugin.outputs.exists == 'true'
61+
run: |
62+
# Настраиваем git для проверки diff
63+
git config --global user.email "test@example.com"
64+
git config --global user.name "Test User"
65+
66+
# Добавляем все файлы в индекс для проверки изменений
67+
git add -A
68+
69+
# Проверяем, что нет изменений после генерации
70+
if ! git diff --exit-code --quiet HEAD; then
71+
echo "❌ Generated code differs from expected. Git diff:"
72+
git diff HEAD
73+
exit 1
74+
else
75+
echo "✅ Generated code matches expected files"
76+
fi
77+
78+
- name: Skip integration tests
79+
if: steps.check_engine_plugin.outputs.exists == 'false'
80+
run: |
81+
echo "⚠️ engine-plugin not found, skipping integration tests"
82+
echo "Integration tests require engine-plugin to be available in ../engine-plugin"

Makefile

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# sqlc-ydb: engine plugin + ydb-go-sdk codegen plugin for sqlc (v2 config)
2+
#
3+
# Prerequisites:
4+
# - Go 1.24+
5+
# - protoc + protoc-gen-go (for proto). go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
6+
# - go mod download (so SQLC_MOD_DIR is available for proto)
7+
# - sqlc with plugin support (for examples), on PATH
8+
#
9+
# Overrides:
10+
# BINDIR where to install plugin binaries (default: bin)
11+
# SQLC sqlc binary for examples (default: bin/sqlc if build-sqlc was run, else sqlc)
12+
#
13+
# Default: show help. Run 'make build' then 'make build-sqlc' then 'make examples' to generate code.
14+
# build-sqlc builds sqlc from ../engine-plugin (required for plugin support).
15+
16+
REPO_ROOT := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
17+
BINDIR ?= bin
18+
# Prefer bin/sqlc from build-sqlc when present
19+
SQLC ?= $(firstword $(wildcard $(REPO_ROOT)$(BINDIR)/sqlc) sqlc)
20+
ENGINE_PLUGIN_DIR := $(REPO_ROOT)../engine-plugin
21+
22+
# Use codegen.proto from the sqlc module (via replace => ../engine-plugin or go get).
23+
SQLC_MOD_DIR := $(shell go list -m -f '{{.Dir}}' github.com/sqlc-dev/sqlc)
24+
SQLC_PROTOS := $(SQLC_MOD_DIR)/protos
25+
PROTO_IN := $(SQLC_PROTOS)/plugin/codegen.proto
26+
PROTO_OUT = $(REPO_ROOT)internal/codegen/pb
27+
PBOUT = $(PROTO_OUT)/codegen.pb.go
28+
ENGINE_BIN = $(BINDIR)/sqlc-engine-ydb
29+
CODEGEN_BIN = $(BINDIR)/sqlc-gen-ydb-go-sdk
30+
31+
.PHONY: all proto build build-engine build-codegen build-sqlc examples clean help
32+
33+
all: help
34+
35+
help:
36+
@echo "Targets:"
37+
@echo " proto - generate codegen.pb.go from sqlc module's protos/plugin/codegen.proto (needs protoc, protoc-gen-go, go mod download)"
38+
@echo " build-engine - build sqlc-engine-ydb into $(BINDIR)/"
39+
@echo " build-codegen - build sqlc-gen-ydb-go-sdk into $(BINDIR)/ (depends on proto)"
40+
@echo " build - proto + build both plugins"
41+
@echo " build-sqlc - build sqlc from ../engine-plugin into $(BINDIR)/sqlc (needed for examples)"
42+
@echo " examples - run 'sqlc generate' in examples/authors (requires build + build-sqlc or sqlc on PATH)"
43+
@echo " clean - remove $(BINDIR)/ and generated example output"
44+
@echo ""
45+
@echo "Overrides: BINDIR=$(BINDIR) SQLC=$(SQLC)"
46+
47+
# Generate plugin proto Go from the sqlc module's codegen.proto (via go get / replace).
48+
# Output goes to internal/codegen/pb with package pb.
49+
proto: $(PBOUT)
50+
@echo "ok: $(PBOUT)"
51+
52+
$(PBOUT): $(PROTO_IN)
53+
@command -v protoc >/dev/null || (echo "error: protoc not found" >&2; exit 1)
54+
@command -v protoc-gen-go >/dev/null || (echo "error: protoc-gen-go not found (run: go install google.golang.org/protobuf/cmd/protoc-gen-go@latest)" >&2; exit 1)
55+
@mkdir -p $(PROTO_OUT)
56+
cd $(REPO_ROOT) && protoc -I$(SQLC_PROTOS) \
57+
--go_out=. --go_opt=module=github.com/sqlc-dev/sqlc-engine-ydb \
58+
--go_opt=Mplugin/codegen.proto=github.com/sqlc-dev/sqlc-engine-ydb/internal/codegen/pb \
59+
plugin/codegen.proto
60+
@if [ ! -f "$(PBOUT)" ]; then \
61+
echo "error: expected $(PBOUT) to be created; protoc may use different paths" >&2; \
62+
exit 1; \
63+
fi
64+
@echo "ok: $(PBOUT)"
65+
66+
# Build the YDB engine plugin (parses schema + queries).
67+
build-engine: $(BINDIR)
68+
go build -o $(ENGINE_BIN) ./cmd/sqlc-engine-ydb/
69+
@echo "ok: $(ENGINE_BIN)"
70+
71+
# Build the ydb-go-sdk codegen plugin. Requires proto.
72+
build-codegen: proto $(BINDIR)
73+
go build -o $(CODEGEN_BIN) ./cmd/sqlc-gen-ydb-go-sdk/
74+
@echo "ok: $(CODEGEN_BIN)"
75+
76+
# Build both plugins.
77+
build: build-engine build-codegen
78+
@echo "Plugins ready in $(BINDIR)/"
79+
80+
# Build sqlc from engine-plugin into BINDIR (so examples use plugin-aware sqlc).
81+
build-sqlc: $(BINDIR)
82+
@if [ ! -d "$(ENGINE_PLUGIN_DIR)" ]; then echo "error: $(ENGINE_PLUGIN_DIR) not found (clone engine-plugin next to sqlc-ydb)" >&2; exit 1; fi
83+
cd $(ENGINE_PLUGIN_DIR) && go build -o $(REPO_ROOT)$(BINDIR)/sqlc ./cmd/sqlc/
84+
@echo "ok: $(BINDIR)/sqlc"
85+
86+
$(BINDIR):
87+
mkdir -p $(BINDIR)
88+
89+
# Run sqlc generate in examples/authors. Uses BINDIR/sqlc if build-sqlc was run, else SQLC from PATH.
90+
examples: build
91+
@which $(SQLC) >/dev/null 2>/dev/null || (echo "error: sqlc not found. Run 'make build-sqlc' or set SQLC to a plugin-aware sqlc binary" >&2; exit 1)
92+
cd $(REPO_ROOT)examples/authors && PATH="$(REPO_ROOT)$(BINDIR):$$PATH" $(SQLC) generate
93+
@echo "ok: examples/authors/db/ generated"
94+
95+
clean:
96+
rm -rf $(REPO_ROOT)$(BINDIR)
97+
rm -rf $(REPO_ROOT)examples/authors/db
98+
@echo "ok: cleaned"

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
11
# sqlc-engine-ydb
2-
sqlc-engine-ydb is an external engine of SQLC with support YDB
2+
sqlc-engine-ydb is an external engine of SQLC with support YDB.
3+
4+
## Generating Go code (examples/authors)
5+
6+
The `examples/authors` project uses the v2 config with the **sqlc-engine-ydb** engine plugin and **sqlc-gen-ydb-go-sdk** codegen plugin. To generate Go code:
7+
8+
1. **Build the plugins** (from the sqlc-ydb repo root):
9+
```bash
10+
make build
11+
```
12+
2. **Build sqlc from engine-plugin** (requires [engine-plugin](https://github.com/sqlc-dev/sqlc) cloned next to sqlc-ydb, e.g. in `../engine-plugin`):
13+
```bash
14+
make build-sqlc
15+
```
16+
3. **Run code generation**:
17+
```bash
18+
make examples
19+
```
20+
21+
Generated files appear in `examples/authors/db/` (`models.go`, `db.go`, `queries.sql.go`). The Makefile uses `bin/sqlc` from `make build-sqlc` by default when running `make examples`; you can override with `make examples SQLC=/path/to/sqlc`.

cmd/sqlc-engine-ydb/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"github.com/sqlc-dev/sqlc/pkg/engine"
5+
6+
"github.com/sqlc-dev/sqlc-engine-ydb/internal/handler"
7+
)
8+
9+
func main() {
10+
engine.Run(engine.Handler{
11+
PluginName: "ydb",
12+
PluginVersion: "1.0.0",
13+
Parse: handler.Parse,
14+
})
15+
}

cmd/sqlc-gen-ydb-go-sdk/main.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// sqlc-gen-ydb-go-sdk is a process-based codegen plugin for sqlc (v2 config).
2+
// It reads a binary GenerateRequest from stdin and writes a binary GenerateResponse to stdout.
3+
// See docs/guides/plugins.md (process plugins).
4+
package main
5+
6+
import (
7+
"bufio"
8+
"context"
9+
"fmt"
10+
"io"
11+
"os"
12+
13+
"github.com/sqlc-dev/sqlc-engine-ydb/internal/codegen/pb"
14+
"github.com/sqlc-dev/sqlc-engine-ydb/internal/codegen/ydb"
15+
"google.golang.org/protobuf/proto"
16+
)
17+
18+
func main() {
19+
if err := run(context.Background()); err != nil {
20+
fmt.Fprintf(os.Stderr, "sqlc-gen-ydb-go-sdk: %v\n", err)
21+
os.Exit(2)
22+
}
23+
}
24+
25+
func run(ctx context.Context) error {
26+
reqBlob, err := io.ReadAll(os.Stdin)
27+
if err != nil {
28+
return err
29+
}
30+
var req pb.GenerateRequest
31+
if err := proto.Unmarshal(reqBlob, &req); err != nil {
32+
return fmt.Errorf("unmarshal request: %w", err)
33+
}
34+
resp, err := ydb.Generate(ctx, &req)
35+
if err != nil {
36+
return err
37+
}
38+
respBlob, err := proto.Marshal(resp)
39+
if err != nil {
40+
return fmt.Errorf("marshal response: %w", err)
41+
}
42+
w := bufio.NewWriter(os.Stdout)
43+
if _, err := w.Write(respBlob); err != nil {
44+
return err
45+
}
46+
return w.Flush()
47+
}

examples/authors/db/db.go

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

examples/authors/db/models.go

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

0 commit comments

Comments
 (0)