Add database logmode to the config
This allows users to change the default logging verbosity (errors) to either *detailed* (prints SQL statements) or *silent*. Also added support for using a custom logger function. - Fixed the gorm unit test that checks the automigrations They will actually fail if any errors were logged now. - Added a postgres unit test Currently disabled because it would need a running local postgres db and a change to the .travis.yml file to work inside the CI build.
Cette révision appartient à :
Parent
61ba31b337
révision
b12e812b36
5 fichiers modifiés avec 115 ajouts et 7 suppressions
|
@ -7,9 +7,9 @@ before_install:
|
||||||
script:
|
script:
|
||||||
# Downloads deps automatically. No need to add manually.
|
# Downloads deps automatically. No need to add manually.
|
||||||
- go list -f '{{.Deps}}' | tr "[" " " | tr "]" " " | xargs go list -e -f '{{if not .Standard}}{{.ImportPath}}{{end}}' | grep -v 'github.com/ewhal/nyaa' | xargs go get -v
|
- go list -f '{{.Deps}}' | tr "[" " " | tr "]" " " | xargs go list -e -f '{{if not .Standard}}{{.ImportPath}}{{end}}' | grep -v 'github.com/ewhal/nyaa' | xargs go get -v
|
||||||
- go get
|
- go get
|
||||||
- go build
|
- go build
|
||||||
- go vet
|
- go vet
|
||||||
- go test -v ./...
|
- go test -v ./...
|
||||||
before_deploy:
|
before_deploy:
|
||||||
- ./package.sh
|
- ./package.sh
|
||||||
|
|
|
@ -22,6 +22,7 @@ type Config struct {
|
||||||
// DBParams will be directly passed to Gorm, and its internal
|
// DBParams will be directly passed to Gorm, and its internal
|
||||||
// structure depends on the dialect for each db type
|
// structure depends on the dialect for each db type
|
||||||
DBParams string `json:"db_params"`
|
DBParams string `json:"db_params"`
|
||||||
|
DBLogMode string `json:"db_logmode"`
|
||||||
// tracker scraper config (required)
|
// tracker scraper config (required)
|
||||||
Scrape ScraperConfig `json:"scraper"`
|
Scrape ScraperConfig `json:"scraper"`
|
||||||
// cache config
|
// cache config
|
||||||
|
@ -32,7 +33,7 @@ type Config struct {
|
||||||
I2P *I2PConfig `json:"i2p"`
|
I2P *I2PConfig `json:"i2p"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var Defaults = Config{"localhost", 9999, "sqlite3", "./nyaa.db?cache_size=50", DefaultScraperConfig, DefaultCacheConfig, DefaultSearchConfig, nil}
|
var Defaults = Config{"localhost", 9999, "sqlite3", "./nyaa.db?cache_size=50", "default", DefaultScraperConfig, DefaultCacheConfig, DefaultSearchConfig, nil}
|
||||||
|
|
||||||
var allowedDatabaseTypes = map[string]bool{
|
var allowedDatabaseTypes = map[string]bool{
|
||||||
"sqlite3": true,
|
"sqlite3": true,
|
||||||
|
@ -41,12 +42,19 @@ var allowedDatabaseTypes = map[string]bool{
|
||||||
"mssql": true,
|
"mssql": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var allowedDBLogModes = map[string]bool{
|
||||||
|
"default": true, // errors only
|
||||||
|
"detailed": true,
|
||||||
|
"silent": true,
|
||||||
|
}
|
||||||
|
|
||||||
func New() *Config {
|
func New() *Config {
|
||||||
var config Config
|
var config Config
|
||||||
config.Host = Defaults.Host
|
config.Host = Defaults.Host
|
||||||
config.Port = Defaults.Port
|
config.Port = Defaults.Port
|
||||||
config.DBType = Defaults.DBType
|
config.DBType = Defaults.DBType
|
||||||
config.DBParams = Defaults.DBParams
|
config.DBParams = Defaults.DBParams
|
||||||
|
config.DBLogMode = Defaults.DBLogMode
|
||||||
config.Scrape = Defaults.Scrape
|
config.Scrape = Defaults.Scrape
|
||||||
config.Cache = Defaults.Cache
|
config.Cache = Defaults.Cache
|
||||||
return &config
|
return &config
|
||||||
|
@ -60,6 +68,7 @@ func (config *Config) BindFlags() func() error {
|
||||||
host := flag.String("host", Defaults.Host, "binding address of the server")
|
host := flag.String("host", Defaults.Host, "binding address of the server")
|
||||||
port := flag.Int("port", Defaults.Port, "port of the server")
|
port := flag.Int("port", Defaults.Port, "port of the server")
|
||||||
dbParams := flag.String("dbparams", Defaults.DBParams, "parameters to open the database (see Gorm's doc)")
|
dbParams := flag.String("dbparams", Defaults.DBParams, "parameters to open the database (see Gorm's doc)")
|
||||||
|
dbLogMode := flag.String("dblogmode", Defaults.DBLogMode, "database log verbosity (errors only by default)")
|
||||||
|
|
||||||
return func() error {
|
return func() error {
|
||||||
// You can override fields in the config file with flags.
|
// You can override fields in the config file with flags.
|
||||||
|
@ -70,6 +79,10 @@ func (config *Config) BindFlags() func() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
err = config.SetDBLogMode(*dbLogMode)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
err = config.HandleConfFileFlag(*confFile)
|
err = config.HandleConfFileFlag(*confFile)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -98,6 +111,14 @@ func (config *Config) SetDBType(db_type string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (config *Config) SetDBLogMode(db_logmode string) error {
|
||||||
|
if !allowedDBLogModes[db_logmode] {
|
||||||
|
return fmt.Errorf("unknown database log mode '%s'", db_logmode)
|
||||||
|
}
|
||||||
|
config.DBLogMode = db_logmode
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (config *Config) Read(input io.Reader) error {
|
func (config *Config) Read(input io.Reader) error {
|
||||||
return json.NewDecoder(input).Decode(config)
|
return json.NewDecoder(input).Decode(config)
|
||||||
}
|
}
|
||||||
|
|
30
db/gorm.go
30
db/gorm.go
|
@ -9,12 +9,20 @@ import (
|
||||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Logger interface {
|
||||||
|
Print(v ...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// use the default gorm logger that prints to stdout
|
||||||
|
var DefaultLogger Logger = nil
|
||||||
|
|
||||||
var ORM *gorm.DB
|
var ORM *gorm.DB
|
||||||
|
|
||||||
var IsSqlite bool
|
var IsSqlite bool
|
||||||
|
|
||||||
// GormInit init gorm ORM.
|
// GormInit init gorm ORM.
|
||||||
func GormInit(conf *config.Config) (*gorm.DB, error) {
|
func GormInit(conf *config.Config, logger Logger) (*gorm.DB, error) {
|
||||||
|
|
||||||
db, openErr := gorm.Open(conf.DBType, conf.DBParams)
|
db, openErr := gorm.Open(conf.DBType, conf.DBParams)
|
||||||
if openErr != nil {
|
if openErr != nil {
|
||||||
log.CheckError(openErr)
|
log.CheckError(openErr)
|
||||||
|
@ -35,9 +43,29 @@ func GormInit(conf *config.Config) (*gorm.DB, error) {
|
||||||
db.LogMode(true)
|
db.LogMode(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch conf.DBLogMode {
|
||||||
|
case "detailed":
|
||||||
|
db.LogMode(true)
|
||||||
|
case "silent":
|
||||||
|
db.LogMode(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
if logger != nil {
|
||||||
|
db.SetLogger(logger)
|
||||||
|
}
|
||||||
|
|
||||||
db.AutoMigrate(&model.User{}, &model.UserFollows{}, &model.UserUploadsOld{})
|
db.AutoMigrate(&model.User{}, &model.UserFollows{}, &model.UserUploadsOld{})
|
||||||
|
if db.Error != nil {
|
||||||
|
return db, db.Error
|
||||||
|
}
|
||||||
db.AutoMigrate(&model.Torrent{}, &model.TorrentReport{})
|
db.AutoMigrate(&model.Torrent{}, &model.TorrentReport{})
|
||||||
|
if db.Error != nil {
|
||||||
|
return db, db.Error
|
||||||
|
}
|
||||||
db.AutoMigrate(&model.Comment{}, &model.OldComment{})
|
db.AutoMigrate(&model.Comment{}, &model.OldComment{})
|
||||||
|
if db.Error != nil {
|
||||||
|
return db, db.Error
|
||||||
|
}
|
||||||
|
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,81 @@
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/azhao12345/gorm"
|
||||||
"github.com/ewhal/nyaa/config"
|
"github.com/ewhal/nyaa/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGormInit(t *testing.T) {
|
type errorLogger struct {
|
||||||
|
t *testing.T
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *errorLogger) Print(values ...interface{}) {
|
||||||
|
if len(values) > 1 {
|
||||||
|
message := gorm.LogFormatter(values...)
|
||||||
|
level := values[0]
|
||||||
|
if level == "log" {
|
||||||
|
logger.t.Error(message...)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(message...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGormInitSqlite(t *testing.T) {
|
||||||
conf := config.New()
|
conf := config.New()
|
||||||
conf.DBType = "sqlite3"
|
conf.DBType = "sqlite3"
|
||||||
conf.DBParams = ":memory:?cache=shared&mode=memory"
|
conf.DBParams = ":memory:?cache=shared&mode=memory"
|
||||||
|
conf.DBLogMode = "detailed"
|
||||||
|
|
||||||
db, err := GormInit(conf)
|
db, err := GormInit(conf, &errorLogger{t})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to initialize database: %v", err)
|
t.Errorf("failed to initialize database: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if db == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to close database: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This test requires a running postgres instance. To run it in CI build add these settings in the .travis.yml
|
||||||
|
// services:
|
||||||
|
// - postgresql
|
||||||
|
// before_script:
|
||||||
|
// - psql -c "CREATE DATABASE nyaapantsu;" -U postgres
|
||||||
|
// - psql -c "CREATE USER nyaapantsu WITH PASSWORD 'nyaapantsu';" -U postgres
|
||||||
|
//
|
||||||
|
// Then enable the test by setting this variable to "true" via ldflags:
|
||||||
|
// go test ./... -v -ldflags="-X github.com/ewhal/nyaa/db.testPostgres=true"
|
||||||
|
var testPostgres = "false"
|
||||||
|
|
||||||
|
func TestGormInitPostgres(t *testing.T) {
|
||||||
|
if testPostgres != "true" {
|
||||||
|
t.Skip("skip", testPostgres)
|
||||||
|
}
|
||||||
|
|
||||||
|
conf := config.New()
|
||||||
|
conf.DBType = "postgres"
|
||||||
|
conf.DBParams = "host=localhost user=nyaapantsu dbname=nyaapantsu sslmode=disable password=nyaapantsu"
|
||||||
|
conf.DBLogMode = "detailed"
|
||||||
|
|
||||||
|
db, err := GormInit(conf, &errorLogger{t})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to initialize database: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if db == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
err = db.Close()
|
err = db.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to close database: %v", err)
|
t.Errorf("failed to close database: %v", err)
|
||||||
|
|
2
main.go
2
main.go
|
@ -118,7 +118,7 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.CheckError(err)
|
log.CheckError(err)
|
||||||
}
|
}
|
||||||
db.ORM, err = db.GormInit(conf)
|
db.ORM, err = db.GormInit(conf, db.DefaultLogger)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err.Error())
|
log.Fatal(err.Error())
|
||||||
}
|
}
|
||||||
|
|
Référencer dans un nouveau ticket