Skip to content

Commit

Permalink
Huge refactor so that tables can be generic
Browse files Browse the repository at this point in the history
The allows drivers to add extra information to constraints and indexes
  • Loading branch information
stephenafamo committed Nov 16, 2024
1 parent 793e307 commit 023957c
Show file tree
Hide file tree
Showing 46 changed files with 1,020 additions and 963 deletions.
47 changes: 1 addition & 46 deletions gen/aliases.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gen

import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
Expand All @@ -10,26 +9,12 @@ import (
"github.com/volatiletech/strmangle"
)

// Aliases defines aliases for the generation run
type Aliases map[string]TableAlias

// TableAlias defines the spellings for a table name in Go
type TableAlias struct {
UpPlural string `yaml:"up_plural,omitempty" toml:"up_plural,omitempty" json:"up_plural,omitempty"`
UpSingular string `yaml:"up_singular,omitempty" toml:"up_singular,omitempty" json:"up_singular,omitempty"`
DownPlural string `yaml:"down_plural,omitempty" toml:"down_plural,omitempty" json:"down_plural,omitempty"`
DownSingular string `yaml:"down_singular,omitempty" toml:"down_singular,omitempty" json:"down_singular,omitempty"`

Columns map[string]string `yaml:"columns,omitempty" toml:"columns,omitempty" json:"columns,omitempty"`
Relationships map[string]string `yaml:"relationships,omitempty" toml:"relationships,omitempty" json:"relationships,omitempty"`
}

// initAliases takes the table information from the driver
// and fills in aliases where the user has provided none.
//
// This leaves us with a complete list of Go names for all tables,
// columns, and relationships.
func initAliases(a Aliases, tables []drivers.Table, relMap Relationships) {
func initAliases[C, I any](a drivers.Aliases, tables drivers.Tables[C, I], relMap Relationships) {
for _, t := range tables {
tableAlias := a[t.Key]
cleanKey := strings.ReplaceAll(t.Key, ".", "_")
Expand Down Expand Up @@ -76,33 +61,3 @@ func initAliases(a Aliases, tables []drivers.Table, relMap Relationships) {
a[t.Key] = tableAlias
}
}

// Table gets a table alias, panics if not found.
func (a Aliases) Table(table string) TableAlias {
t, ok := a[table]
if !ok {
panic("could not find table aliases for: " + table)
}

return t
}

// Column get's a column's aliased name, panics if not found.
func (t TableAlias) Column(column string) string {
c, ok := t.Columns[column]
if !ok {
panic(fmt.Sprintf("could not find column alias for: %s.%s", t.UpSingular, column))
}

return c
}

// Relationship looks up a relationship, panics if not found.
func (t TableAlias) Relationship(fkey string) string {
r, ok := t.Relationships[fkey]
if !ok {
panic(fmt.Sprintf("could not find relationship alias for: %s.%s", t.UpSingular, fkey))
}

return r
}
10 changes: 5 additions & 5 deletions gen/bobgen-helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func DefaultOutputs(destination, pkgname string, noFactory bool, templates *Temp
return outputs
}

func GetConfigFromFile[DriverConfig any](configPath, driverConfigKey string) (gen.Config, DriverConfig, error) {
func GetConfigFromFile[ConstraintExtra, DriverConfig any](configPath, driverConfigKey string) (gen.Config[ConstraintExtra], DriverConfig, error) {
var provider koanf.Provider
var config gen.Config
var config gen.Config[ConstraintExtra]
var driverConfig DriverConfig

_, err := os.Stat(configPath)
Expand All @@ -85,11 +85,11 @@ func GetConfigFromFile[DriverConfig any](configPath, driverConfigKey string) (ge
return config, driverConfig, err
}

return GetConfigFromProvider[DriverConfig](provider, driverConfigKey)
return GetConfigFromProvider[ConstraintExtra, DriverConfig](provider, driverConfigKey)
}

func GetConfigFromProvider[DriverConfig any](provider koanf.Provider, driverConfigKey string) (gen.Config, DriverConfig, error) {
var config gen.Config
func GetConfigFromProvider[ConstraintExtra, DriverConfig any](provider koanf.Provider, driverConfigKey string) (gen.Config[ConstraintExtra], DriverConfig, error) {
var config gen.Config[ConstraintExtra]
var driverConfig DriverConfig

k := koanf.New(".")
Expand Down
42 changes: 20 additions & 22 deletions gen/bobgen-mysql/driver/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
var rgxEnum = regexp.MustCompile(`^enum\([^\)]+\)$`)

type (
Interface = drivers.Interface[any]
DBInfo = drivers.DBInfo[any]
Interface = drivers.Interface[any, any, any]
DBInfo = drivers.DBInfo[any, any, any]
)

type Config struct {
Expand Down Expand Up @@ -69,10 +69,6 @@ func (d *driver) Dialect() string {
return "mysql"
}

func (d *driver) Capabilities() drivers.Capabilities {
return drivers.Capabilities{}
}

// Assemble all the information we need to provide back to the driver
func (d *driver) Assemble(ctx context.Context) (*DBInfo, error) {
var dbinfo *DBInfo
Expand Down Expand Up @@ -100,7 +96,7 @@ func (d *driver) Assemble(ctx context.Context) (*DBInfo, error) {

dbinfo = &DBInfo{DriverName: "github.com/go-sql-driver/mysql"}

dbinfo.Tables, err = drivers.BuildDBInfo(ctx, d, d.config.Concurrency, d.config.Only, d.config.Except)
dbinfo.Tables, err = drivers.BuildDBInfo[any](ctx, d, d.config.Concurrency, d.config.Only, d.config.Except)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -326,11 +322,11 @@ func (d *driver) Types() drivers.Types {
return d.types
}

func (d *driver) Constraints(ctx context.Context, _ drivers.ColumnFilter) (drivers.DBConstraints, error) {
ret := drivers.DBConstraints{
PKs: map[string]*drivers.Constraint{},
FKs: map[string][]drivers.ForeignKey{},
Uniques: map[string][]drivers.Constraint{},
func (d *driver) Constraints(ctx context.Context, _ drivers.ColumnFilter) (drivers.DBConstraints[any], error) {
ret := drivers.DBConstraints[any]{
PKs: map[string]*drivers.Constraint[any]{},
FKs: map[string][]drivers.ForeignKey[any]{},
Uniques: map[string][]drivers.Constraint[any]{},
}

query := `SELECT
Expand Down Expand Up @@ -364,31 +360,33 @@ func (d *driver) Constraints(ctx context.Context, _ drivers.ColumnFilter) (drive
// Extra for the loop
constraints = append(constraints, constraint{})

var current drivers.Constraint
var current drivers.Constraint[any]
var table, foreignTable, currentTyp string
var foreignCols []string
for i, c := range constraints {
if i != 0 && (c.TableName != table || c.Name != current.Name || c.Type != currentTyp) {
switch currentTyp {
case "PRIMARY KEY":
// Create a new constraint because it is a pointer
ret.PKs[table] = &drivers.Constraint{
ret.PKs[table] = &drivers.Constraint[any]{
Name: current.Name,
Columns: current.Columns,
}
case "UNIQUE":
ret.Uniques[table] = append(ret.Uniques[table], current)
case "FOREIGN KEY":
ret.FKs[table] = append(ret.FKs[table], drivers.ForeignKey{
Name: current.Name,
Columns: current.Columns,
ret.FKs[table] = append(ret.FKs[table], drivers.ForeignKey[any]{
Constraint: drivers.Constraint[any]{
Name: current.Name,
Columns: current.Columns,
},
ForeignTable: foreignTable,
ForeignColumns: foreignCols,
})
}

// reset things
current = drivers.Constraint{}
current = drivers.Constraint[any]{}
table, foreignTable, currentTyp, foreignCols = "", "", "", nil //nolint:ineffassign
}

Expand All @@ -406,8 +404,8 @@ func (d *driver) Constraints(ctx context.Context, _ drivers.ColumnFilter) (drive
return ret, nil
}

func (d *driver) Indexes(ctx context.Context) (drivers.DBIndexes, error) {
ret := drivers.DBIndexes{}
func (d *driver) Indexes(ctx context.Context) (drivers.DBIndexes[any], error) {
ret := drivers.DBIndexes[any]{}

query := `SELECT
s.table_name AS table_name,
Expand All @@ -430,12 +428,12 @@ func (d *driver) Indexes(ctx context.Context) (drivers.DBIndexes, error) {
}
indexColumns = append(indexColumns, indexColumn{})

var current drivers.Index
var current drivers.Index[any]
var table string
for i, c := range indexColumns {
if i != 0 && (c.TableName != table || c.IndexName != current.Name) {
ret[table] = append(ret[table], current)
current = drivers.Index{}
current = drivers.Index[any]{}
table = "" //nolint:ineffassign
}
table = c.TableName
Expand Down
4 changes: 2 additions & 2 deletions gen/bobgen-mysql/driver/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ func TestDriver(t *testing.T) {
os.RemoveAll(out)
}()

testgen.TestDriver(t, testgen.DriverTestConfig[any]{
testgen.TestDriver(t, testgen.DriverTestConfig[any, any, any]{
Root: out,
GetDriver: func() drivers.Interface[any] {
GetDriver: func() drivers.Interface[any, any, any] {
return New(tt.config)
},
GoldenFile: tt.goldenJson,
Expand Down
4 changes: 2 additions & 2 deletions gen/bobgen-mysql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
}

func run(c *cli.Context) error {
config, driverConfig, err := helpers.GetConfigFromFile[driver.Config](c.String("config"), "mysql")
config, driverConfig, err := helpers.GetConfigFromFile[any, driver.Config](c.String("config"), "mysql")
if err != nil {
return err
}
Expand All @@ -55,7 +55,7 @@ func run(c *cli.Context) error {
&helpers.Templates{Models: []fs.FS{gen.MySQLModelTemplates}},
)

state := &gen.State{
state := &gen.State[any]{
Config: config,
Outputs: outputs,
}
Expand Down
2 changes: 1 addition & 1 deletion gen/bobgen-mysql/templates/models/100_blocks.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
type {{$tAlias.UpPlural}}Query = *{{$.Dialect}}.ViewQuery[*{{$tAlias.UpSingular}}, {{$tAlias.UpSingular}}Slice]
{{- else -}}
// {{$tAlias.UpPlural}} contains methods to work with the {{$table.Name}} table
var {{$tAlias.UpPlural}} = {{$.Dialect}}.NewTablex[*{{$tAlias.UpSingular}}, {{$tAlias.UpSingular}}Slice, *{{$tAlias.UpSingular}}Setter]("{{$table.Name}}", {{uniqueColPairs $table}})
var {{$tAlias.UpPlural}} = {{$.Dialect}}.NewTablex[*{{$tAlias.UpSingular}}, {{$tAlias.UpSingular}}Slice, *{{$tAlias.UpSingular}}Setter]("{{$table.Name}}", {{$table.UniqueColPairs}})
// {{$tAlias.UpPlural}}Query is a query on the {{$table.Name}} table
type {{$tAlias.UpPlural}}Query = *{{$.Dialect}}.ViewQuery[*{{$tAlias.UpSingular}}, {{$tAlias.UpSingular}}Slice]
{{- end}}
Expand Down
22 changes: 12 additions & 10 deletions gen/bobgen-psql/driver/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"github.com/stephenafamo/scan/stdscan"
)

func (d *driver) Constraints(ctx context.Context, _ drivers.ColumnFilter) (drivers.DBConstraints, error) {
ret := drivers.DBConstraints{
PKs: map[string]*drivers.Constraint{},
FKs: map[string][]drivers.ForeignKey{},
Uniques: map[string][]drivers.Constraint{},
func (d *driver) Constraints(ctx context.Context, _ drivers.ColumnFilter) (drivers.DBConstraints[any], error) {
ret := drivers.DBConstraints[any]{
PKs: map[string]*drivers.Constraint[any]{},
FKs: map[string][]drivers.ForeignKey[any]{},
Uniques: map[string][]drivers.Constraint[any]{},
}

query := `SELECT
Expand Down Expand Up @@ -87,12 +87,12 @@ func (d *driver) Constraints(ctx context.Context, _ drivers.ColumnFilter) (drive

switch c.Type {
case "p":
ret.PKs[key] = &drivers.Constraint{
ret.PKs[key] = &drivers.Constraint[any]{
Name: c.Name,
Columns: c.Columns,
}
case "u":
ret.Uniques[key] = append(ret.Uniques[c.Table], drivers.Constraint{
ret.Uniques[key] = append(ret.Uniques[c.Table], drivers.Constraint[any]{
Name: c.Name,
Columns: c.Columns,
})
Expand All @@ -101,9 +101,11 @@ func (d *driver) Constraints(ctx context.Context, _ drivers.ColumnFilter) (drive
if c.ForeignSchema.Valid && c.ForeignSchema.String != d.config.SharedSchema {
fkey = c.ForeignSchema.String + "." + c.ForeignTable.String
}
ret.FKs[key] = append(ret.FKs[key], drivers.ForeignKey{
Name: key + "." + c.Name,
Columns: c.Columns,
ret.FKs[key] = append(ret.FKs[key], drivers.ForeignKey[any]{
Constraint: drivers.Constraint[any]{
Name: key + "." + c.Name,
Columns: c.Columns,
},
ForeignTable: fkey,
ForeignColumns: c.ForeignColumns,
})
Expand Down
16 changes: 6 additions & 10 deletions gen/bobgen-psql/driver/psql.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
var rgxValidColumnName = regexp.MustCompile(`(?i)^[a-z_][a-z0-9_]*$`)

type (
Interface = drivers.Interface[any]
DBInfo = drivers.DBInfo[any]
Interface = drivers.Interface[any, any, any]
DBInfo = drivers.DBInfo[any, any, any]
)

type Enum struct {
Expand Down Expand Up @@ -120,10 +120,6 @@ func (d *driver) Types() drivers.Types {
return d.types
}

func (d *driver) Capabilities() drivers.Capabilities {
return drivers.Capabilities{}
}

// Assemble all the information we need to provide back to the driver
func (d *driver) Assemble(ctx context.Context) (*DBInfo, error) {
var dbinfo *DBInfo
Expand All @@ -146,7 +142,7 @@ func (d *driver) Assemble(ctx context.Context) (*DBInfo, error) {
return nil, fmt.Errorf("unable to load enums: %w", err)
}

dbinfo.Tables, err = drivers.BuildDBInfo(ctx, d, d.config.Concurrency, d.config.Only, d.config.Except)
dbinfo.Tables, err = drivers.BuildDBInfo[any](ctx, d, d.config.Concurrency, d.config.Only, d.config.Except)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -448,8 +444,8 @@ func (d *driver) loadEnums(ctx context.Context) error {
return nil
}

func (d *driver) Indexes(ctx context.Context) (drivers.DBIndexes, error) {
ret := drivers.DBIndexes{}
func (d *driver) Indexes(ctx context.Context) (drivers.DBIndexes[any], error) {
ret := drivers.DBIndexes[any]{}

query := `SELECT
n.nspname AS schema_name,
Expand Down Expand Up @@ -482,7 +478,7 @@ func (d *driver) Indexes(ctx context.Context) (drivers.DBIndexes, error) {
if r.SchemaName != "" && r.SchemaName != d.config.SharedSchema {
key = r.SchemaName + "." + r.TableName
}
var index drivers.Index
var index drivers.Index[any]
index.Name = r.IndexName
for _, colName := range r.IndexCols {
if rgxValidColumnName.MatchString(colName) {
Expand Down
4 changes: 2 additions & 2 deletions gen/bobgen-psql/driver/psql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ func TestDriver(t *testing.T) {
os.RemoveAll(out)
}()

testgen.TestDriver(t, testgen.DriverTestConfig[any]{
testgen.TestDriver(t, testgen.DriverTestConfig[any, any, any]{
Root: out,
GetDriver: func() drivers.Interface[any] {
GetDriver: func() drivers.Interface[any, any, any] {
return New(tt.config)
},
GoldenFile: tt.goldenJson,
Expand Down
4 changes: 2 additions & 2 deletions gen/bobgen-psql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
}

func run(c *cli.Context) error {
config, driverConfig, err := helpers.GetConfigFromFile[driver.Config](c.String("config"), "psql")
config, driverConfig, err := helpers.GetConfigFromFile[any, driver.Config](c.String("config"), "psql")
if err != nil {
return err
}
Expand All @@ -55,7 +55,7 @@ func run(c *cli.Context) error {
&helpers.Templates{Models: []fs.FS{gen.PSQLModelTemplates}},
)

state := &gen.State{
state := &gen.State[any]{
Config: config,
Outputs: outputs,
}
Expand Down
Loading

0 comments on commit 023957c

Please sign in to comment.