From e04b7ca474833cde5f2c225b0ccb49bf80c79a0d Mon Sep 17 00:00:00 2001 From: Dmitry Kropachev Date: Fri, 14 Jun 2024 13:39:03 -0400 Subject: [PATCH] Switch to scylladb/gocql --- Makefile | 4 ++-- cmd/schemagen/keyspace.tmpl | 4 ++-- cmd/schemagen/schemagen.go | 16 ++++++++-------- cmd/schemagen/schemagen_test.go | 4 ++-- go.mod | 2 ++ go.sum | 23 ++++++++++++++++------- 6 files changed, 32 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index ad6f721..02afd4a 100644 --- a/Makefile +++ b/Makefile @@ -51,8 +51,8 @@ run-examples: 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 + @docker run --name gocqlx-scylla -p 9042:9042 --cpuset-cpus=$(SCYLLA_CPU) --memory 1G --rm -d scylladb/scylla:$(SCYLLA_VERSION) + @until docker exec gocqlx-scylla cqlsh -e "DESCRIBE SCHEMA"; do sleep 2; done .PHONY: stop-scylla stop-scylla: diff --git a/cmd/schemagen/keyspace.tmpl b/cmd/schemagen/keyspace.tmpl index cddb23d..e5c2255 100644 --- a/cmd/schemagen/keyspace.tmpl +++ b/cmd/schemagen/keyspace.tmpl @@ -54,8 +54,8 @@ type {{$type_name}}UserType struct { {{- $model_name := .Name | camelize}} type {{$model_name}}Struct struct { {{- range .Columns}} - {{- if not (eq .Validator "empty") }} - {{.Name | camelize}} {{.Validator | mapScyllaToGoType}} + {{- if not (eq .Type "empty") }} + {{.Name | camelize}} {{.Type | mapScyllaToGoType}} {{- end}} {{- end}} } diff --git a/cmd/schemagen/schemagen.go b/cmd/schemagen/schemagen.go index 85875c7..12dadb9 100644 --- a/cmd/schemagen/schemagen.go +++ b/cmd/schemagen/schemagen.go @@ -101,25 +101,25 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) { } orphanedTypes := make(map[string]struct{}) - for userTypeName := range md.UserTypes { + for userTypeName := range md.Views { if !usedInTables(userTypeName, md.Tables) { orphanedTypes[userTypeName] = struct{}{} } } for typeName := range orphanedTypes { - delete(md.UserTypes, typeName) + delete(md.Views, typeName) } imports := make([]string, 0) for _, t := range md.Tables { for _, c := range t.Columns { - if (c.Validator == "timestamp" || c.Validator == "date" || c.Validator == "duration" || c.Validator == "time") && !existsInSlice(imports, "time") { + if (c.Type == "timestamp" || c.Type == "date" || c.Type == "duration" || c.Type == "time") && !existsInSlice(imports, "time") { imports = append(imports, "time") } - if c.Validator == "decimal" && !existsInSlice(imports, "gopkg.in/inf.v0") { + if c.Type == "decimal" && !existsInSlice(imports, "gopkg.in/inf.v0") { imports = append(imports, "gopkg.in/inf.v0") } - if c.Validator == "duration" && !existsInSlice(imports, "github.com/gocql/gocql") { + if c.Type == "duration" && !existsInSlice(imports, "github.com/gocql/gocql") { imports = append(imports, "github.com/gocql/gocql") } } @@ -129,7 +129,7 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) { data := map[string]interface{}{ "PackageName": *flagPkgname, "Tables": md.Tables, - "UserTypes": md.UserTypes, + "UserTypes": md.Views, "Imports": imports, } @@ -175,10 +175,10 @@ var userTypes = regexp.MustCompile(`(?:<|\s)(\w+)(?:>|,)`) // match all types co func usedInTables(typeName string, tables map[string]*gocql.TableMetadata) bool { for _, table := range tables { for _, column := range table.Columns { - if typeName == column.Validator { + if typeName == column.Type { return true } - matches := userTypes.FindAllStringSubmatch(column.Validator, -1) + matches := userTypes.FindAllStringSubmatch(column.Type, -1) for _, s := range matches { if s[1] == typeName { return true diff --git a/cmd/schemagen/schemagen_test.go b/cmd/schemagen/schemagen_test.go index 44c5925..368ecc1 100644 --- a/cmd/schemagen/schemagen_test.go +++ b/cmd/schemagen/schemagen_test.go @@ -96,7 +96,7 @@ func Test_usedInTables(t *testing.T) { t.Run(name, func(t *testing.T) { tables := map[string]*gocql.TableMetadata{ "table": {Columns: map[string]*gocql.ColumnMetadata{ - "column": {Validator: tt.columnValidator}, + "column": {Type: tt.columnValidator}, }}, } if !usedInTables(tt.typeName, tables) { @@ -108,7 +108,7 @@ func Test_usedInTables(t *testing.T) { t.Run("doesn't panic with empty type name", func(t *testing.T) { tables := map[string]*gocql.TableMetadata{ "table": {Columns: map[string]*gocql.ColumnMetadata{ - "column": {Validator: "map"}, + "column": {Type: "map"}, }}, } usedInTables("", tables) diff --git a/go.mod b/go.mod index f8ff801..b444b47 100644 --- a/go.mod +++ b/go.mod @@ -16,3 +16,5 @@ require ( github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect ) + +replace github.com/gocql/gocql => github.com/scylladb/gocql v1.14.0 diff --git a/go.sum b/go.sum index 00894b5..3fe0ba2 100644 --- a/go.sum +++ b/go.sum @@ -3,16 +3,11 @@ github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCS github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY= github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gocql/gocql v0.0.0-20200131111108-92af2e088537 h1:NaMut1fdw76YYX/TPinSAbai4DShF5tPort3bHpET6g= -github.com/gocql/gocql v0.0.0-20200131111108-92af2e088537/go.mod h1:DL0ekTmBSTdlNF25Orwt/JMzqIq3EJ4MVa/J/uK64OY= -github.com/gocql/gocql v0.0.0-20211015133455-b225f9b53fa1 h1:px9qUCy/RNJNsfCam4m2IxWGxNuimkrioEF0vrrbPsg= -github.com/gocql/gocql v0.0.0-20211015133455-b225f9b53fa1/go.mod h1:3gM2c4D3AnkISwBxGnMMsS8Oy4y2lhbPRsH4xnJrHG8= -github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= -github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8= @@ -27,11 +22,25 @@ github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef h1:NKxTG6GVGbfMXc2m github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef/go.mod h1:tcaRap0jS3eifrEEllL6ZMd9dg8IlDpi2S1oARrQ+NI= github.com/scylladb/go-reflectx v1.0.1 h1:b917wZM7189pZdlND9PbIJ6NQxfDPfBvUaQ7cjj1iZQ= github.com/scylladb/go-reflectx v1.0.1/go.mod h1:rWnOfDIRWBGN0miMLIcoPt/Dhi2doCMZqwMCJ3KupFc= +github.com/scylladb/gocql v1.14.0 h1:MuQ2sEOHxqTmCWS8zoH34SD5bpm3NbXi7CQbIVxSsZY= +github.com/scylladb/gocql v1.14.0/go.mod h1:ZLEJ0EVE5JhmtxIW2stgHq/v1P4fWap0qyyXSKyV8K0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +golang.org/x/net v0.0.0-20220526153639-5463443f8c37 h1:lUkvobShwKsOesNfWWlCS5q7fnbG1MEliIzwu886fn8= +golang.org/x/net v0.0.0-20220526153639-5463443f8c37/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= +sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=