Merge pull request #31 from matthewcmead/sqlite_for_server

allow for sqlite backend database for low-profile self-hosting deploy…
This commit is contained in:
David Dworken 2022-11-16 20:50:34 -08:00 committed by GitHub
commit 4aacd8b5e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -393,14 +393,29 @@ func OpenDB() (*gorm.DB, error) {
return db, nil
}
postgresDb := fmt.Sprintf(PostgresDb, os.Getenv("POSTGRESQL_PASSWORD"))
if os.Getenv("HISHTORY_POSTGRES_DB") != "" {
postgresDb = os.Getenv("HISHTORY_POSTGRES_DB")
var sqliteDb string
if os.Getenv("HISHTORY_SQLITE_DB") != "" {
sqliteDb = os.Getenv("HISHTORY_SQLITE_DB")
}
db, err := gorm.Open(postgres.Open(postgresDb), &gorm.Config{})
var db *gorm.DB
var err error
if sqliteDb != "" {
db, err = gorm.Open(sqlite.Open(sqliteDb), &gorm.Config{})
} else {
postgresDb := fmt.Sprintf(PostgresDb, os.Getenv("POSTGRESQL_PASSWORD"))
if os.Getenv("HISHTORY_POSTGRES_DB") != "" {
postgresDb = os.Getenv("HISHTORY_POSTGRES_DB")
}
db, err = gorm.Open(postgres.Open(postgresDb), &gorm.Config{})
}
if err != nil {
return nil, fmt.Errorf("failed to connect to the DB: %v", err)
}
db.AutoMigrate(&shared.EncHistoryEntry{})
db.AutoMigrate(&shared.Device{})
db.AutoMigrate(&UsageData{})