Skip to content

Commit

Permalink
adding timestamps to the version table
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacobbrewer1 committed Jan 18, 2025
1 parent 27a7adb commit 18b5f8f
Show file tree
Hide file tree
Showing 693 changed files with 288,851 additions and 52 deletions.
30 changes: 30 additions & 0 deletions cmd_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"context"
"flag"

"github.com/google/subcommands"
)

type statusCmd struct{}

Check failure on line 10 in cmd_status.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

type `statusCmd` is unused (unused)

func (c *statusCmd) Name() string {

Check failure on line 12 in cmd_status.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

func `(*statusCmd).Name` is unused (unused)
return "status"
}

func (c *statusCmd) Synopsis() string {

Check failure on line 16 in cmd_status.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

func `(*statusCmd).Synopsis` is unused (unused)
return "Print the status of the database migrations."
}

func (c *statusCmd) Usage() string {

Check failure on line 20 in cmd_status.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

func `(*statusCmd).Usage` is unused (unused)
return `status:
Print the status of the database migrations.
`
}

func (c *statusCmd) SetFlags(f *flag.FlagSet) {}

Check failure on line 26 in cmd_status.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

func `(*statusCmd).SetFlags` is unused (unused)

func (c *statusCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {

Check failure on line 28 in cmd_status.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

func `(*statusCmd).Execute` is unused (unused)
return subcommands.ExitSuccess
}
13 changes: 10 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ require (
github.com/google/subcommands v1.2.0
github.com/hashicorp/vault/api v1.15.0
github.com/huandu/xstrings v1.5.0
github.com/jacobbrewer1/patcher v0.1.14
github.com/jacobbrewer1/vaulty v0.1.6
github.com/jmoiron/sqlx v1.4.0
github.com/pingcap/tidb/pkg/parser v0.0.0-20241220080229-acba0cd1e2b0
github.com/prometheus/client_golang v1.20.5
github.com/stretchr/testify v1.10.0
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/go-jose/go-jose/v4 v4.0.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand All @@ -38,15 +41,18 @@ require (
github.com/hashicorp/vault/api/auth/kubernetes v0.8.0 // indirect
github.com/hashicorp/vault/api/auth/userpass v0.8.0 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pingcap/errors v0.11.5-0.20240311024730-e056997136bb // indirect
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect
github.com/pingcap/log v1.1.1-0.20230317032135-a0d097d16e22 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
Expand All @@ -55,9 +61,10 @@ require (
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
23 changes: 20 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZC
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand Down Expand Up @@ -67,10 +70,14 @@ github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/jacobbrewer1/patcher v0.1.14 h1:+E043ZXH4pyT+nMM0R8eKhA3/fCvmVvrnbxpWnEDoXE=
github.com/jacobbrewer1/patcher v0.1.14/go.mod h1:RTWS5kY5uTo0VBv7PcFamRy6UGiNvM6f4zGvIShBw0k=
github.com/jacobbrewer1/vaulty v0.1.6 h1:h+STKGtDwVugWmeVu3ixlWiuU/DQYmJj0x5ADq1jm5Y=
github.com/jacobbrewer1/vaulty v0.1.6/go.mod h1:kKQcDjAdRZLTlaOv6HbhqDXObDjIBgWC7FBC9rdDfbA=
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand Down Expand Up @@ -99,6 +106,8 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/pingcap/errors v0.11.0/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
github.com/pingcap/errors v0.11.5-0.20240311024730-e056997136bb h1:3pSi4EDG6hg0orE1ndHkXvX6Qdq2cZn8gAPir8ymKZk=
github.com/pingcap/errors v0.11.5-0.20240311024730-e056997136bb/go.mod h1:X2r9ueLEUZgtx2cIogM0v4Zj5uvvzhuuiu7Pn8HzMPg=
Expand All @@ -108,13 +117,19 @@ github.com/pingcap/log v1.1.1-0.20230317032135-a0d097d16e22 h1:2SOzvGvE8beiC1Y4g
github.com/pingcap/log v1.1.1-0.20230317032135-a0d097d16e22/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
github.com/pingcap/tidb/pkg/parser v0.0.0-20241220080229-acba0cd1e2b0 h1:gNmGUmjwyQj0nvgVhJNCaIG2+Utrg3gI/Qakv8qRrzI=
github.com/pingcap/tidb/pkg/parser v0.0.0-20241220080229-acba0cd1e2b0/go.mod h1:Hju1TEWZvrctQKbztTRwXH7rd41Yq0Pgmq4PrEKcq7o=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
Expand Down Expand Up @@ -165,6 +180,8 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/column.go → pkg/entities/column.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package entities

import (
"log/slog"
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/constraint.go → pkg/entities/constraint.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package entities

import (
"github.com/pingcap/tidb/pkg/parser/ast"
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/key.go → pkg/entities/key.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package entities

// Key represents a MySQL key (primary, unique, index, etc)
type Key struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/table.go → pkg/entities/table.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package entities

import (
"log"
Expand Down
14 changes: 7 additions & 7 deletions pkg/generation/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ import (
"path/filepath"
"strings"

"github.com/jacobbrewer1/goschema/pkg/models"
"github.com/jacobbrewer1/goschema/pkg/entities"
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/parser/ast"
_ "github.com/pingcap/tidb/pkg/parser/test_driver"
)

// LoadSQL loads all SQL files in the given paths and parses them
func LoadSQL(paths ...string) ([]*models.Table, error) {
func LoadSQL(paths ...string) ([]*entities.Table, error) {
p := parser.New()
tables := make([]*models.Table, 0)
tables := make([]*entities.Table, 0)

for _, path := range paths {
matches, err := filepath.Glob(path)
if err != nil {
return nil, err
}
for _, m := range matches {
var matchedTables []*models.Table
var matchedTables []*entities.Table
if fi, err := os.Stat(m); err != nil {
return nil, err
} else if fi.IsDir() {
Expand All @@ -49,7 +49,7 @@ func LoadSQL(paths ...string) ([]*models.Table, error) {
return tables, nil
}

func parseSQL(p *parser.Parser, path string) ([]*models.Table, error) {
func parseSQL(p *parser.Parser, path string) ([]*entities.Table, error) {
sql, err := os.ReadFile(path)
if err != nil {
return nil, err
Expand All @@ -73,15 +73,15 @@ func parseSQL(p *parser.Parser, path string) ([]*models.Table, error) {
return nil, fmt.Errorf("error parsing SQL: %w", err)
}

tables := make([]*models.Table, 0, len(stmts))
tables := make([]*entities.Table, 0, len(stmts))
for _, stmt := range stmts {
ct, ok := stmt.(*ast.CreateTableStmt)
if !ok {
// We only support create table statements
continue
}

t, err := models.NewTable(ct)
t, err := entities.NewTable(ct)
if err != nil {
return nil, fmt.Errorf("error creating table from statement: %w", err)
}
Expand Down
38 changes: 19 additions & 19 deletions pkg/generation/template_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"text/template"

"github.com/huandu/xstrings"
"github.com/jacobbrewer1/goschema/pkg/models"
"github.com/jacobbrewer1/goschema/pkg/entities"
)

// Helpers defines the map of functions exposed to generation templates
Expand All @@ -29,12 +29,12 @@ var Helpers = template.FuncMap{
}

// hasPrimaryKey returns true if the table has a primary key
func hasPrimaryKey(t *models.Table) bool {
func hasPrimaryKey(t *entities.Table) bool {
return t.PrimaryKey != nil
}

// hasAutoIncrementingKey returns true if the table has an auto-incrementing key
func hasAutoIncrementingKey(t *models.Table) bool {
func hasAutoIncrementingKey(t *entities.Table) bool {
for _, col := range t.Columns {
if col.AutoIncrementing {
return true
Expand All @@ -45,7 +45,7 @@ func hasAutoIncrementingKey(t *models.Table) bool {
}

// primaryAutoIncColumn returns a column that is both auto-incrementing and part of the primary key, if any
func primaryAutoIncColumn(t *models.Table) *models.Column {
func primaryAutoIncColumn(t *entities.Table) *entities.Column {
for _, col := range t.Columns {
if col.AutoIncrementing && col.InPrimaryKey {
return col
Expand All @@ -56,7 +56,7 @@ func primaryAutoIncColumn(t *models.Table) *models.Column {
}

// autoIncColumn returns the auto-incrementing column, if any
func autoIncColumn(t *models.Table) *models.Column {
func autoIncColumn(t *entities.Table) *entities.Column {
for _, col := range t.Columns {
if col.AutoIncrementing {
return col
Expand All @@ -67,8 +67,8 @@ func autoIncColumn(t *models.Table) *models.Column {
}

// nonAutoIncColumns returns the non-auto-incrementing columns, if any
func nonAutoIncColumns(t *models.Table) []*models.Column {
cols := make([]*models.Column, 0, len(t.Columns)-1)
func nonAutoIncColumns(t *entities.Table) []*entities.Column {
cols := make([]*entities.Column, 0, len(t.Columns)-1)
for _, col := range t.Columns {
if !col.AutoIncrementing {
cols = append(cols, col)
Expand All @@ -82,7 +82,7 @@ func nonAutoIncColumns(t *models.Table) []*models.Column {
// Columns uniquely identifying a row relies on being in at least one of the following:
// - a primary key
// - a unique key
func identityColumns(t *models.Table) []*models.Column {
func identityColumns(t *entities.Table) []*entities.Column {
if t.PrimaryKey != nil {
return t.PrimaryKey.Columns
}
Expand All @@ -101,18 +101,18 @@ func identityColumns(t *models.Table) []*models.Column {
// Columns that are not uniquely identifying must not be in any of the following:
// - a primary key
// - a unique key
func nonIdentityColumns(t *models.Table) []*models.Column {
func nonIdentityColumns(t *entities.Table) []*entities.Column {
cols := identityColumns(t)
if cols == nil {
return t.Columns
}

checker := make(map[*models.Column]struct{}, len(cols))
checker := make(map[*entities.Column]struct{}, len(cols))
for _, col := range cols {
checker[col] = struct{}{}
}

ret := make([]*models.Column, 0, len(t.Columns)-len(cols))
ret := make([]*entities.Column, 0, len(t.Columns)-len(cols))
for _, col := range t.Columns {
if _, ok := checker[col]; !ok {
ret = append(ret, col)
Expand All @@ -134,8 +134,8 @@ func structify(s string) string {
}

// enumColumns returns the columns which are enum types
func enumColumns(t *models.Table) []*models.Column {
ret := make([]*models.Column, 0)
func enumColumns(t *entities.Table) []*entities.Column {
ret := make([]*entities.Column, 0)
for _, col := range t.Columns {
if col.Type == "enum" {
ret = append(ret, col)
Expand All @@ -148,9 +148,9 @@ func enumColumns(t *models.Table) []*models.Column {
// uniqueColumnKeys returns a list of keys, none of which have the same set of columns as each other
// This is required because you can have mulitple indexs including the exact same columns in the same order,
// but of different types (unique, non-unique, etc). Includes the primary key, if any.
func uniqueColumnKeys(t *models.Table) []models.Key {
func uniqueColumnKeys(t *entities.Table) []entities.Key {
m := make(map[string]struct{}, len(t.Keys))
keys := make([]models.Key, 0, len(t.Keys))
keys := make([]entities.Key, 0, len(t.Keys))
if t.PrimaryKey != nil {
keys = append(keys, *t.PrimaryKey)
m[fmt.Sprint(t.PrimaryKey.Columns)] = struct{}{}
Expand All @@ -167,14 +167,14 @@ func uniqueColumnKeys(t *models.Table) []models.Key {
}

// sortedColumns returns a slice of the columns for a given table sorted alphabetically
func sortedColumns(t *models.Table) []*models.Column {
ret := make([]*models.Column, len(t.Columns))
func sortedColumns(t *entities.Table) []*entities.Column {
ret := make([]*entities.Column, len(t.Columns))
copy(ret, t.Columns)
sort.SliceStable(ret, func(i, j int) bool { return ret[i].Name < ret[j].Name })
return ret
}

func getType(col *models.Column) string {
func getType(col *entities.Column) string {
switch strings.ToLower(col.Type) {
case "bigint":
if col.Nullable {
Expand Down Expand Up @@ -263,7 +263,7 @@ func getType(col *models.Column) string {
}
}

func getTags(col *models.Column) string {
func getTags(col *entities.Column) string {
tags := fmt.Sprintf("`db:\"%s", col.Name)
if col.InPrimaryKey {
tags += ",pk"
Expand Down
8 changes: 4 additions & 4 deletions pkg/generation/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (

"github.com/Masterminds/sprig"
"github.com/huandu/xstrings"
"github.com/jacobbrewer1/goschema/pkg/models"
"github.com/jacobbrewer1/goschema/pkg/entities"
)

type templateInfo struct {
OutputDir string
Table *models.Table
Table *entities.Table
}

func RenderTemplates(tables []*models.Table, templatesLoc, outputLoc string, fileExtensionPrefix string) error {
func RenderTemplates(tables []*entities.Table, templatesLoc, outputLoc string, fileExtensionPrefix string) error {
tmpl, err := template.New("model.tmpl").Funcs(sprig.TxtFuncMap()).Funcs(Helpers).ParseGlob(templatesLoc)
if err != nil {
return fmt.Errorf("error parsing templates: %w", err)
Expand All @@ -40,7 +40,7 @@ func RenderTemplates(tables []*models.Table, templatesLoc, outputLoc string, fil
}

// RenderWithTemplates renders templates that are provided as embedded files
func RenderWithTemplates(fs embed.FS, tables []*models.Table, outputLoc string, fileExtensionPrefix string) error {
func RenderWithTemplates(fs embed.FS, tables []*entities.Table, outputLoc string, fileExtensionPrefix string) error {
tmpl, err := template.New("model.tmpl").Funcs(sprig.TxtFuncMap()).Funcs(Helpers).ParseFS(fs, "templates/*.tmpl")
if err != nil {
return fmt.Errorf("error parsing templates: %w", err)
Expand Down
Loading

0 comments on commit 18b5f8f

Please sign in to comment.