Skip to content

Commit

Permalink
Create local database if does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
lewissteele committed Nov 27, 2024
1 parent 178f4bf commit 7c0274f
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions internal/db/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,30 @@ func SaveHistory(query string) {
}

func init() {
configHome := os.Getenv("XDG_CONFIG_HOME")
config := os.Getenv("XDG_CONFIG_HOME")

if len(configHome) == 0 {
configHome = filepath.Join(os.Getenv("HOME"), ".config")
if len(config) == 0 {
config = filepath.Join(os.Getenv("HOME"), ".config")
}

var err error
path := filepath.Join(configHome, "dbat/dbat.db")
err := os.MkdirAll(
filepath.Join(config, "dbat"),
0700,
)

if err != nil {
panic("cannot create config dir")
}

db := filepath.Join(config, "dbat/dbat.db")

LocalDB, err = gorm.Open(sqlite.Open(path), &gorm.Config{
LocalDB, err = gorm.Open(sqlite.Open(db), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
SkipDefaultTransaction: true,
})

if err != nil {
panic("could not connect to sqlite db")
panic("cannot connect to dbat.db")
}

LocalDB.AutoMigrate(
Expand Down

0 comments on commit 7c0274f

Please sign in to comment.