Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: support dialect postgres #6

Merged
merged 2 commits into from
Nov 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 6 additions & 28 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
module github.com/iKala/gorm-cli

go 1.19

require (
github.com/manifoldco/promptui v0.3.2
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/exp v0.0.0-20221114191408-850992195362
gopkg.in/yaml.v2 v2.2.2
gorm.io/gorm v1.21.15
)
go 1.14

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/alecthomas/gometalinter v2.0.11+incompatible // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/client9/misspell v0.3.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3 // indirect
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf // indirect
github.com/gordonklaus/ineffassign v0.0.0-20180909121442-1003c8bd00dc // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.2 // indirect
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a // indirect
github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/manifoldco/promptui v0.3.2
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/tools v0.2.0 // indirect
golang.org/x/tools v0.0.0-20190312170243-e65039ee4138 // indirect
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect
gopkg.in/yaml.v2 v2.2.2
gorm.io/gorm v1.21.15
)
32 changes: 20 additions & 12 deletions migrate/create_connection.go
Original file line number Diff line number Diff line change
@@ -10,38 +10,46 @@ import (

type tmplData struct {
GormCliConfig
DialectString string
Port string
}

// CreateConnection - Create the migration file with template.
func CreateConnection(c GormCliConfig) (string, error) {
// Create migration folder anyway.
_ = os.Mkdir(MigrationTargetFolder, os.ModePerm)

data := tmplData{GormCliConfig: c}
if c.DB.Dialects == "mysql" {
data.DialectString = `"gorm.io/driver/mysql"`
}
dialectString := ""
dsn := ""

if c.DB.Port == "" {
data.Port = "3306"
data := tmplData{GormCliConfig: c}
switch c.DB.Dialects {
case "mysql":
dialectString = `"gorm.io/driver/mysql"`
dsn = `"{{.DB.User}}:{{.DB.Password}}@tcp({{.DB.Host}}:{{.DB.Port}})/{{.DB.Dbname}}?charset={{.DB.Charset}}&parseTime=True&loc=Local"`
case "postgres":
if data.DB.TimeZone == "" {
data.DB.TimeZone = "UTC"
}
if data.DB.SSLMode == "" {
data.DB.SSLMode = "disable"
}
dialectString = `"gorm.io/driver/postgres"`
dsn = `"host=({{.DB.Host}} user={{.DB.User}} password={{.DB.Password}} dbname={{.DB.Dbname}} port={{.DB.Port}} sslmode={{.DB.SSLMode}} TimeZone={{.DB.TimeZone}}"`
}

connectionTemplate :=
connectionTemplate := fmt.Sprintf(
`package main
import (
"gorm.io/gorm"
{{.DialectString}}
%v
)
// NewDB - Get gorm DB instance.
func NewDB() (*gorm.DB, error) {
dsn := "{{.DB.User}}:{{.DB.Password}}@tcp({{.DB.Host}}:{{.DB.Port}})/{{.DB.Dbname}}?charset={{.DB.Charset}}&parseTime=True&loc=Local"
dsn := %v
db, err := gorm.Open({{.DB.Dialects}}.Open(dsn), &gorm.Config{})
return db, err
}`
}`, dialectString, dsn)

tmpl, err := template.New("connection").Parse(connectionTemplate)
if err != nil {
1 change: 0 additions & 1 deletion migrate/error.go
Original file line number Diff line number Diff line change
@@ -8,5 +8,4 @@ var (
ErrMigrationCanceled = errors.New("rollback migration canceled")
ErrEmptyPurpose = errors.New("missing purpose when creating migration")
ErrDuplicatedMigration = errors.New("migration exists")
ErrNoMigration = errors.New("there is no migration")
)
2 changes: 2 additions & 0 deletions migrate/model.go
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@ type GormCliConfig struct {
Password string
Dbname string
Charset string
SSLMode string
TimeZone string
}
Migration struct {
Path string