mirror of
https://github.com/documize/community.git
synced 2025-07-22 22:59:43 +02:00
refactored routing/web serving logic
This commit is contained in:
parent
76d77bef9b
commit
d888962082
16 changed files with 410 additions and 382 deletions
|
@ -18,9 +18,8 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/log"
|
||||
"github.com/documize/community/core/streamutil"
|
||||
"github.com/documize/community/core/web"
|
||||
"github.com/documize/community/server/web"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
|
@ -39,7 +38,7 @@ var dbPtr *sqlx.DB
|
|||
func Check(runtime *env.Runtime) bool {
|
||||
dbPtr = runtime.Db
|
||||
|
||||
log.Info("Database checks: started")
|
||||
runtime.Log.Info("Database checks: started")
|
||||
|
||||
csBits := strings.Split(runtime.Flags.DBConn, "/")
|
||||
if len(csBits) > 1 {
|
||||
|
@ -48,7 +47,7 @@ func Check(runtime *env.Runtime) bool {
|
|||
|
||||
rows, err := runtime.Db.Query("SELECT VERSION() AS version, @@version_comment as comment, @@character_set_database AS charset, @@collation_database AS collation;")
|
||||
if err != nil {
|
||||
log.Error("Can't get MySQL configuration", err)
|
||||
runtime.Log.Error("Can't get MySQL configuration", err)
|
||||
web.SiteInfo.Issue = "Can't get MySQL configuration: " + err.Error()
|
||||
runtime.Flags.SiteMode = web.SiteModeBadDB
|
||||
return false
|
||||
|
@ -64,7 +63,7 @@ func Check(runtime *env.Runtime) bool {
|
|||
}
|
||||
|
||||
if err != nil {
|
||||
log.Error("no MySQL configuration returned", err)
|
||||
runtime.Log.Error("no MySQL configuration returned", err)
|
||||
web.SiteInfo.Issue = "no MySQL configuration return issue: " + err.Error()
|
||||
runtime.Flags.SiteMode = web.SiteModeBadDB
|
||||
return false
|
||||
|
@ -74,12 +73,12 @@ func Check(runtime *env.Runtime) bool {
|
|||
// MySQL and Percona share same version scheme (e..g 5.7.10).
|
||||
// MariaDB starts at 10.2.x
|
||||
sqlVariant := GetSQLVariant(dbComment)
|
||||
log.Info("Database checks: SQL variant " + sqlVariant)
|
||||
log.Info("Database checks: SQL version " + version)
|
||||
runtime.Log.Info("Database checks: SQL variant " + sqlVariant)
|
||||
runtime.Log.Info("Database checks: SQL version " + version)
|
||||
|
||||
verNums, err := GetSQLVersion(version)
|
||||
if err != nil {
|
||||
log.Error("Database version check failed", err)
|
||||
runtime.Log.Error("Database version check failed", err)
|
||||
}
|
||||
|
||||
// Check minimum MySQL version as we need JSON column type.
|
||||
|
@ -91,7 +90,7 @@ func Check(runtime *env.Runtime) bool {
|
|||
for k, v := range verInts {
|
||||
if verNums[k] < v {
|
||||
want := fmt.Sprintf("%d.%d.%d", verInts[0], verInts[1], verInts[2])
|
||||
log.Error("MySQL version element "+strconv.Itoa(k+1)+" of '"+version+"' not high enough, need at least version "+want, errors.New("bad MySQL version"))
|
||||
runtime.Log.Error("MySQL version element "+strconv.Itoa(k+1)+" of '"+version+"' not high enough, need at least version "+want, errors.New("bad MySQL version"))
|
||||
web.SiteInfo.Issue = "MySQL version element " + strconv.Itoa(k+1) + " of '" + version + "' not high enough, need at least version " + want
|
||||
runtime.Flags.SiteMode = web.SiteModeBadDB
|
||||
return false
|
||||
|
@ -100,13 +99,13 @@ func Check(runtime *env.Runtime) bool {
|
|||
|
||||
{ // check the MySQL character set and collation
|
||||
if charset != "utf8" {
|
||||
log.Error("MySQL character set not utf8:", errors.New(charset))
|
||||
runtime.Log.Error("MySQL character set not utf8:", errors.New(charset))
|
||||
web.SiteInfo.Issue = "MySQL character set not utf8: " + charset
|
||||
runtime.Flags.SiteMode = web.SiteModeBadDB
|
||||
return false
|
||||
}
|
||||
if !strings.HasPrefix(collation, "utf8") {
|
||||
log.Error("MySQL collation sequence not utf8...:", errors.New(collation))
|
||||
runtime.Log.Error("MySQL collation sequence not utf8...:", errors.New(collation))
|
||||
web.SiteInfo.Issue = "MySQL collation sequence not utf8...: " + collation
|
||||
runtime.Flags.SiteMode = web.SiteModeBadDB
|
||||
return false
|
||||
|
@ -118,13 +117,13 @@ func Check(runtime *env.Runtime) bool {
|
|||
if err := runtime.Db.Select(&flds,
|
||||
`SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '`+web.SiteInfo.DBname+
|
||||
`' and TABLE_TYPE='BASE TABLE'`); err != nil {
|
||||
log.Error("Can't get MySQL number of tables", err)
|
||||
runtime.Log.Error("Can't get MySQL number of tables", err)
|
||||
web.SiteInfo.Issue = "Can't get MySQL number of tables: " + err.Error()
|
||||
runtime.Flags.SiteMode = web.SiteModeBadDB
|
||||
return false
|
||||
}
|
||||
if strings.TrimSpace(flds[0]) == "0" {
|
||||
log.Info("Entering database set-up mode because the database is empty.....")
|
||||
runtime.Log.Info("Entering database set-up mode because the database is empty.....")
|
||||
runtime.Flags.SiteMode = web.SiteModeSetup
|
||||
return false
|
||||
}
|
||||
|
@ -139,7 +138,7 @@ func Check(runtime *env.Runtime) bool {
|
|||
for _, table := range tables {
|
||||
var dummy []string
|
||||
if err := runtime.Db.Select(&dummy, "SELECT 1 FROM "+table+" LIMIT 1;"); err != nil {
|
||||
log.Error("Entering bad database mode because: SELECT 1 FROM "+table+" LIMIT 1;", err)
|
||||
runtime.Log.Error("Entering bad database mode because: SELECT 1 FROM "+table+" LIMIT 1;", err)
|
||||
web.SiteInfo.Issue = "MySQL database is not empty, but does not contain table: " + table
|
||||
runtime.Flags.SiteMode = web.SiteModeBadDB
|
||||
return false
|
||||
|
|
|
@ -23,7 +23,7 @@ import (
|
|||
"github.com/documize/community/core/secrets"
|
||||
"github.com/documize/community/core/stringutil"
|
||||
"github.com/documize/community/core/uniqueid"
|
||||
"github.com/documize/community/core/web"
|
||||
"github.com/documize/community/server/web"
|
||||
)
|
||||
|
||||
// runSQL creates a transaction per call
|
||||
|
@ -61,7 +61,6 @@ func runSQL(sql string) (id uint64, err error) {
|
|||
|
||||
// Create the tables in a blank database
|
||||
func Create(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
defer func() {
|
||||
target := "/setup"
|
||||
status := http.StatusBadRequest
|
||||
|
@ -123,7 +122,7 @@ func Create(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if err = Migrate(false /* no tables exist yet */); err != nil {
|
||||
if err = Migrate(api.Runtime, false /* no tables exist yet */); err != nil {
|
||||
log.Error("database.Create()", err)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -22,9 +22,9 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/documize/community/core/log"
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/streamutil"
|
||||
"github.com/documize/community/core/web"
|
||||
"github.com/documize/community/server/web"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
|
@ -69,9 +69,9 @@ func migrations(lastMigration string) (migrationsT, error) {
|
|||
}
|
||||
|
||||
// migrate the database as required, by applying the migrations.
|
||||
func (m migrationsT) migrate(tx *sqlx.Tx) error {
|
||||
func (m migrationsT) migrate(runtime env.Runtime, tx *sqlx.Tx) error {
|
||||
for _, v := range m {
|
||||
log.Info("Processing migration file: " + v)
|
||||
runtime.Log.Info("Processing migration file: " + v)
|
||||
|
||||
buf, err := web.Asset(migrationsDir + "/" + v)
|
||||
if err != nil {
|
||||
|
@ -96,7 +96,7 @@ func (m migrationsT) migrate(tx *sqlx.Tx) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func lockDB() (bool, error) {
|
||||
func lockDB(runtime env.Runtime) (bool, error) {
|
||||
b := make([]byte, 2)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
|
@ -117,8 +117,10 @@ func lockDB() (bool, error) {
|
|||
|
||||
defer func() {
|
||||
_, err = tx.Exec("UNLOCK TABLES;")
|
||||
log.IfErr(err)
|
||||
log.IfErr(tx.Commit())
|
||||
if err != nil {
|
||||
runtime.Log.Error("unable to unlock tables", err)
|
||||
}
|
||||
tx.Commit()
|
||||
}()
|
||||
|
||||
_, err = tx.Exec("INSERT INTO `config` (`key`,`config`) " +
|
||||
|
@ -126,13 +128,13 @@ func lockDB() (bool, error) {
|
|||
if err != nil {
|
||||
// good error would be "Error 1062: Duplicate entry 'DBLOCK' for key 'idx_config_area'"
|
||||
if strings.HasPrefix(err.Error(), "Error 1062:") {
|
||||
log.Info("Database locked by annother Documize instance")
|
||||
runtime.Log.Info("Database locked by annother Documize instance")
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
log.Info("Database locked by this Documize instance")
|
||||
runtime.Log.Info("Database locked by this Documize instance")
|
||||
return true, err // success!
|
||||
}
|
||||
|
||||
|
@ -148,18 +150,18 @@ func unlockDB() error {
|
|||
return tx.Commit()
|
||||
}
|
||||
|
||||
func migrateEnd(tx *sqlx.Tx, err error, amLeader bool) error {
|
||||
func migrateEnd(runtime env.Runtime, tx *sqlx.Tx, err error, amLeader bool) error {
|
||||
if amLeader {
|
||||
defer func() { log.IfErr(unlockDB()) }()
|
||||
defer func() { unlockDB() }()
|
||||
if tx != nil {
|
||||
if err == nil {
|
||||
log.IfErr(tx.Commit())
|
||||
log.Info("Database checks: completed")
|
||||
tx.Commit()
|
||||
runtime.Log.Info("Database checks: completed")
|
||||
return nil
|
||||
}
|
||||
log.IfErr(tx.Rollback())
|
||||
tx.Rollback()
|
||||
}
|
||||
log.Error("Database checks: failed: ", err)
|
||||
runtime.Log.Error("Database checks: failed: ", err)
|
||||
return err
|
||||
}
|
||||
return nil // not the leader, so ignore errors
|
||||
|
@ -186,21 +188,22 @@ func getLastMigration(tx *sqlx.Tx) (lastMigration string, err error) {
|
|||
}
|
||||
|
||||
// Migrate the database as required, consolidated action.
|
||||
func Migrate(ConfigTableExists bool) error {
|
||||
|
||||
func Migrate(runtime env.Runtime, ConfigTableExists bool) error {
|
||||
amLeader := false
|
||||
|
||||
if ConfigTableExists {
|
||||
var err error
|
||||
amLeader, err = lockDB()
|
||||
log.IfErr(err)
|
||||
amLeader, err = lockDB(runtime)
|
||||
if err != nil {
|
||||
runtime.Log.Error("unable to lock DB", err)
|
||||
}
|
||||
} else {
|
||||
amLeader = true // what else can you do?
|
||||
}
|
||||
|
||||
tx, err := (*dbPtr).Beginx()
|
||||
if err != nil {
|
||||
return migrateEnd(tx, err, amLeader)
|
||||
return migrateEnd(runtime, tx, err, amLeader)
|
||||
}
|
||||
|
||||
lastMigration := ""
|
||||
|
@ -208,53 +211,50 @@ func Migrate(ConfigTableExists bool) error {
|
|||
if ConfigTableExists {
|
||||
lastMigration, err = getLastMigration(tx)
|
||||
if err != nil {
|
||||
return migrateEnd(tx, err, amLeader)
|
||||
return migrateEnd(runtime, tx, err, amLeader)
|
||||
}
|
||||
log.Info("Database checks: last applied " + lastMigration)
|
||||
runtime.Log.Info("Database checks: last applied " + lastMigration)
|
||||
}
|
||||
|
||||
mig, err := migrations(lastMigration)
|
||||
if err != nil {
|
||||
return migrateEnd(tx, err, amLeader)
|
||||
return migrateEnd(runtime, tx, err, amLeader)
|
||||
}
|
||||
|
||||
if len(mig) == 0 {
|
||||
log.Info("Database checks: no updates required")
|
||||
return migrateEnd(tx, nil, amLeader) // no migrations to perform
|
||||
runtime.Log.Info("Database checks: no updates required")
|
||||
return migrateEnd(runtime, tx, nil, amLeader) // no migrations to perform
|
||||
}
|
||||
|
||||
if amLeader {
|
||||
log.Info("Database checks: will execute the following update files: " + strings.Join([]string(mig), ", "))
|
||||
return migrateEnd(tx, mig.migrate(tx), amLeader)
|
||||
runtime.Log.Info("Database checks: will execute the following update files: " + strings.Join([]string(mig), ", "))
|
||||
return migrateEnd(runtime, tx, mig.migrate(runtime, tx), amLeader)
|
||||
}
|
||||
|
||||
// a follower instance
|
||||
targetMigration := string(mig[len(mig)-1])
|
||||
for targetMigration != lastMigration {
|
||||
time.Sleep(time.Second)
|
||||
log.Info("Waiting for database migration completion")
|
||||
runtime.Log.Info("Waiting for database migration completion")
|
||||
tx.Rollback() // ignore error
|
||||
tx, err := (*dbPtr).Beginx() // need this in order to see the changed situation since last tx
|
||||
if err != nil {
|
||||
return migrateEnd(tx, err, amLeader)
|
||||
return migrateEnd(runtime, tx, err, amLeader)
|
||||
}
|
||||
lastMigration, _ = getLastMigration(tx)
|
||||
}
|
||||
|
||||
return migrateEnd(tx, nil, amLeader)
|
||||
return migrateEnd(runtime, tx, nil, amLeader)
|
||||
}
|
||||
|
||||
func processSQLfile(tx *sqlx.Tx, buf []byte) error {
|
||||
|
||||
stmts := getStatements(buf)
|
||||
|
||||
for _, stmt := range stmts {
|
||||
|
||||
_, err := tx.Exec(stmt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue