1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-08-02 20:15:26 +02:00

Refactoring of database init code

This commit is contained in:
Harvey Kandola 2018-09-12 20:03:34 +01:00
parent d7fea2125f
commit 3bccd6a537
7 changed files with 155 additions and 134 deletions

View file

@ -37,6 +37,15 @@ import (
// StoreMySQL creates MySQL provider
func StoreMySQL(r *env.Runtime, s *domain.Store) {
// Required connection string parameters and defaults.
r.Storage.Params = map[string]string{
"charset": "utf8mb4",
"parseTime": "True",
"maxAllowedPacket": "104857600", // 4194304 // 16777216 = 16MB // 104857600 = 100MB
}
r.Storage.Example = "database connection string format is 'username:password@tcp(host:3306)/database'"
s.Account = account.Scope{Runtime: r}
s.Activity = activity.Scope{Runtime: r}
s.Attachment = attachment.Scope{Runtime: r}

View file

@ -13,7 +13,6 @@
package boot
import (
"strings"
"time"
"github.com/documize/community/core/database"
@ -47,25 +46,35 @@ func InitRuntime(r *env.Runtime, s *domain.Store) bool {
}
}
// Prepare DB
db, err := sqlx.Open("mysql", stdConn(r.Flags.DBConn))
// Work out required storage provider set it up.
switch r.Flags.DBType {
case "mysql":
r.Storage = env.StoreProvider{Type: env.StoreTypeMySQL, DriverName: "mysql"}
StoreMySQL(r, s)
case "mariadb":
r.Storage = env.StoreProvider{Type: env.StoreTypeMariaDB, DriverName: "mysql"}
StoreMySQL(r, s)
case "percona":
r.Storage = env.StoreProvider{Type: env.StoreTypePercona, DriverName: "mysql"}
StoreMySQL(r, s)
}
// Open connection to database
db, err := sqlx.Open(r.Storage.DriverName, r.Storage.ConnectionString(r.Flags.DBConn))
if err != nil {
r.Log.Error("unable to setup database", err)
}
r.Db = db
r.Db.SetMaxIdleConns(30)
r.Db.SetMaxOpenConns(100)
r.Db.SetConnMaxLifetime(time.Second * 14400)
err = r.Db.Ping()
if err != nil {
r.Log.Error("unable to connect to database, connection string should be of the form: '"+
"username:password@tcp(host:3306)/database'", err)
r.Log.Error("unable to connect to database - "+r.Storage.Example, err)
return false
}
// go into setup mode if required
// Go into setup mode if required.
if r.Flags.SiteMode != env.SiteModeOffline {
if database.Check(r) {
if err := database.Migrate(r, true /* the config table exists */); err != nil {
@ -75,46 +84,8 @@ func InitRuntime(r *env.Runtime, s *domain.Store) bool {
}
}
// setup store based upon database type
AttachStore(r, s)
return true
}
var stdParams = map[string]string{
"charset": "utf8mb4",
"parseTime": "True",
"maxAllowedPacket": "104857600", // 4194304 // 16777216 = 16MB // 104857600 = 100MB
}
func stdConn(cs string) string {
queryBits := strings.Split(cs, "?")
ret := queryBits[0] + "?"
retFirst := true
if len(queryBits) == 2 {
paramBits := strings.Split(queryBits[1], "&")
for _, pb := range paramBits {
found := false
if assignBits := strings.Split(pb, "="); len(assignBits) == 2 {
_, found = stdParams[strings.TrimSpace(assignBits[0])]
}
if !found { // if we can't work out what it is, put it through
if retFirst {
retFirst = false
} else {
ret += "&"
}
ret += pb
}
}
}
for k, v := range stdParams {
if retFirst {
retFirst = false
} else {
ret += "&"
}
ret += k + "=" + v
}
return ret
}
// Clever way to detect database type:
// https://github.com/golang-sql/sqlexp/blob/c2488a8be21d20d31abf0d05c2735efd2d09afe4/quoter.go#L46

View file

@ -1,32 +0,0 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
// Package boot prepares runtime environment.
package boot
import (
"github.com/documize/community/core/env"
"github.com/documize/community/domain"
)
// AttachStore selects database persistence layer
func AttachStore(r *env.Runtime, s *domain.Store) {
switch r.DbVariant {
case env.DbVariantMySQL, env.DBVariantPercona, env.DBVariantMariaDB:
StoreMySQL(r, s)
case env.DBVariantMSSQL:
// todo
case env.DBVariantPostgreSQL:
// todo
}
}
// https://github.com/golang-sql/sqlexp/blob/c2488a8be21d20d31abf0d05c2735efd2d09afe4/quoter.go#L46