Finish the sqlite feature.
This commit is contained in:
@ -20,6 +20,8 @@ type TankConfig struct {
|
||||
matterPath string
|
||||
//mysql url.
|
||||
mysqlUrl string
|
||||
//sqlite file path
|
||||
sqliteFolder string
|
||||
//configs in tank.json
|
||||
item *ConfigItem
|
||||
}
|
||||
@ -30,7 +32,10 @@ type ConfigItem struct {
|
||||
ServerPort int
|
||||
//file storage location. eg./var/www/matter
|
||||
MatterPath string
|
||||
//mysql configurations.
|
||||
//********db configurations.********
|
||||
//default value is "mysql"
|
||||
DbType string
|
||||
//********mysql configurations..********
|
||||
//mysql port
|
||||
MysqlPort int
|
||||
//mysql host
|
||||
@ -43,6 +48,9 @@ type ConfigItem struct {
|
||||
MysqlPassword string
|
||||
//mysql charset
|
||||
MysqlCharset string
|
||||
//********sqlite configurations..********
|
||||
//default value is matter/
|
||||
SqliteFolder string
|
||||
}
|
||||
|
||||
//validate whether the config file is ok
|
||||
@ -53,33 +61,39 @@ func (this *ConfigItem) validate() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
if this.MysqlUsername == "" {
|
||||
core.LOGGER.Error("MysqlUsername is not configured")
|
||||
return false
|
||||
}
|
||||
if this.DbType == "sqlite" {
|
||||
|
||||
if this.MysqlPassword == "" {
|
||||
core.LOGGER.Error("MysqlPassword is not configured")
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
|
||||
if this.MysqlHost == "" {
|
||||
core.LOGGER.Error("MysqlHost is not configured")
|
||||
return false
|
||||
}
|
||||
if this.MysqlUsername == "" {
|
||||
core.LOGGER.Error("MysqlUsername is not configured")
|
||||
return false
|
||||
}
|
||||
|
||||
if this.MysqlPort == 0 {
|
||||
core.LOGGER.Error("MysqlPort is not configured")
|
||||
return false
|
||||
}
|
||||
if this.MysqlPassword == "" {
|
||||
core.LOGGER.Error("MysqlPassword is not configured")
|
||||
return false
|
||||
}
|
||||
|
||||
if this.MysqlSchema == "" {
|
||||
core.LOGGER.Error("MysqlSchema is not configured")
|
||||
return false
|
||||
}
|
||||
if this.MysqlCharset == "" {
|
||||
core.LOGGER.Error("MysqlCharset is not configured")
|
||||
return false
|
||||
if this.MysqlHost == "" {
|
||||
core.LOGGER.Error("MysqlHost is not configured")
|
||||
return false
|
||||
}
|
||||
|
||||
if this.MysqlPort == 0 {
|
||||
core.LOGGER.Error("MysqlPort is not configured")
|
||||
return false
|
||||
}
|
||||
|
||||
if this.MysqlSchema == "" {
|
||||
core.LOGGER.Error("MysqlSchema is not configured")
|
||||
return false
|
||||
}
|
||||
|
||||
if this.MysqlCharset == "" {
|
||||
core.LOGGER.Error("MysqlCharset is not configured")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
@ -169,11 +183,30 @@ func (this *TankConfig) ServerPort() int {
|
||||
return this.serverPort
|
||||
}
|
||||
|
||||
//get the db type
|
||||
func (this *TankConfig) DbType() string {
|
||||
return this.item.DbType
|
||||
}
|
||||
|
||||
//mysql url
|
||||
func (this *TankConfig) MysqlUrl() string {
|
||||
return this.mysqlUrl
|
||||
}
|
||||
|
||||
//get the sqlite path
|
||||
func (this *TankConfig) SqliteFolder() string {
|
||||
if this.sqliteFolder == "" {
|
||||
//use default file location.
|
||||
if this.item == nil || this.item.SqliteFolder == "" {
|
||||
this.sqliteFolder = util.GetHomePath() + "/matter"
|
||||
} else {
|
||||
this.sqliteFolder = util.UniformPath(this.item.SqliteFolder)
|
||||
}
|
||||
}
|
||||
|
||||
return this.sqliteFolder
|
||||
}
|
||||
|
||||
//matter path
|
||||
func (this *TankConfig) MatterPath() string {
|
||||
return this.matterPath
|
||||
@ -187,10 +220,11 @@ func (this *TankConfig) NamingStrategy() schema.NamingStrategy {
|
||||
}
|
||||
}
|
||||
|
||||
//Finish the installation. Write config to tank.json
|
||||
func (this *TankConfig) FinishInstall(mysqlPort int, mysqlHost string, mysqlSchema string, mysqlUsername string, mysqlPassword string, mysqlCharset string) {
|
||||
//TODO: Finish the installation. Write config to tank.json. add sqlite support.
|
||||
func (this *TankConfig) FinishInstall(dbType string, mysqlPort int, mysqlHost string, mysqlSchema string, mysqlUsername string, mysqlPassword string, mysqlCharset string) {
|
||||
|
||||
var configItem = &ConfigItem{
|
||||
DbType: dbType,
|
||||
//server port
|
||||
ServerPort: core.CONFIG.ServerPort(),
|
||||
//file storage location. eg./var/www/matter
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"github.com/eyebluecn/tank/code/core"
|
||||
"github.com/eyebluecn/tank/code/rest"
|
||||
"github.com/eyebluecn/tank/code/tool/cache"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
@ -80,19 +81,36 @@ func (this *TankContext) OpenDb() {
|
||||
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
|
||||
logger.Config{
|
||||
SlowThreshold: time.Second, // slow SQL 1s
|
||||
LogLevel: logger.Silent, // log level
|
||||
LogLevel: logger.Silent, // log level. open when debug.
|
||||
IgnoreRecordNotFoundError: true, // ignore ErrRecordNotFound
|
||||
Colorful: false, // colorful print
|
||||
},
|
||||
)
|
||||
|
||||
//table name strategy.
|
||||
namingStrategy := core.CONFIG.NamingStrategy()
|
||||
|
||||
var err error = nil
|
||||
this.db, err = gorm.Open(mysql.Open(core.CONFIG.MysqlUrl()), &gorm.Config{Logger: dbLogger, NamingStrategy: namingStrategy})
|
||||
if core.CONFIG.DbType() == "sqlite" {
|
||||
|
||||
if err != nil {
|
||||
core.LOGGER.Panic("failed to connect mysql database")
|
||||
var err error = nil
|
||||
this.db, err = gorm.Open(sqlite.Open(core.CONFIG.SqliteFolder()+"/tank.sqlite"), &gorm.Config{Logger: dbLogger, NamingStrategy: namingStrategy})
|
||||
|
||||
if err != nil {
|
||||
core.LOGGER.Panic("failed to connect mysql database")
|
||||
}
|
||||
|
||||
//sqlite lock issue. https://gist.github.com/mrnugget/0eda3b2b53a70fa4a894
|
||||
phyDb, err := this.db.DB()
|
||||
phyDb.SetMaxOpenConns(1)
|
||||
|
||||
} else {
|
||||
|
||||
var err error = nil
|
||||
this.db, err = gorm.Open(mysql.Open(core.CONFIG.MysqlUrl()), &gorm.Config{Logger: dbLogger, NamingStrategy: namingStrategy})
|
||||
|
||||
if err != nil {
|
||||
core.LOGGER.Panic("failed to connect mysql database")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user