Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: scylladb/gocqlx
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.0.3
Choose a base ref
...
head repository: scylladb/gocqlx
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Loading
Showing with 3,843 additions and 530 deletions.
  1. +49 −0 .github/workflows/main.yml
  2. +3 −0 .gitignore
  3. +49 −13 .golangci.yml
  4. +0 −19 .travis.yml
  5. +31 −10 Makefile
  6. +92 −5 README.md
  7. +84 −0 batchx.go
  8. +191 −0 batchx_test.go
  9. +13 −10 benchmark_test.go
  10. +43 −0 cmd/schemagen/camelize.go
  11. +31 −0 cmd/schemagen/camelize_test.go
  12. +63 −0 cmd/schemagen/keyspace.tmpl
  13. +87 −0 cmd/schemagen/map_types.go
  14. +49 −0 cmd/schemagen/map_types_test.go
  15. +226 −0 cmd/schemagen/schemagen.go
  16. +214 −0 cmd/schemagen/schemagen_test.go
  17. +21 −0 cmd/schemagen/testdata/go.mod
  18. +45 −0 cmd/schemagen/testdata/go.sum
  19. +71 −0 cmd/schemagen/testdata/models.go
  20. +92 −0 cmd/schemagen/testdata/models_test.go
  21. +6 −0 dbutil/doc.go
  22. +41 −0 dbutil/rewrite.go
  23. +122 −0 dbutil/rewrite_test.go
  24. +3 −2 doc_test.go
  25. +334 −72 example_test.go
  26. +13 −7 go.mod
  27. +26 −14 go.sum
  28. +46 −28 gocqlxtest/gocqlxtest.go
  29. +21 −22 iterx.go
  30. +362 −40 iterx_test.go
  31. +6 −35 migrate/README.md
  32. +42 −4 migrate/callback.go
  33. +5 −5 migrate/checksum.go
  34. +5 −2 migrate/checksum_test.go
  35. +4 −6 migrate/doc.go
  36. +15 −0 migrate/example/cql/embed.go
  37. +15 −0 migrate/example/cql/m1.cql
  38. +68 −0 migrate/example/example_test.go
  39. +5 −0 migrate/export_test.go
  40. +111 −35 migrate/migrate.go
  41. +160 −75 migrate/migrate_test.go
  42. +37 −4 qb/batch.go
  43. +13 −2 qb/batch_test.go
  44. +122 −2 qb/cmp.go
  45. +60 −9 qb/cmp_test.go
  46. +27 −1 qb/delete.go
  47. +15 −4 qb/delete_test.go
  48. +28 −2 qb/insert.go
  49. +36 −4 qb/insert_test.go
  50. +45 −0 qb/limit.go
  51. +53 −21 qb/select.go
  52. +38 −2 qb/select_test.go
  53. +38 −1 qb/update.go
  54. +19 −2 qb/update_test.go
  55. +46 −21 qb/using.go
  56. +37 −5 qb/using_test.go
  57. +26 −0 qb/utils.go
  58. +61 −0 qb/utils_test.go
  59. +3 −3 qb/value.go
  60. +61 −17 queryx.go
  61. +1 −1 queryx_bench_test.go
  62. +66 −6 queryx_test.go
  63. +3 −0 queryx_wrap.go
  64. +6 −1 session.go
  65. +75 −1 table/table.go
  66. +13 −1 table/table_test.go
  67. +30 −0 transformer.go
  68. +20 −16 udt.go
49 changes: 49 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Build

on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]

jobs:
build:
name: Build
runs-on: ubuntu-latest
env:
SCYLLA_IMAGE: scylladb/scylla:6.0.0
GOBIN: ./bin
steps:
- name: Git Checkout
uses: actions/checkout@v3
with:
fetch-depth: '0'
- name: Install Go 1.17
uses: actions/setup-go@v3
with:
go-version: 1.17

- name: Cache Dependencies
uses: actions/cache@v3
id: gomod-cache
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('go.mod') }}
restore-keys: |
${{ runner.os }}-go-
- name: Make Directory for GOBIN
run: mkdir -p "${GOBIN}"

- name: Download Dependencies
run: git --version && make get-deps && make get-tools

- name: Lint
run: make check

- name: Run Scylla Container
run: make run-scylla

- name: Test
run: make test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -14,3 +14,6 @@
vendor
# GoLand IDE
.idea/

cmd/schemagen/schemagen
cmd/schemagen/testdata/go.sum
62 changes: 49 additions & 13 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
run:
deadline: 5m
tests: false
tests: true
allow-parallel-runners: true
modules-download-mode: readonly
build-tags: [ all, integration ]

linters-settings:
revive:
rules:
- name: package-comments
disabled: true
goimports:
local-prefixes: github.com/scylladb/gocqlx
gofumpt:
extra-rules: true
govet:
enable-all: true
disable:
- shadow
errcheck:
check-blank: true
gocognit:
@@ -23,22 +38,43 @@ linters-settings:
line-length: 180

linters:
enable-all: true
disable:
- funlen
- gas
- gochecknoglobals
- gochecknoinits
- gomnd
- interfacer
- maligned
- nakedret
- prealloc
- wsl
disable-all: true
enable:
- errcheck
- gocritic
- gofumpt
- goheader
- goimports
- gosimple
- govet
- ineffassign
- lll
- misspell
- predeclared
- revive
- staticcheck
- thelper
- tparallel
- typecheck
- unused
- forbidigo

issues:
new: true
new-from-rev: origin/master
exclude-use-default: false
exclude:
- composite literal uses unkeyed fields
- Error return value of `.+\.Close` is not checked
- method Json should be JSON
exclude-rules:
- path: (.*_test.go|migrate/example|gocqlxtest/)
linters:
- fieldalignment
- govet
- errcheck
- path: doc_test.go
linters:
- unused
- revive

19 changes: 0 additions & 19 deletions .travis.yml

This file was deleted.

41 changes: 31 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
all: check test

ifndef SCYLLA_VERSION
SCYLLA_VERSION := latest
ifndef SCYLLA_IMAGE
SCYLLA_IMAGE := scylladb/scylla:6.0.0
endif

ifndef SCYLLA_CPU
@@ -12,6 +12,10 @@ ifndef GOTEST_CPU
GOTEST_CPU := 1
endif

ifndef GOPATH
GOPATH := $(shell go env GOPATH)
endif

ifndef GOBIN
GOBIN := $(GOPATH)/bin
endif
@@ -24,33 +28,50 @@ fmt:
check:
@$(GOBIN)/golangci-lint run ./...

.PHONY: fix
fix:
@$(GOBIN)/golangci-lint run --fix ./...
@fieldalignment -V=full >/dev/null 2>&1 || go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@v0.11.0
@$(GOBIN)/fieldalignment -test=false -fix ./...

GOTEST := go test -cpu $(GOTEST_CPU) -count=1 -cover -race -tags all

.PHONY: test
test:
echo "==> Running tests..."
echo "==> Running tests... in ."
@$(GOTEST) .
@$(GOTEST) ./migrate
echo "==> Running tests... in ./qb"
@$(GOTEST) ./qb
echo "==> Running tests... in ./table"
@$(GOTEST) ./table
echo "==> Running tests... in ./migrate"
@$(GOTEST) ./migrate
echo "==> Running tests... in ./dbutil"
@$(GOTEST) ./dbutil
echo "==> Running tests... in ./cmd/schemagen"
@$(GOTEST) ./cmd/schemagen
echo "==> Running tests... in ./cmd/schemagen/testdata"
@cd ./cmd/schemagen/testdata ; go mod tidy ; $(GOTEST) .; cd ../../..

.PHONY: bench
bench:
@go test -cpu $(GOTEST_CPU) -tags all -run=XXX -bench=. -benchmem ./...

.PHONY: run-examples
run-examples:
@go test -tags all -v -run=Example
@go test -tags all -v -run=Example ./...

.PHONY: run-scylla
run-scylla:
@echo "==> Running test instance of Scylla $(SCYLLA_VERSION)"
@docker pull scylladb/scylla:$(SCYLLA_VERSION)
@docker run --name scylla -p 9042:9042 --cpuset-cpus=$(SCYLLA_CPU) --memory 1G --rm -d scylladb/scylla:$(SCYLLA_VERSION)
@until docker exec scylla cqlsh -e "DESCRIBE SCHEMA"; do sleep 2; done
@echo "==> Running test instance of Scylla $(SCYLLA_IMAGE)"
@docker pull $(SCYLLA_IMAGE)
@docker run --name gocqlx-scylla -p 9042:9042 --cpuset-cpus=$(SCYLLA_CPU) --memory 1G --rm -d $(SCYLLA_IMAGE)
@until docker exec gocqlx-scylla cqlsh -e "DESCRIBE SCHEMA"; do sleep 2; done

.PHONY: stop-scylla
stop-scylla:
@docker stop scylla
@docker stop gocqlx-scylla

.PHONY: get-deps
get-deps:
@@ -63,4 +84,4 @@ endef
.PHONY: get-tools
get-tools:
@echo "==> Installing tools at $(GOBIN)..."
@$(call dl_tgz,golangci-lint,https://github.com/golangci/golangci-lint/releases/download/v1.24.0/golangci-lint-1.24.0-linux-amd64.tar.gz)
@$(call dl_tgz,golangci-lint,https://github.com/golangci/golangci-lint/releases/download/v1.59.1/golangci-lint-1.59.1-linux-amd64.tar.gz)
97 changes: 92 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 🚀 GocqlX [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/scylladb/gocqlx) [![Go Report Card](https://goreportcard.com/badge/github.com/scylladb/gocqlx)](https://goreportcard.com/report/github.com/scylladb/gocqlx) [![Build Status](https://travis-ci.org/scylladb/gocqlx.svg?branch=master)](https://travis-ci.org/scylladb/gocqlx)
# 🚀 GocqlX [![GoDoc](https://pkg.go.dev/badge/github.com/scylladb/gocqlx/v3.svg)](https://pkg.go.dev/github.com/scylladb/gocqlx/v3) [![Go Report Card](https://goreportcard.com/badge/github.com/scylladb/gocqlx)](https://goreportcard.com/report/github.com/scylladb/gocqlx) [![Build Status](https://travis-ci.org/scylladb/gocqlx.svg?branch=master)](https://travis-ci.org/scylladb/gocqlx)

GocqlX makes working with Scylla easy and error-prone without sacrificing performance.
GocqlX makes working with Scylla easy and less error-prone.
It’s inspired by [Sqlx](https://github.com/jmoiron/sqlx), a tool for working with SQL databases, but it goes beyond what Sqlx provides.

## Features
@@ -20,7 +20,9 @@ Subpackages provide additional functionality:
## Installation

```bash
go get -u github.com/scylladb/gocqlx/v2
git clone git@github.com:scylladb/gocqlx.git
cd gocqlx/cmd/schemagen/
go install .
```

## Getting started
@@ -52,12 +54,14 @@ var personMetadata = table.Metadata{
var personTable = table.New(personMetadata)

// Person represents a row in person table.
// Field names are converted to camel case by default, no need to add special tags.
// If you want to disable a field add `db:"-"` tag, it will not be persisted.
// Field names are converted to snake case by default, no need to add special tags.
// A field will not be persisted by adding the `db:"-"` tag or making it unexported.
type Person struct {
FirstName string
LastName string
Email []string
HairColor string `db:"-"` // exported and skipped
eyeColor string // unexported also skipped
}
```

@@ -68,6 +72,8 @@ p := Person{
"Michał",
"Matczuk",
[]string{"michal@scylladb.com"},
"red", // not persisted
"hazel" // not persisted
}
q := session.Query(personTable.Insert()).BindStruct(p)
if err := q.ExecRelease(); err != nil {
@@ -103,6 +109,83 @@ t.Log(people)
// stdout: [{Michał Matczuk [michal@scylladb.com]}]
```

## Generating table metadata with schemagen

Installation

```bash
go get -u "github.com/scylladb/gocqlx/v3/cmd/schemagen"
```

Usage:
```bash
schemagen [flags]

Flags:
-cluster string
a comma-separated list of host:port tuples (default "127.0.0.1")
-keyspace string
keyspace to inspect (required)
-output string
the name of the folder to output to (default "models")
-pkgname string
the name you wish to assign to your generated package (default "models")
```

Example:

Running the following command for `examples` keyspace:
```bash
schemagen -cluster="127.0.0.1:9042" -keyspace="examples" -output="models" -pkgname="models"
```

Generates `models/models.go` as follows:
```go
// Code generated by "gocqlx/cmd/schemagen"; DO NOT EDIT.

package models

import "github.com/scylladb/gocqlx/v3/table"

// Table models.
var (
Playlists = table.New(table.Metadata{
Name: "playlists",
Columns: []string{
"album",
"artist",
"id",
"song_id",
"title",
},
PartKey: []string{
"id",
},
SortKey: []string{
"title",
"album",
"artist",
},
})

Songs = table.New(table.Metadata{
Name: "songs",
Columns: []string{
"album",
"artist",
"data",
"id",
"tags",
"title",
},
PartKey: []string{
"id",
},
SortKey: []string{},
})
)
```

## Examples

You can find lots of examples in [example_test.go](https://github.com/scylladb/gocqlx/blob/master/example_test.go).
@@ -114,6 +197,10 @@ make run-scylla
make run-examples
```

## Training

The course [Using Scylla Drivers](https://university.scylladb.com/courses/using-scylla-drivers) in Scylla University explains how to use drivers in different languages to interact with a Scylla cluster. The lesson, [Golang and Scylla Part 3 - GoCQLX](https://university.scylladb.com/courses/using-scylla-drivers/lessons/golang-and-scylla-part-3-gocqlx/), goes over a sample application that, using GoCQLX, interacts with a three-node Scylla cluster. It connects to a Scylla cluster, displays the contents of a table, inserts and deletes data, and shows the contents of the table after each action. [Scylla University](https://university.scylladb.com/) includes other training material and online courses which will help you become a Scylla NoSQL database expert.

## Performance

GocqlX performance is comparable to the raw `gocql` driver.
Loading