Skip to content

Commit

Permalink
Set all anti-cgo options and add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
kradalby committed Feb 23, 2022
1 parent 67f5c32 commit 686e45c
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package headscale

import (
"errors"
"time"

"github.com/glebarez/sqlite"
"gorm.io/driver/postgres"
Expand Down Expand Up @@ -81,13 +82,24 @@ func (h *Headscale) openDB() (*gorm.DB, error) {

switch h.dbType {
case Sqlite:
db, err = gorm.Open(sqlite.Open(h.dbString), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
Logger: log,
})

db, err = gorm.Open(
sqlite.Open(h.dbString+"?_synchronous=1&_journal_mode=WAL"),
&gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
Logger: log,
},
)

db.Exec("PRAGMA foreign_keys=ON")

// The pure Go SQLite library does not handle locking in
// the same way as the C based one and we cant use the gorm
// connection pool as of 2022/02/23.
sqlDB, _ := db.DB()
sqlDB.SetMaxIdleConns(1)
sqlDB.SetMaxOpenConns(1)
sqlDB.SetConnMaxIdleTime(time.Hour)

case Postgres:
db, err = gorm.Open(postgres.Open(h.dbString), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
Expand Down

0 comments on commit 686e45c

Please sign in to comment.