Bring back materialized view generation (#321)
This commit is contained in:
committed by
GitHub
parent
368eebb8a7
commit
16bd2372a7
@@ -36,6 +36,33 @@ var (
|
|||||||
{{end}}
|
{{end}}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Materialized view models.
|
||||||
|
var (
|
||||||
|
{{with .Views}}
|
||||||
|
{{range .}}
|
||||||
|
{{$model_name := .ViewName | camelize}}
|
||||||
|
{{$model_name}} = table.New(table.Metadata {
|
||||||
|
Name: "{{.ViewName}}",
|
||||||
|
Columns: []string{
|
||||||
|
{{- range .OrderedColumns}}
|
||||||
|
"{{.}}",
|
||||||
|
{{- end}}
|
||||||
|
},
|
||||||
|
PartKey: []string {
|
||||||
|
{{- range .PartitionKey}}
|
||||||
|
"{{.Name}}",
|
||||||
|
{{- end}}
|
||||||
|
},
|
||||||
|
SortKey: []string{
|
||||||
|
{{- range .ClusteringColumns}}
|
||||||
|
"{{.Name}}",
|
||||||
|
{{- end}}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
{{end}}
|
||||||
|
{{end}}
|
||||||
|
)
|
||||||
|
|
||||||
{{with .UserTypes}}
|
{{with .UserTypes}}
|
||||||
{{range .}}
|
{{range .}}
|
||||||
{{- $type_name := .Name | camelize}}
|
{{- $type_name := .Name | camelize}}
|
||||||
@@ -61,3 +88,16 @@ type {{$model_name}}Struct struct {
|
|||||||
}
|
}
|
||||||
{{- end}}
|
{{- end}}
|
||||||
{{- end}}
|
{{- end}}
|
||||||
|
|
||||||
|
{{with .Views}}
|
||||||
|
{{range .}}
|
||||||
|
{{- $model_name := .ViewName | camelize}}
|
||||||
|
type {{$model_name}}Struct struct {
|
||||||
|
{{- range .Columns}}
|
||||||
|
{{- if not (eq .Type "empty") }}
|
||||||
|
{{.Name | camelize}} {{.Type | mapScyllaToGoType}}
|
||||||
|
{{- end}}
|
||||||
|
{{- end}}
|
||||||
|
}
|
||||||
|
{{- end}}
|
||||||
|
{{- end}}
|
||||||
|
|||||||
@@ -128,11 +128,8 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
|
|||||||
imports = append(imports, "github.com/scylladb/gocqlx/v3")
|
imports = append(imports, "github.com/scylladb/gocqlx/v3")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, t := range md.Tables {
|
updateImports := func(columns map[string]*gocql.ColumnMetadata) {
|
||||||
// Ensure ordered columns are sorted alphabetically
|
for _, c := range columns {
|
||||||
sort.Strings(t.OrderedColumns)
|
|
||||||
|
|
||||||
for _, c := range t.Columns {
|
|
||||||
if (c.Type == "timestamp" || c.Type == "date" || c.Type == "time") && !existsInSlice(imports, "time") {
|
if (c.Type == "timestamp" || c.Type == "date" || c.Type == "time") && !existsInSlice(imports, "time") {
|
||||||
imports = append(imports, "time")
|
imports = append(imports, "time")
|
||||||
}
|
}
|
||||||
@@ -145,10 +142,24 @@ func renderTemplate(md *gocql.KeyspaceMetadata) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
"PackageName": *flagPkgname,
|
"PackageName": *flagPkgname,
|
||||||
"Tables": md.Tables,
|
"Tables": md.Tables,
|
||||||
|
"Views": md.Views,
|
||||||
"UserTypes": md.Types,
|
"UserTypes": md.Types,
|
||||||
"Imports": imports,
|
"Imports": imports,
|
||||||
}
|
}
|
||||||
|
|||||||
22
cmd/schemagen/testdata/models.go
vendored
22
cmd/schemagen/testdata/models.go
vendored
@@ -47,6 +47,23 @@ var (
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Materialized view models.
|
||||||
|
var (
|
||||||
|
ComposersByName = table.New(table.Metadata{
|
||||||
|
Name: "composers_by_name",
|
||||||
|
Columns: []string{
|
||||||
|
"id",
|
||||||
|
"name",
|
||||||
|
},
|
||||||
|
PartKey: []string{
|
||||||
|
"id",
|
||||||
|
},
|
||||||
|
SortKey: []string{
|
||||||
|
"name",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
type AlbumUserType struct {
|
type AlbumUserType struct {
|
||||||
gocqlx.UDT
|
gocqlx.UDT
|
||||||
Name string
|
Name string
|
||||||
@@ -69,3 +86,8 @@ type SongsStruct struct {
|
|||||||
Tags []string
|
Tags []string
|
||||||
Title string
|
Title string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ComposersByNameStruct struct {
|
||||||
|
Id [16]byte
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user