2021-11-13 13:55:44 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
_ "embed"
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
2021-12-12 09:34:17 +01:00
|
|
|
"go/format"
|
2021-11-13 13:55:44 +02:00
|
|
|
"html/template"
|
2025-04-20 17:15:00 +03:00
|
|
|
"io/fs"
|
2021-11-13 13:55:44 +02:00
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"path"
|
2024-02-23 17:08:44 +01:00
|
|
|
"regexp"
|
2024-06-16 08:40:32 -04:00
|
|
|
"sort"
|
2021-11-13 13:55:44 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/gocql/gocql"
|
2024-06-14 13:07:21 -04:00
|
|
|
|
2024-07-10 07:49:07 +02:00
|
|
|
"github.com/scylladb/gocqlx/v3"
|
|
|
|
|
_ "github.com/scylladb/gocqlx/v3/table"
|
2021-11-13 13:55:44 +02:00
|
|
|
)
|
|
|
|
|
|
2024-10-01 06:49:16 -04:00
|
|
|
var defaultClusterConfig = gocql.NewCluster()
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
defaultQueryTimeout = defaultClusterConfig.Timeout
|
|
|
|
|
defaultConnectionTimeout = defaultClusterConfig.ConnectTimeout
|
|
|
|
|
)
|
|
|
|
|
|
2021-11-13 13:55:44 +02:00
|
|
|
var (
|
2024-10-01 06:54:15 -04:00
|
|
|
cmd = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
|
|
|
|
flagCluster = cmd.String("cluster", "127.0.0.1", "a comma-separated list of host:port tuples")
|
|
|
|
|
flagKeyspace = cmd.String("keyspace", "", "keyspace to inspect")
|
|
|
|
|
flagPkgname = cmd.String("pkgname", "models", "the name you wish to assign to your generated package")
|
|
|
|
|
flagOutput = cmd.String("output", "models", "the name of the folder to output to")
|
2025-04-20 17:15:00 +03:00
|
|
|
flagOutputDirPerm = cmd.Uint64("output-dir-perm", 0o755, "output directory permissions")
|
|
|
|
|
flagOutputFilePerm = cmd.Uint64("output-file-perm", 0o644, "output file permissions")
|
2024-10-01 06:54:15 -04:00
|
|
|
flagUser = cmd.String("user", "", "user 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")
|
|
|
|
|
flagIgnoreIndexes = cmd.Bool("ignore-indexes", false, "don't generate types for indexes")
|
|
|
|
|
flagQueryTimeout = cmd.Duration("query-timeout", defaultQueryTimeout, "query timeout ( in seconds )")
|
|
|
|
|
flagConnectionTimeout = cmd.Duration("connection-timeout", defaultConnectionTimeout, "connection timeout ( in seconds )")
|
|
|
|
|
flagSSLEnableHostVerification = cmd.Bool("ssl-enable-host-verification", false, "don't check server ssl certificate")
|
|
|
|
|
flagSSLCAPath = cmd.String("ssl-ca-path", "", "path to ssl CA certificates")
|
|
|
|
|
flagSSLCertPath = cmd.String("ssl-cert-path", "", "path to ssl certificate")
|
|
|
|
|
flagSSLKeyPath = cmd.String("ssl-key-path", "", "path to ssl key")
|
2021-11-13 13:55:44 +02:00
|
|
|
)
|
|
|
|
|
|
2024-06-14 13:07:21 -04:00
|
|
|
//go:embed keyspace.tmpl
|
|
|
|
|
var keyspaceTmpl string
|
2021-11-13 13:55:44 +02:00
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
err := cmd.Parse(os.Args[1:])
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln("can't parse flags")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if *flagKeyspace == "" {
|
|
|
|
|
log.Fatalln("missing required flag: keyspace")
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 11:49:42 +01:00
|
|
|
if err := schemagen(); err != nil {
|
|
|
|
|
log.Fatalf("failed to generate schema: %s", err)
|
|
|
|
|
}
|
2021-11-13 13:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-17 11:49:42 +01:00
|
|
|
func schemagen() error {
|
2025-04-20 17:15:00 +03:00
|
|
|
if err := os.MkdirAll(*flagOutput, os.FileMode(*flagOutputDirPerm)); err != nil {
|
2021-11-17 11:49:42 +01:00
|
|
|
return fmt.Errorf("create output directory: %w", err)
|
2021-11-13 13:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-17 11:49:42 +01:00
|
|
|
session, err := createSession()
|
2021-11-13 13:55:44 +02:00
|
|
|
if err != nil {
|
2021-11-17 11:49:42 +01:00
|
|
|
return fmt.Errorf("open output file: %w", err)
|
2021-11-13 13:55:44 +02:00
|
|
|
}
|
2021-11-17 11:49:42 +01:00
|
|
|
metadata, err := session.KeyspaceMetadata(*flagKeyspace)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("fetch keyspace metadata: %w", err)
|
2021-11-13 13:55:44 +02:00
|
|
|
}
|
2021-11-17 11:49:42 +01:00
|
|
|
b, err := renderTemplate(metadata)
|
2021-11-13 13:55:44 +02:00
|
|
|
if err != nil {
|
2021-11-17 11:49:42 +01:00
|
|
|
return fmt.Errorf("render template: %w", err)
|
2021-11-13 13:55:44 +02:00
|
|
|
}
|
2021-11-17 11:49:42 +01:00
|
|
|
outputPath := path.Join(*flagOutput, *flagPkgname+".go")
|
2021-11-13 13:55:44 +02:00
|
|
|
|
2025-04-20 17:15:00 +03:00
|
|
|
return os.WriteFile(outputPath, b, fs.FileMode(*flagOutputFilePerm))
|
2021-11-13 13:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-17 11:49:42 +01:00
|
|
|
func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
|
2021-11-13 13:55:44 +02:00
|
|
|
t, err := template.
|
|
|
|
|
New("keyspace.tmpl").
|
|
|
|
|
Funcs(template.FuncMap{"camelize": camelize}).
|
2021-12-10 09:19:47 +01:00
|
|
|
Funcs(template.FuncMap{"mapScyllaToGoType": mapScyllaToGoType}).
|
2021-11-13 13:55:44 +02:00
|
|
|
Parse(keyspaceTmpl)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln("unable to parse models template:", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-23 17:08:44 +01:00
|
|
|
ignoredNames := make(map[string]struct{})
|
|
|
|
|
for _, ignoredName := range strings.Split(*flagIgnoreNames, ",") {
|
|
|
|
|
ignoredNames[ignoredName] = struct{}{}
|
|
|
|
|
}
|
2024-02-23 17:11:06 +01:00
|
|
|
if *flagIgnoreIndexes {
|
|
|
|
|
for name := range md.Tables {
|
|
|
|
|
if strings.HasSuffix(name, "_index") {
|
|
|
|
|
ignoredNames[name] = struct{}{}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-23 17:08:44 +01:00
|
|
|
for name := range ignoredNames {
|
|
|
|
|
delete(md.Tables, name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
orphanedTypes := make(map[string]struct{})
|
2024-06-16 08:40:32 -04:00
|
|
|
for userTypeName := range md.Types {
|
2024-02-23 17:08:44 +01:00
|
|
|
if !usedInTables(userTypeName, md.Tables) {
|
|
|
|
|
orphanedTypes[userTypeName] = struct{}{}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for typeName := range orphanedTypes {
|
2024-06-16 08:40:32 -04:00
|
|
|
delete(md.Types, typeName)
|
2024-02-23 17:08:44 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-10 09:19:47 +01:00
|
|
|
imports := make([]string, 0)
|
2024-06-26 13:32:47 -04:00
|
|
|
if len(md.Types) != 0 {
|
2024-07-10 07:49:07 +02:00
|
|
|
imports = append(imports, "github.com/scylladb/gocqlx/v3")
|
2024-06-26 13:32:47 -04:00
|
|
|
}
|
|
|
|
|
|
2025-06-10 19:18:46 +03:00
|
|
|
updateImports := func(columns map[string]*gocql.ColumnMetadata) {
|
|
|
|
|
for _, c := range columns {
|
2024-06-26 13:32:47 -04:00
|
|
|
if (c.Type == "timestamp" || c.Type == "date" || c.Type == "time") && !existsInSlice(imports, "time") {
|
2021-12-12 09:34:17 +01:00
|
|
|
imports = append(imports, "time")
|
|
|
|
|
}
|
2024-06-16 08:40:32 -04:00
|
|
|
if c.Type == "decimal" && !existsInSlice(imports, "gopkg.in/inf.v0") {
|
2021-12-12 09:34:17 +01:00
|
|
|
imports = append(imports, "gopkg.in/inf.v0")
|
|
|
|
|
}
|
2024-06-16 08:40:32 -04:00
|
|
|
if c.Type == "duration" && !existsInSlice(imports, "github.com/gocql/gocql") {
|
2021-12-10 09:19:47 +01:00
|
|
|
imports = append(imports, "github.com/gocql/gocql")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-10 19:18:46 +03:00
|
|
|
// Ensure that for each table and materialized view
|
|
|
|
|
//
|
|
|
|
|
// 1. ordered columns are sorted alphabetically;
|
|
|
|
|
// 2. imports are resolves for column types.
|
|
|
|
|
for _, t := range md.Tables {
|
|
|
|
|
sort.Strings(t.OrderedColumns)
|
|
|
|
|
updateImports(t.Columns)
|
|
|
|
|
}
|
|
|
|
|
for _, v := range md.Views {
|
|
|
|
|
sort.Strings(v.OrderedColumns)
|
|
|
|
|
updateImports(v.Columns)
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-13 13:55:44 +02:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
|
"PackageName": *flagPkgname,
|
|
|
|
|
"Tables": md.Tables,
|
2025-06-10 19:18:46 +03:00
|
|
|
"Views": md.Views,
|
2024-06-16 08:40:32 -04:00
|
|
|
"UserTypes": md.Types,
|
2021-12-10 09:19:47 +01:00
|
|
|
"Imports": imports,
|
2021-11-13 13:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-17 11:49:42 +01:00
|
|
|
if err = t.Execute(buf, data); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("template: %w", err)
|
2021-11-13 13:55:44 +02:00
|
|
|
}
|
2021-12-12 09:34:17 +01:00
|
|
|
return format.Source(buf.Bytes())
|
2021-11-13 13:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-17 11:49:42 +01:00
|
|
|
func createSession() (gocqlx.Session, error) {
|
|
|
|
|
cluster := gocql.NewCluster(clusterHosts()...)
|
2024-10-01 06:54:15 -04:00
|
|
|
|
2022-06-30 13:07:15 +02:00
|
|
|
if *flagUser != "" {
|
|
|
|
|
cluster.Authenticator = gocql.PasswordAuthenticator{
|
|
|
|
|
Username: *flagUser,
|
|
|
|
|
Password: *flagPassword,
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-01 06:54:15 -04:00
|
|
|
|
2024-10-01 06:49:16 -04:00
|
|
|
if *flagQueryTimeout >= 0 {
|
|
|
|
|
cluster.Timeout = *flagQueryTimeout
|
|
|
|
|
}
|
|
|
|
|
if *flagConnectionTimeout >= 0 {
|
|
|
|
|
cluster.ConnectTimeout = *flagConnectionTimeout
|
|
|
|
|
}
|
2024-10-01 06:54:15 -04:00
|
|
|
|
|
|
|
|
if *flagSSLCAPath != "" || *flagSSLCertPath != "" || *flagSSLKeyPath != "" {
|
|
|
|
|
cluster.SslOpts = &gocql.SslOptions{
|
|
|
|
|
EnableHostVerification: *flagSSLEnableHostVerification,
|
|
|
|
|
CaPath: *flagSSLCAPath,
|
|
|
|
|
CertPath: *flagSSLCertPath,
|
|
|
|
|
KeyPath: *flagSSLKeyPath,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 11:49:42 +01:00
|
|
|
return gocqlx.WrapSession(cluster.CreateSession())
|
2021-11-13 13:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-17 11:49:42 +01:00
|
|
|
func clusterHosts() []string {
|
2021-11-13 13:55:44 +02:00
|
|
|
return strings.Split(*flagCluster, ",")
|
|
|
|
|
}
|
2021-12-10 09:19:47 +01:00
|
|
|
|
|
|
|
|
func existsInSlice(s []string, v string) bool {
|
|
|
|
|
for _, i := range s {
|
|
|
|
|
if v == i {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-02-23 17:08:44 +01:00
|
|
|
|
|
|
|
|
// 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"]]
|
2024-06-14 13:07:21 -04:00
|
|
|
var userTypes = regexp.MustCompile(`(?:<|\s)(\w+)[>,]`) // match all types contained in set<X>, list<X>, tuple<A, B> etc.
|
2024-02-23 17:08:44 +01:00
|
|
|
|
|
|
|
|
// 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 {
|
2024-06-16 08:40:32 -04:00
|
|
|
if typeName == column.Type {
|
2024-02-23 17:08:44 +01:00
|
|
|
return true
|
|
|
|
|
}
|
2024-06-16 08:40:32 -04:00
|
|
|
matches := userTypes.FindAllStringSubmatch(column.Type, -1)
|
2024-02-23 17:08:44 +01:00
|
|
|
for _, s := range matches {
|
|
|
|
|
if s[1] == typeName {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|