Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/db/gorm.go

43 lignes
1,2 Kio
Go
Brut Vue normale Historique

package db
import (
"github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/model"
2017-05-06 20:27:26 +02:00
"github.com/ewhal/nyaa/util/log"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
2017-05-06 17:07:50 +02:00
_ "github.com/jinzhu/gorm/dialects/postgres"
// _ "github.com/go-sql-driver/mysql"
)
2017-05-06 20:27:26 +02:00
var ORM *gorm.DB
// GormInit init gorm ORM.
2017-05-06 20:27:26 +02:00
func GormInit(conf *config.Config) (*gorm.DB, error) {
db, err := gorm.Open(conf.DBType, conf.DBParams)
// db, err := gorm.Open("mysql", config.MysqlDSL())
//db, err := gorm.Open("sqlite3", "/tmp/gorm.db")
// Get database connection handle [*sql.DB](http://golang.org/pkg/database/sql/#DB)
db.DB()
// Then you could invoke `*sql.DB`'s functions with it
db.DB().Ping()
db.DB().SetMaxIdleConns(10)
db.DB().SetMaxOpenConns(100)
// Disable table name's pluralization
// db.SingularTable(true)
if config.Environment == "DEVELOPMENT" {
db.LogMode(true)
// db.DropTable(&model.User{}, "UserFollower")
2017-05-08 02:18:48 +02:00
db.AutoMigrate(&model.Torrents{}, &model.UsersFollowers{}, &model.User{}, &model.Role{}, &model.Language{})
// db.AutoMigrate(&model.Comment{})
// db.Model(&model.User{}).AddIndex("idx_user_token", "token")
}
log.CheckError(err)
return db, err
}