Skip to content

Commit

Permalink
Add --sqlite option (evcc-io#4841)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Oct 16, 2022
1 parent b26cf6d commit 59bcc1f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var conf = config{
},
Database: dbConfig{
Type: "sqlite",
Dsn: "~/.evcc/evcc.db?_pragma=busy_timeout(5000)",
Dsn: "~/.evcc/evcc.db",
},
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
)

const (
flagSqlite = "sqlite"
flagSqliteDescription = "Sqlite database file"

flagHeaders = "log-headers"
flagHeadersDescription = "Log headers"

Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func init() {

rootCmd.PersistentFlags().Bool(flagHeaders, false, flagHeadersDescription)

rootCmd.PersistentFlags().String(flagSqlite, "", flagSqliteDescription)

// config file options
rootCmd.PersistentFlags().StringP("log", "l", "info", "Log level (fatal, error, warn, info, debug, trace)")
bindP(rootCmd, "log")
Expand Down
4 changes: 4 additions & 0 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func configureEnvironment(cmd *cobra.Command, conf config) (err error) {

// setup persistence
if err == nil && conf.Database.Dsn != "" {
if flag := cmd.Flags().Lookup(flagSqlite); flag.Changed {
conf.Database.Type = "sqlite"
conf.Database.Dsn = flag.Value.String()
}
err = configureDatabase(conf.Database)
}

Expand Down
3 changes: 2 additions & 1 deletion server/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func New(driver, dsn string) (*gorm.DB, error) {
if err := os.MkdirAll(filepath.Dir(file), os.ModePerm); err != nil {
return nil, err
}
dialect = sqlite.Open(file)
// avoid busy errors
dialect = sqlite.Open(file + "?_pragma=busy_timeout(5000)")
// case "postgres":
// dialect = postgres.Open(dsn)
// case "mysql":
Expand Down

0 comments on commit 59bcc1f

Please sign in to comment.