Skip to content

Commit

Permalink
taskqueueworker: parameterize sql persistent
Browse files Browse the repository at this point in the history
  • Loading branch information
agungdwiprasetyo committed May 22, 2023
1 parent efbb3d0 commit 614b389
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 204 deletions.
8 changes: 4 additions & 4 deletions cmd/candi/template_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ import (
"github.com/pressly/goose/v3"
{{if eq .SQLDriver "postgres"}}_ "github.com/lib/pq"{{else if eq .SQLDriver "mysql"}}_ "github.com/go-sql-driver/mysql"{{end}}
"gorm.io/driver/{{.SQLDriver}}"
{{if eq .SQLDriver "postgres"}}_ "github.com/lib/pq"{{else if eq .SQLDriver "mysql"}}_ "github.com/go-sql-driver/mysql"{{else if eq .SQLDriver "sqlite3"}}_ "github.com/mattn/go-sqlite3"{{end}}
{{if eq .SQLDriver "sqlite3"}}"gorm.io/driver/sqlite"{{else}}"gorm.io/driver/{{.SQLDriver}}"{{end}}
"gorm.io/gorm"
)
Expand Down Expand Up @@ -90,9 +90,9 @@ func main() {
}
if migrateTables := migrations.GetMigrateTables(); len(migrateTables) > 0 {
gormWrite, err := gorm.Open({{ .SQLDriver }}.New({{ .SQLDriver }}.Config{
gormWrite, err := gorm.Open({{if eq .SQLDriver "sqlite3"}}sqlite.Dialector{Conn: sqlDeps.WriteDB()}{{else}}{{ .SQLDriver }}.New({{ .SQLDriver }}.Config{
Conn: sqlDeps.WriteDB(),
}), &gorm.Config{
}){{end}}, &gorm.Config{
SkipDefaultTransaction: true,
DisableForeignKeyConstraintWhenMigrating: true,
})
Expand Down
12 changes: 6 additions & 6 deletions cmd/candi/template_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
{{ if .IsMonorepo }}"monorepo/globalshared"{{else}}"{{$.PackagePrefix}}/pkg/shared"{{end}}
"gorm.io/driver/{{.SQLDriver}}"
{{if eq .SQLDriver "sqlite3"}}"gorm.io/driver/sqlite"{{else}}"gorm.io/driver/{{.SQLDriver}}"{{end}}
"gorm.io/gorm"{{end}}` + `
)
Expand All @@ -70,20 +70,20 @@ var (
// setSharedRepoSQL set the global singleton "RepoSQL" implementation
func setSharedRepoSQL(readDB, writeDB *sql.DB) {
{{if .SQLUseGORM}}gormRead, err := gorm.Open({{.SQLDriver}}.New({{.SQLDriver}}.Config{
{{if .SQLUseGORM}}gormRead, err := gorm.Open({{if eq .SQLDriver "sqlite3"}}sqlite.Dialector{Conn: readDB}{{else}}{{ .SQLDriver }}.New({{ .SQLDriver }}.Config{
Conn: readDB,
}), &gorm.Config{})
}){{end}}, &gorm.Config{})
if err != nil {
panic(err)
}
gormWrite, err := gorm.Open({{.SQLDriver}}.New({{.SQLDriver}}.Config{
gormWrite, err := gorm.Open({{if eq .SQLDriver "sqlite3"}}sqlite.Dialector{Conn: writeDB}{{else}}{{ .SQLDriver }}.New({{ .SQLDriver }}.Config{
Conn: writeDB,
}), &gorm.Config{SkipDefaultTransaction: true})
}){{end}}, &gorm.Config{SkipDefaultTransaction: true})
if err != nil {
panic(err)
}
{{ if .IsMonorepo }}global{{end}}shared.AddGormCallbacks(gormRead)
{{ if .IsMonorepo }}global{{end}}shared.AddGormCallbacks(gormWrite){{end}}
Expand Down
Loading

0 comments on commit 614b389

Please sign in to comment.