diff options
author | Joe Chen <jc@unknwon.io> | 2022-06-03 10:03:48 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-03 10:03:48 +0800 |
commit | ccdcb1c45bdc3b85d5e6dcc7b96e9670281a83ac (patch) | |
tree | 70640681a9ed12e0aed65be7597eac16d90d8fb6 /internal/db | |
parent | 5f34265db6548e3be72b865e41f227137b18474c (diff) |
db: run tests with pure-Go SQLite (#6998)
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/db.go | 5 | ||||
-rw-r--r-- | internal/db/main_test.go | 14 |
2 files changed, 18 insertions, 1 deletions
diff --git a/internal/db/db.go b/internal/db/db.go index 987d90df..44077843 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -81,7 +81,7 @@ func newDSN(opts conf.DatabaseOpts) (dsn string, err error) { dsn = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, opts.Name, opts.User, opts.Password) - case "sqlite3": + case "sqlite3", "sqlite": dsn = "file:" + opts.Path + "?cache=shared&mode=rwc" default: @@ -124,6 +124,9 @@ func openDB(opts conf.DatabaseOpts, cfg *gorm.Config) (*gorm.DB, error) { dialector = sqlserver.Open(dsn) case "sqlite3": dialector = sqlite.Open(dsn) + case "sqlite": + dialector = sqlite.Open(dsn) + dialector.(*sqlite.Dialector).DriverName = "sqlite" default: panic("unreachable") } diff --git a/internal/db/main_test.go b/internal/db/main_test.go index cedd7cfc..d5598c58 100644 --- a/internal/db/main_test.go +++ b/internal/db/main_test.go @@ -17,6 +17,7 @@ import ( "gorm.io/gorm" "gorm.io/gorm/logger" "gorm.io/gorm/schema" + _ "modernc.org/sqlite" log "unknwon.dev/clog/v2" "gogs.io/gogs/internal/conf" @@ -127,6 +128,19 @@ func initTestDB(t *testing.T, suite string, tables ...interface{}) *gorm.DB { db.Exec(fmt.Sprintf(`DROP DATABASE %q`, dbName)) _ = sqlDB.Close() } + case "sqlite": + dbName = filepath.Join(os.TempDir(), fmt.Sprintf("gogs-%s-%d.db", suite, time.Now().Unix())) + dbOpts = conf.DatabaseOpts{ + Type: "sqlite", + Path: dbName, + } + cleanup = func(db *gorm.DB) { + sqlDB, err := db.DB() + if err == nil { + _ = sqlDB.Close() + } + _ = os.Remove(dbName) + } default: dbName = filepath.Join(os.TempDir(), fmt.Sprintf("gogs-%s-%d.db", suite, time.Now().Unix())) dbOpts = conf.DatabaseOpts{ |