Skip to content

Commit

Permalink
kv/sqlite3: workaround for modernc/sqlite3 not building on illumos
Browse files Browse the repository at this point in the history
  • Loading branch information
zllovesuki committed Feb 2, 2025
1 parent 20ea126 commit 49bb64f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
14 changes: 9 additions & 5 deletions kv/sqlite3/kv.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package sqlite3

import (
"fmt"
"os"
"path/filepath"
"runtime"
"time"

"go.miragespace.co/specter/spec/chord"

"github.com/glebarez/sqlite"
"go.uber.org/zap"
"gorm.io/gorm"
"moul.io/zapgorm2"
Expand Down Expand Up @@ -48,10 +46,16 @@ func New(cfg Config) (*SqliteKV, error) {
if err := os.MkdirAll(dbDir, 0750); err != nil {
return nil, err
}
dsn := fmt.Sprintf("file:%s?_pragma=journal_mode(WAL)&_pragma=foreign_keys(1)&_pragma=busy_timeout(5000)&_pragma=synchronous(1)&_pragma=page_size(4096)&_txlock=immediate", filepath.Join(dbDir, "db"))
dbPath := filepath.Join(dbDir, "db")

readDb := sqlite.Open(dsn)
writeDb := sqlite.Open(dsn)
readDb, err := openSQLite(dbPath)
if err != nil {
return nil, err
}
writeDb, err := openSQLite(dbPath)
if err != nil {
return nil, err
}

logger := zapgorm2.New(cfg.Logger)
logger.IgnoreRecordNotFoundError = true
Expand Down
14 changes: 14 additions & 0 deletions kv/sqlite3/sqlite_illumos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build illumos

package sqlite3

import (
"errors"

"gorm.io/gorm"
)

// FIXME: Introduce support for SQLite3 via mattn/go-sqlite3
func openSQLite(_ string) (gorm.Dialector, error) {
return nil, errors.ErrUnsupported
}
16 changes: 16 additions & 0 deletions kv/sqlite3/sqlite_purego.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build !illumos

package sqlite3

import (
"fmt"

"github.com/glebarez/sqlite"
"gorm.io/gorm"
)

func openSQLite(dbpath string) (gorm.Dialector, error) {
dsn := fmt.Sprintf("file:%s?_pragma=journal_mode(WAL)&_pragma=foreign_keys(1)&_pragma=busy_timeout(5000)&_pragma=synchronous(1)&_pragma=page_size(4096)&_txlock=immediate", dbpath)
db := sqlite.Open(dsn)
return db, nil
}

0 comments on commit 49bb64f

Please sign in to comment.