Implement schemagen ignore-names flag

Co-authored by Alexander Setzer <Alexander.Setzer@alfatraining.de>
This commit is contained in:
Ingmar Wittkau
2024-02-23 17:08:44 +01:00
committed by Roy Dahan
parent 12811b5554
commit e8f30f8dda
2 changed files with 154 additions and 7 deletions

View File

@@ -11,6 +11,7 @@ import (
"log" "log"
"os" "os"
"path" "path"
"regexp"
"strings" "strings"
"github.com/gocql/gocql" "github.com/gocql/gocql"
@@ -26,6 +27,7 @@ var (
flagOutput = cmd.String("output", "models", "the name of the folder to output to") flagOutput = cmd.String("output", "models", "the name of the folder to output to")
flagUser = cmd.String("user", "", "user for password authentication") flagUser = cmd.String("user", "", "user for password authentication")
flagPassword = cmd.String("password", "", "password for password authentication") flagPassword = cmd.String("password", "", "password for password authentication")
flagIgnoreNames = cmd.String("ignore-names", "", "a comma-separated list of table, view or index names to ignore")
) )
var ( var (
@@ -82,6 +84,24 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
log.Fatalln("unable to parse models template:", err) log.Fatalln("unable to parse models template:", err)
} }
ignoredNames := make(map[string]struct{})
for _, ignoredName := range strings.Split(*flagIgnoreNames, ",") {
ignoredNames[ignoredName] = struct{}{}
}
for name := range ignoredNames {
delete(md.Tables, name)
}
orphanedTypes := make(map[string]struct{})
for userTypeName := range md.UserTypes {
if !usedInTables(userTypeName, md.Tables) {
orphanedTypes[userTypeName] = struct{}{}
}
}
for typeName := range orphanedTypes {
delete(md.UserTypes, typeName)
}
imports := make([]string, 0) imports := make([]string, 0)
for _, t := range md.Tables { for _, t := range md.Tables {
for _, c := range t.Columns { for _, c := range t.Columns {
@@ -135,3 +155,28 @@ func existsInSlice(s []string, v string) bool {
return false return false
} }
// userTypes finds Cassandra schema types enclosed in angle brackets.
// Calling FindAllStringSubmatch on it will return a slice of string slices containing two elements.
// The second element contains the name of the type.
//
// [["<my_type,", "my_type"] ["my_other_type>", "my_other_type"]]
var userTypes = regexp.MustCompile(`(?:<|\s)(\w+)(?:>|,)`) // match all types contained in set<X>, list<X>, tuple<A, B> etc.
// usedInTables reports whether the typeName is used in any of columns of the provided tables.
func usedInTables(typeName string, tables map[string]*gocql.TableMetadata) bool {
for _, table := range tables {
for _, column := range table.Columns {
if typeName == column.Validator {
return true
}
matches := userTypes.FindAllStringSubmatch(column.Validator, -1)
for _, s := range matches {
if s[1] == typeName {
return true
}
}
}
}
return false
}

View File

@@ -5,8 +5,10 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"strings"
"testing" "testing"
"github.com/gocql/gocql"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/scylladb/gocqlx/v2/gocqlxtest" "github.com/scylladb/gocqlx/v2/gocqlxtest"
) )
@@ -16,6 +18,13 @@ var flagUpdate = flag.Bool("update", false, "update golden file")
func TestSchemagen(t *testing.T) { func TestSchemagen(t *testing.T) {
flag.Parse() flag.Parse()
createTestSchema(t) createTestSchema(t)
// add ignored types and table
*flagIgnoreNames = strings.Join([]string{
"composers",
"composers_by_name",
"label",
}, ",")
b := runSchemagen(t, "foobar") b := runSchemagen(t, "foobar")
const goldenFile = "testdata/models.go.txt" const goldenFile = "testdata/models.go.txt"
@@ -34,6 +43,76 @@ func TestSchemagen(t *testing.T) {
} }
} }
func Test_usedInTables(t *testing.T) {
tests := map[string]struct {
columnValidator string
typeName string
}{
"matches given a frozen collection": {
columnValidator: "frozen<album>",
typeName: "album",
},
"matches given a set": {
columnValidator: "set<artist>",
typeName: "artist",
},
"matches given a list": {
columnValidator: "list<song>",
typeName: "song",
},
"matches given a tuple: first of two elements": {
columnValidator: "tuple<first, second>",
typeName: "first",
},
"matches given a tuple: second of two elements": {
columnValidator: "tuple<first, second>",
typeName: "second",
},
"matches given a tuple: first of three elements": {
columnValidator: "tuple<first, second, third>",
typeName: "first",
},
"matches given a tuple: second of three elements": {
columnValidator: "tuple<first, second, third>",
typeName: "second",
},
"matches given a tuple: third of three elements": {
columnValidator: "tuple<first, second, third>",
typeName: "third",
},
"matches given a frozen set": {
columnValidator: "set<frozen<album>>",
typeName: "album",
},
"matches snake_case names given a nested map": {
columnValidator: "map<album, tuple<first, map<map_key, map-value>, third>>",
typeName: "map_key",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
tables := map[string]*gocql.TableMetadata{
"table": {Columns: map[string]*gocql.ColumnMetadata{
"column": {Validator: tt.columnValidator},
}},
}
if !usedInTables(tt.typeName, tables) {
t.Fatal()
}
})
}
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<text, album>"},
}},
}
usedInTables("", tables)
})
}
func createTestSchema(t *testing.T) { func createTestSchema(t *testing.T) {
t.Helper() t.Helper()
@@ -73,6 +152,29 @@ func createTestSchema(t *testing.T) {
if err != nil { if err != nil {
t.Fatal("create table:", err) t.Fatal("create table:", err)
} }
err = session.ExecStmt(`CREATE TABLE IF NOT EXISTS schemagen.composers (
id uuid PRIMARY KEY,
name text)`)
if err != nil {
t.Fatal("create table:", err)
}
err = session.ExecStmt(`CREATE MATERIALIZED VIEW IF NOT EXISTS schemagen.composers_by_name AS
SELECT id, name
FROM composers
WHERE id IS NOT NULL AND name IS NOT NULL
PRIMARY KEY (id, name)`)
if err != nil {
t.Fatal("create view:", err)
}
err = session.ExecStmt(`CREATE TYPE IF NOT EXISTS schemagen.label (
name text,
artists set<text>)`)
if err != nil {
t.Fatal("create type:", err)
}
} }
func runSchemagen(t *testing.T, pkgname string) []byte { func runSchemagen(t *testing.T, pkgname string) []byte {