Skip to content

Commit

Permalink
gen_sqlboiler: fix incorrect database path for non-default sqlite loc…
Browse files Browse the repository at this point in the history
…ation (thrasher-corp#603)

* the generator should use the same path as the database/drivers/sqlite3
* remove unused data directory
* generate sqlboiler.json for model generation
* ignore target folder
  • Loading branch information
Rots authored Dec 1, 2020
1 parent ba4ac4f commit 45d0973
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ sqlboiler.json

# GCT API Check
backup.json
.DS_STORE
.DS_STORE

# Designated generated files dir
target/
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ GCTPROFILERLISTENPORT=8085
CRON = $(TRAVIS_EVENT_TYPE)
DRIVER ?= psql
RACE_FLAG := $(if $(NO_RACE_TEST),,-race)
CONFIG_FLAG = $(if $(CONFIG),-config $(CONFIG),)

.PHONY: get linter check test build install update_deps

Expand Down Expand Up @@ -52,9 +53,16 @@ profile_heap:
profile_cpu:
go tool pprof -http "localhost:$(GCTPROFILERLISTENPORT)" 'http://localhost:$(GCTLISTENPORT)/debug/pprof/profile'

gen_db_models:
.PHONY: gen_db_models
gen_db_models: target/sqlboiler.json
ifeq ($(DRIVER), psql)
sqlboiler -o database/models/postgres -p postgres --no-auto-timestamps --wipe $(DRIVER)
sqlboiler -c $< -o database/models/postgres -p postgres --no-auto-timestamps --wipe $(DRIVER)
else ifeq ($(DRIVER), sqlite3)
sqlboiler -c $< -o database/models/sqlite3 -p sqlite3 --no-auto-timestamps --wipe $(DRIVER)
else
sqlboiler -o database/models/sqlite3 -p sqlite3 --no-auto-timestamps --wipe $(DRIVER)
$(error Driver '$(DRIVER)' not supported)
endif

target/sqlboiler.json:
mkdir -p $(@D)
go run ./cmd/gen_sqlboiler_config/main.go $(CONFIG_FLAG) -outdir $(@D)
15 changes: 6 additions & 9 deletions cmd/gen_sqlboiler_config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"

"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/core"
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/database/repository"
)

var (
configFile string
defaultDataDir string
outputFolder string
configFile string
outputFolder string
)

var sqlboilerConfig map[string]driverConfig
Expand All @@ -41,7 +38,6 @@ func main() {
fmt.Println()

flag.StringVar(&configFile, "config", config.DefaultFilePath(), "config file to load")
flag.StringVar(&defaultDataDir, "datadir", common.GetDefaultDataDir(runtime.GOOS), "default data directory for GoCryptoTrader files")
flag.StringVar(&outputFolder, "outdir", "", "overwrite default output folder")
flag.Parse()

Expand Down Expand Up @@ -82,7 +78,7 @@ func convertGCTtoSQLBoilerConfig(c *database.Config) {
dbType = "psql"
}
if dbType == database.DBSQLite || dbType == database.DBSQLite3 {
tempConfig.DBName = convertDBName(c.Database)
tempConfig.DBName = getLoadedDBPath()
} else {
tempConfig.User = c.Username
tempConfig.Pass = c.Password
Expand All @@ -95,6 +91,7 @@ func convertGCTtoSQLBoilerConfig(c *database.Config) {
sqlboilerConfig[dbType] = tempConfig
}

func convertDBName(in string) string {
return filepath.Join(common.GetDefaultDataDir(runtime.GOOS), "database", in)
// getLoadedDBPath gets the path loaded by 'database/drivers/sqlite3'
func getLoadedDBPath() string {
return filepath.Join(database.DB.DataPath, database.DB.Config.Database)
}

0 comments on commit 45d0973

Please sign in to comment.