service/db: switch sqlite driver by target architecture

Use build-tag specific driver selection for sqlite:

- keep modernc sqlite on non-mipsle targets

- use go-sqlite3 for mipsle builds with cgo

- return explicit error when sqlite driver is unavailable

Also apply the same driver selection in migration db initialization.
This commit is contained in:
Markson Hon committed 2026-05-04 19:27:15 +08:00
1 parent 8ccd4bb85a
commit afd73bbbc8
6 files changed
+36 -4

No files matched your search

+5 -2
View File
@@ -10,7 +10,6 @@ import (
"github.com/v2rayA/v2rayA/conf"
"github.com/v2rayA/v2rayA/pkg/util/log"
"go.etcd.io/bbolt"
_ "modernc.org/sqlite"
)
// MigrateFromBoltDB migrates data from BoltDB to SQLite.
@@ -118,7 +117,11 @@ func createSQLiteDB(dbPath string) (*sql.DB, error) {
}
f.Close()
db, err := sql.Open("sqlite", dbPath)
if err := validateSQLiteDriver(); err != nil {
return nil, err
}
db, err := sql.Open(sqliteDriverName, dbPath)
if err != nil {
return nil, fmt.Errorf("failed to open SQLite database: %w", err)
}
+11 -2
View File
@@ -10,7 +10,6 @@ import (
"github.com/v2rayA/v2rayA/conf"
"github.com/v2rayA/v2rayA/pkg/util/log"
_ "modernc.org/sqlite"
)
var (
@@ -25,6 +24,13 @@ var (
// indicating that migration to SQLite is required before normal operation.
var ErrNeedMigration = errors.New("bolt.db exists, migration required")
func validateSQLiteDriver() error {
if sqliteDriverName == "" {
return errors.New("mipsle build requires CGO_ENABLED=1 to enable sqlite driver")
}
return nil
}
// SetReadOnly sets the database to read-only mode.
// In read-only mode, a temporary copy of the database is created.
func SetReadOnly() {
@@ -59,7 +65,10 @@ func initDB() {
}
var err error
sqlDB, err = sql.Open("sqlite", dbPath)
if err = validateSQLiteDriver(); err != nil {
log.Fatal("SQLite driver is unavailable: %v", err)
}
sqlDB, err = sql.Open(sqliteDriverName, dbPath)
if err != nil {
log.Fatal("sql.Open: %v", err)
}
+7
View File
@@ -0,0 +1,7 @@
//go:build !mipsle
package db
import _ "modernc.org/sqlite"
const sqliteDriverName = "sqlite"
+7
View File
@@ -0,0 +1,7 @@
//go:build mipsle && cgo
package db
import _ "github.com/mattn/go-sqlite3"
const sqliteDriverName = "sqlite3"
+5
View File
@@ -0,0 +1,5 @@
//go:build mipsle && !cgo
package db
const sqliteDriverName = ""
+1
View File
@@ -13,6 +13,7 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/json-iterator/go v1.1.12
github.com/matoous/go-nanoid v1.5.0
github.com/mattn/go-sqlite3 v1.14.22
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/muhammadmuzzammil1998/jsonc v0.0.0-20201229145248-615b0916ca38
github.com/pkg/errors v0.9.1