Add --sqlite option (#4841)

This commit is contained in:
andig 2022-10-16 14:19:22 +02:00 • committed by GitHub
parent b26cf6d9b9
commit 59bcc1fa52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 2 deletions

View file

@ -38,7 +38,7 @@ var conf = config{
},
Database: dbConfig{
Type: "sqlite",
Dsn: "~/.evcc/evcc.db?_pragma=busy_timeout(5000)",
Dsn: "~/.evcc/evcc.db",
},
}

View file

@ -8,6 +8,9 @@ import (
)
const (
flagSqlite = "sqlite"
flagSqliteDescription = "Sqlite database file"
flagHeaders = "log-headers"
flagHeadersDescription = "Log headers"

View file

@ -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")

View file

@ -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)
}

View file

@ -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":