mirror of
https://github.com/documize/community.git
synced 2025-08-05 05:25:27 +02:00
Refactoring of database init code
This commit is contained in:
parent
d7fea2125f
commit
3bccd6a537
7 changed files with 155 additions and 134 deletions
|
@ -46,23 +46,18 @@ func Check(runtime *env.Runtime) bool {
|
|||
if rows.Next() {
|
||||
err = rows.Scan(&version, &dbComment, &charset, &collation)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
err = rows.Err() // get any error encountered during iteration
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
runtime.Log.Error("no MySQL configuration returned", err)
|
||||
web.SiteInfo.Issue = "no MySQL configuration return issue: " + err.Error()
|
||||
runtime.Flags.SiteMode = env.SiteModeBadDB
|
||||
return false
|
||||
}
|
||||
// runtime.DbVariant = GetSQLVariant(runtime.Flags.DBType, dbComment)
|
||||
|
||||
// Get SQL variant as this affects minimum version checking logic.
|
||||
// MySQL and Percona share same version scheme (e..g 5.7.10).
|
||||
// MariaDB starts at 10.2.x
|
||||
runtime.DbVariant = GetSQLVariant(runtime.Flags.DBType, dbComment)
|
||||
runtime.Log.Info(fmt.Sprintf("Database checks: SQL variant %v", runtime.DbVariant))
|
||||
runtime.Log.Info(fmt.Sprintf("Database checks: SQL variant %v", runtime.Storage.Type))
|
||||
runtime.Log.Info("Database checks: SQL version " + version)
|
||||
|
||||
verNums, err := GetSQLVersion(version)
|
||||
|
@ -72,7 +67,7 @@ func Check(runtime *env.Runtime) bool {
|
|||
|
||||
// Check minimum MySQL version as we need JSON column type.
|
||||
verInts := []int{5, 7, 10} // Minimum MySQL version
|
||||
if runtime.DbVariant == env.DBVariantMariaDB {
|
||||
if runtime.Storage.Type == env.StoreTypeMariaDB {
|
||||
verInts = []int{10, 3, 0} // Minimum MariaDB version
|
||||
}
|
||||
|
||||
|
@ -146,31 +141,31 @@ func Check(runtime *env.Runtime) bool {
|
|||
}
|
||||
|
||||
// GetSQLVariant uses database value form @@version_comment to deduce MySQL variant.
|
||||
func GetSQLVariant(dbType, vc string) env.DbVariant {
|
||||
vc = strings.ToLower(vc)
|
||||
dbType = strings.ToLower(dbType)
|
||||
// func GetSQLVariant(dbType, vc string) env.DbVariant {
|
||||
// vc = strings.ToLower(vc)
|
||||
// dbType = strings.ToLower(dbType)
|
||||
|
||||
// determine type from database
|
||||
if strings.Contains(vc, "mariadb") {
|
||||
return env.DBVariantMariaDB
|
||||
} else if strings.Contains(vc, "percona") {
|
||||
return env.DBVariantPercona
|
||||
} else if strings.Contains(vc, "mysql") {
|
||||
return env.DbVariantMySQL
|
||||
}
|
||||
// // determine type from database
|
||||
// if strings.Contains(vc, "mariadb") {
|
||||
// return env.DBVariantMariaDB
|
||||
// } else if strings.Contains(vc, "percona") {
|
||||
// return env.DBVariantPercona
|
||||
// } else if strings.Contains(vc, "mysql") {
|
||||
// return env.DbVariantMySQL
|
||||
// }
|
||||
|
||||
// now determine type from command line switch
|
||||
if strings.Contains(dbType, "mariadb") {
|
||||
return env.DBVariantMariaDB
|
||||
} else if strings.Contains(dbType, "percona") {
|
||||
return env.DBVariantPercona
|
||||
} else if strings.Contains(dbType, "mysql") {
|
||||
return env.DbVariantMySQL
|
||||
}
|
||||
// // now determine type from command line switch
|
||||
// if strings.Contains(dbType, "mariadb") {
|
||||
// return env.DBVariantMariaDB
|
||||
// } else if strings.Contains(dbType, "percona") {
|
||||
// return env.DBVariantPercona
|
||||
// } else if strings.Contains(dbType, "mysql") {
|
||||
// return env.DbVariantMySQL
|
||||
// }
|
||||
|
||||
// horrid default could cause app to crash
|
||||
return env.DbVariantMySQL
|
||||
}
|
||||
// // horrid default could cause app to crash
|
||||
// return env.DbVariantMySQL
|
||||
// }
|
||||
|
||||
// GetSQLVersion returns SQL version as major,minor,patch numerics.
|
||||
func GetSQLVersion(v string) (ints []int, err error) {
|
||||
|
|
|
@ -75,7 +75,7 @@ func (m migrationsT) migrate(runtime *env.Runtime, tx *sqlx.Tx) error {
|
|||
return err
|
||||
}
|
||||
|
||||
err = processSQLfile(tx, runtime.DbVariant, buf)
|
||||
err = processSQLfile(tx, runtime.Storage.Type, buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -244,12 +244,12 @@ func Migrate(runtime *env.Runtime, ConfigTableExists bool) error {
|
|||
return migrateEnd(runtime, tx, nil, amLeader)
|
||||
}
|
||||
|
||||
func processSQLfile(tx *sqlx.Tx, v env.DbVariant, buf []byte) error {
|
||||
func processSQLfile(tx *sqlx.Tx, v env.StoreType, buf []byte) error {
|
||||
stmts := getStatements(buf)
|
||||
|
||||
for _, stmt := range stmts {
|
||||
// MariaDB has no specific JSON column type (but has JSON queries)
|
||||
if v == env.DBVariantMariaDB {
|
||||
if v == env.StoreTypeMariaDB {
|
||||
stmt = strings.Replace(stmt, "` JSON", "` TEXT", -1)
|
||||
}
|
||||
|
||||
|
|
6
core/env/flags.go
vendored
6
core/env/flags.go
vendored
|
@ -80,8 +80,8 @@ func ParseFlags() (f Flags) {
|
|||
register(&port, "port", false, "http/https port number")
|
||||
register(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS")
|
||||
register(&siteMode, "offline", false, "set to '1' for OFFLINE mode")
|
||||
register(&dbType, "dbtype", false, "set to database type mysql|percona|mariadb")
|
||||
register(&dbConn, "db", true, `'username:password@protocol(hostname:port)/databasename" for example "fred:bloggs@tcp(localhost:3306)/documize"`)
|
||||
register(&dbType, "dbtype", true, "specify the database provider: mysql|percona|mariadb|postgressql")
|
||||
register(&dbConn, "db", true, `'database specific connection string for example "user:password@tcp(localhost:3306)/dbname"`)
|
||||
|
||||
parse("db")
|
||||
|
||||
|
@ -92,7 +92,7 @@ func ParseFlags() (f Flags) {
|
|||
f.SiteMode = siteMode
|
||||
f.SSLCertFile = certFile
|
||||
f.SSLKeyFile = keyFile
|
||||
f.DBType = dbType
|
||||
f.DBType = strings.ToLower(dbType)
|
||||
|
||||
return f
|
||||
}
|
||||
|
|
114
core/env/runtime.go
vendored
114
core/env/runtime.go
vendored
|
@ -12,48 +12,126 @@
|
|||
// Package env provides runtime, server level setup and configuration
|
||||
package env
|
||||
|
||||
import "github.com/jmoiron/sqlx"
|
||||
import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SQL-STORE: DbVariant needs to be struct like: name, delims, std params and conn string method
|
||||
|
||||
// Runtime provides access to database, logger and other server-level scoped objects.
|
||||
// Use Context for per-request values.
|
||||
type Runtime struct {
|
||||
Flags Flags
|
||||
Db *sqlx.DB
|
||||
DbVariant DbVariant
|
||||
Log Logger
|
||||
Product ProdInfo
|
||||
Flags Flags
|
||||
Db *sqlx.DB
|
||||
Storage StoreProvider
|
||||
Log Logger
|
||||
Product ProdInfo
|
||||
}
|
||||
|
||||
const (
|
||||
// SiteModeNormal serves app
|
||||
SiteModeNormal = ""
|
||||
|
||||
// SiteModeOffline serves offline.html
|
||||
SiteModeOffline = "1"
|
||||
|
||||
// SiteModeSetup tells Ember to serve setup route
|
||||
SiteModeSetup = "2"
|
||||
|
||||
// SiteModeBadDB redirects to db-error.html page
|
||||
SiteModeBadDB = "3"
|
||||
)
|
||||
|
||||
// DbVariant details SQL database variant
|
||||
type DbVariant string
|
||||
// StoreType represents name of database system
|
||||
type StoreType string
|
||||
|
||||
const (
|
||||
// DbVariantMySQL is MySQL
|
||||
DbVariantMySQL DbVariant = "MySQL"
|
||||
// DBVariantPercona is Percona
|
||||
DBVariantPercona DbVariant = "Percona"
|
||||
// DBVariantMariaDB is MariaDB
|
||||
DBVariantMariaDB DbVariant = "MariaDB"
|
||||
// DBVariantMSSQL is Microsoft SQL Server
|
||||
DBVariantMSSQL DbVariant = "MSSQL"
|
||||
// DBVariantPostgreSQL is PostgreSQL
|
||||
DBVariantPostgreSQL DbVariant = "PostgreSQL"
|
||||
// StoreTypeMySQL is MySQL
|
||||
StoreTypeMySQL StoreType = "MySQL"
|
||||
|
||||
// StoreTypePercona is Percona
|
||||
StoreTypePercona StoreType = "Percona"
|
||||
|
||||
// StoreTypeMariaDB is MariaDB
|
||||
StoreTypeMariaDB StoreType = "MariaDB"
|
||||
|
||||
// StoreTypePostgreSQL is PostgreSQL
|
||||
StoreTypePostgreSQL StoreType = "PostgreSQL"
|
||||
|
||||
// StoreTypeMSSQL is Microsoft SQL Server
|
||||
StoreTypeMSSQL StoreType = "MSSQL"
|
||||
)
|
||||
|
||||
// StoreProvider contains database specific details
|
||||
type StoreProvider struct {
|
||||
// Type identifies storage provider
|
||||
Type StoreType
|
||||
|
||||
// SQL driver name used to open DB connection.
|
||||
DriverName string
|
||||
|
||||
// Database connection string parameters that must be present before connecting to DB
|
||||
Params map[string]string
|
||||
|
||||
// Example holds storage provider specific connection string format
|
||||
// used in error messages
|
||||
Example string
|
||||
}
|
||||
|
||||
// ConnectionString returns provider specific DB connection string
|
||||
// complete with default parameters.
|
||||
func (s *StoreProvider) ConnectionString(cs string) string {
|
||||
switch s.Type {
|
||||
|
||||
case StoreTypePostgreSQL:
|
||||
return "pg"
|
||||
|
||||
case StoreTypeMSSQL:
|
||||
return "sql server"
|
||||
|
||||
case StoreTypeMySQL, StoreTypeMariaDB, StoreTypePercona:
|
||||
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 = s.Params[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 s.Params {
|
||||
if retFirst {
|
||||
retFirst = false
|
||||
} else {
|
||||
ret += "&"
|
||||
}
|
||||
ret += k + "=" + v
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
const (
|
||||
// CommunityEdition is AGPL product variant
|
||||
CommunityEdition = "Community"
|
||||
|
||||
// EnterpriseEdition is commercial licensed product variant
|
||||
EnterpriseEdition = "Enterprise"
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue