mirror of
https://github.com/documize/community.git
synced 2025-07-19 13:19:43 +02:00
mark follower instances with prefix of "-" on the db username
This commit is contained in:
parent
a6873f807c
commit
66dfc8c6ca
4 changed files with 659 additions and 724 deletions
|
@ -50,8 +50,16 @@ func init() {
|
|||
var err error
|
||||
|
||||
environment.GetString(&connectionString, "db", true,
|
||||
`"username:password@protocol(hostname:port)/databasename" for example "fred:bloggs@tcp(localhost:3306)/documize"`,
|
||||
`'username:password@protocol(hostname:port)/databasename" for example "fred:bloggs@tcp(localhost:3306)/documize", or if a follower instance "-fred:bloggs@tcp(localhost:3306)/documize"`,
|
||||
func(*string, string) bool {
|
||||
isLeader := true
|
||||
followerPrefix := "-"
|
||||
if strings.HasPrefix(connectionString, followerPrefix) {
|
||||
isLeader = false
|
||||
connectionString = strings.TrimPrefix(connectionString, followerPrefix)
|
||||
log.Info("This is a follower instance, no DB schema updates will be performed")
|
||||
}
|
||||
|
||||
Db, err = sqlx.Open("mysql", stdConn(connectionString))
|
||||
|
||||
if err != nil {
|
||||
|
@ -73,7 +81,7 @@ func init() {
|
|||
// go into setup mode if required
|
||||
if web.SiteMode != web.SiteModeOffline {
|
||||
if database.Check(Db, connectionString) {
|
||||
if err := database.Migrate(true /* the config table exists */); err != nil {
|
||||
if err := database.Migrate(true /* the config table exists */, isLeader); err != nil {
|
||||
log.Error("Unable to run database migration: ", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
|
|
@ -19,9 +19,9 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/documize/community/core/api/util"
|
||||
"github.com/documize/community/core/web"
|
||||
"github.com/documize/community/core/log"
|
||||
"github.com/documize/community/core/utility"
|
||||
"github.com/documize/community/core/web"
|
||||
)
|
||||
|
||||
// runSQL creates a transaction per call
|
||||
|
@ -116,7 +116,8 @@ func Create(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if err = Migrate(false /* no tables exist yet */); err != nil {
|
||||
if err = Migrate(false, /* no tables exist yet */
|
||||
true /* and we must be the only instance 1st time through */); err != nil {
|
||||
log.Error("database.Create()", err)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -14,15 +14,17 @@ package database
|
|||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/documize/community/core/web"
|
||||
"github.com/documize/community/core/log"
|
||||
"github.com/documize/community/core/utility"
|
||||
"github.com/documize/community/core/web"
|
||||
)
|
||||
|
||||
const migrationsDir = "bindata/scripts"
|
||||
|
@ -96,8 +98,6 @@ func (m migrationsT) migrate(tx *sqlx.Tx) error {
|
|||
|
||||
func migrateEnd(tx *sqlx.Tx, err error) error {
|
||||
if tx != nil {
|
||||
_, ulerr := tx.Exec("UNLOCK TABLES;")
|
||||
log.IfErr(ulerr)
|
||||
if err == nil {
|
||||
log.IfErr(tx.Commit())
|
||||
log.Info("Database checks: completed")
|
||||
|
@ -109,8 +109,32 @@ func migrateEnd(tx *sqlx.Tx, err error) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func getLastMigration(tx *sqlx.Tx) (lastMigration string, err error) {
|
||||
var stmt *sql.Stmt
|
||||
stmt, err = tx.Prepare("SELECT JSON_EXTRACT(`config`,'$.database') FROM `config` WHERE `key` = 'META';")
|
||||
if err == nil {
|
||||
defer utility.Close(stmt)
|
||||
var item = make([]uint8, 0)
|
||||
|
||||
row := stmt.QueryRow()
|
||||
|
||||
err = row.Scan(&item)
|
||||
if err == nil {
|
||||
if len(item) > 1 {
|
||||
q := []byte(`"`)
|
||||
lastMigration = string(bytes.TrimPrefix(bytes.TrimSuffix(item, q), q))
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Migrate the database as required, consolidated action.
|
||||
func Migrate(ConfigTableExists bool) error {
|
||||
func Migrate(ConfigTableExists, amLeader bool) error {
|
||||
|
||||
if !ConfigTableExists && !amLeader {
|
||||
return errors.New("database.Migrate() does not work on empty databases for follower instances")
|
||||
}
|
||||
|
||||
lastMigration := ""
|
||||
|
||||
|
@ -120,31 +144,10 @@ func Migrate(ConfigTableExists bool) error {
|
|||
}
|
||||
|
||||
if ConfigTableExists {
|
||||
_, err = tx.Exec("LOCK TABLE `config` WRITE;")
|
||||
lastMigration, err = getLastMigration(tx)
|
||||
if err != nil {
|
||||
return migrateEnd(tx, err)
|
||||
}
|
||||
|
||||
log.Info("Database checks: lock taken")
|
||||
|
||||
var stmt *sql.Stmt
|
||||
stmt, err = tx.Prepare("SELECT JSON_EXTRACT(`config`,'$.database') FROM `config` WHERE `key` = 'META';")
|
||||
if err == nil {
|
||||
defer utility.Close(stmt)
|
||||
var item = make([]uint8, 0)
|
||||
|
||||
row := stmt.QueryRow()
|
||||
|
||||
err = row.Scan(&item)
|
||||
if err != nil {
|
||||
return migrateEnd(tx, err)
|
||||
}
|
||||
|
||||
if len(item) > 1 {
|
||||
q := []byte(`"`)
|
||||
lastMigration = string(bytes.TrimPrefix(bytes.TrimSuffix(item, q), q))
|
||||
}
|
||||
}
|
||||
log.Info("Database checks: last previously applied file was " + lastMigration)
|
||||
}
|
||||
|
||||
|
@ -157,9 +160,26 @@ func Migrate(ConfigTableExists bool) error {
|
|||
log.Info("Database checks: no updates to perform")
|
||||
return migrateEnd(tx, nil) // no migrations to perform
|
||||
}
|
||||
log.Info("Database checks: will execute the following update files: " + strings.Join([]string(mig), ", "))
|
||||
|
||||
return migrateEnd(tx, mig.migrate(tx))
|
||||
if amLeader {
|
||||
log.Info("Database checks: will execute the following update files: " + strings.Join([]string(mig), ", "))
|
||||
return migrateEnd(tx, mig.migrate(tx))
|
||||
}
|
||||
|
||||
// a follower instance
|
||||
targetMigration := string(mig[len(mig)-1])
|
||||
for targetMigration != lastMigration {
|
||||
time.Sleep(time.Second)
|
||||
log.Info("Waiting for migration process to complete")
|
||||
tx.Rollback() // ignore error
|
||||
tx, err := (*dbPtr).Beginx()
|
||||
if err != nil {
|
||||
return migrateEnd(tx, err)
|
||||
}
|
||||
lastMigration, _ = getLastMigration(tx)
|
||||
}
|
||||
|
||||
return migrateEnd(tx, nil)
|
||||
}
|
||||
|
||||
func processSQLfile(tx *sqlx.Tx, buf []byte) error {
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue