1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-20 13:49:42 +02:00

mark follower instances with prefix of "-" on the db username

This commit is contained in:
Elliott Stoneham 2016-07-27 12:58:19 +01:00
parent a6873f807c
commit 66dfc8c6ca
4 changed files with 659 additions and 724 deletions

View file

@ -50,8 +50,16 @@ func init() {
var err error var err error
environment.GetString(&connectionString, "db", true, 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 { 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)) Db, err = sqlx.Open("mysql", stdConn(connectionString))
if err != nil { if err != nil {
@ -73,7 +81,7 @@ func init() {
// go into setup mode if required // go into setup mode if required
if web.SiteMode != web.SiteModeOffline { if web.SiteMode != web.SiteModeOffline {
if database.Check(Db, connectionString) { 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) log.Error("Unable to run database migration: ", err)
os.Exit(2) os.Exit(2)
} }

View file

@ -19,9 +19,9 @@ import (
"time" "time"
"github.com/documize/community/core/api/util" "github.com/documize/community/core/api/util"
"github.com/documize/community/core/web"
"github.com/documize/community/core/log" "github.com/documize/community/core/log"
"github.com/documize/community/core/utility" "github.com/documize/community/core/utility"
"github.com/documize/community/core/web"
) )
// runSQL creates a transaction per call // runSQL creates a transaction per call
@ -116,7 +116,8 @@ func Create(w http.ResponseWriter, r *http.Request) {
return 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) log.Error("database.Create()", err)
return return
} }

View file

@ -14,15 +14,17 @@ package database
import ( import (
"bytes" "bytes"
"database/sql" "database/sql"
"errors"
"regexp" "regexp"
"sort" "sort"
"strings" "strings"
"time"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/documize/community/core/web"
"github.com/documize/community/core/log" "github.com/documize/community/core/log"
"github.com/documize/community/core/utility" "github.com/documize/community/core/utility"
"github.com/documize/community/core/web"
) )
const migrationsDir = "bindata/scripts" const migrationsDir = "bindata/scripts"
@ -96,8 +98,6 @@ func (m migrationsT) migrate(tx *sqlx.Tx) error {
func migrateEnd(tx *sqlx.Tx, err error) error { func migrateEnd(tx *sqlx.Tx, err error) error {
if tx != nil { if tx != nil {
_, ulerr := tx.Exec("UNLOCK TABLES;")
log.IfErr(ulerr)
if err == nil { if err == nil {
log.IfErr(tx.Commit()) log.IfErr(tx.Commit())
log.Info("Database checks: completed") log.Info("Database checks: completed")
@ -109,24 +109,7 @@ func migrateEnd(tx *sqlx.Tx, err error) error {
return err return err
} }
// Migrate the database as required, consolidated action. func getLastMigration(tx *sqlx.Tx) (lastMigration string, err error) {
func Migrate(ConfigTableExists bool) error {
lastMigration := ""
tx, err := (*dbPtr).Beginx()
if err != nil {
return migrateEnd(tx, err)
}
if ConfigTableExists {
_, err = tx.Exec("LOCK TABLE `config` WRITE;")
if err != nil {
return migrateEnd(tx, err)
}
log.Info("Database checks: lock taken")
var stmt *sql.Stmt var stmt *sql.Stmt
stmt, err = tx.Prepare("SELECT JSON_EXTRACT(`config`,'$.database') FROM `config` WHERE `key` = 'META';") stmt, err = tx.Prepare("SELECT JSON_EXTRACT(`config`,'$.database') FROM `config` WHERE `key` = 'META';")
if err == nil { if err == nil {
@ -136,15 +119,35 @@ func Migrate(ConfigTableExists bool) error {
row := stmt.QueryRow() row := stmt.QueryRow()
err = row.Scan(&item) err = row.Scan(&item)
if err != nil { if err == nil {
return migrateEnd(tx, err)
}
if len(item) > 1 { if len(item) > 1 {
q := []byte(`"`) q := []byte(`"`)
lastMigration = string(bytes.TrimPrefix(bytes.TrimSuffix(item, q), q)) lastMigration = string(bytes.TrimPrefix(bytes.TrimSuffix(item, q), q))
} }
} }
}
return
}
// Migrate the database as required, consolidated action.
func Migrate(ConfigTableExists, amLeader bool) error {
if !ConfigTableExists && !amLeader {
return errors.New("database.Migrate() does not work on empty databases for follower instances")
}
lastMigration := ""
tx, err := (*dbPtr).Beginx()
if err != nil {
return migrateEnd(tx, err)
}
if ConfigTableExists {
lastMigration, err = getLastMigration(tx)
if err != nil {
return migrateEnd(tx, err)
}
log.Info("Database checks: last previously applied file was " + lastMigration) log.Info("Database checks: last previously applied file was " + lastMigration)
} }
@ -157,11 +160,28 @@ func Migrate(ConfigTableExists bool) error {
log.Info("Database checks: no updates to perform") log.Info("Database checks: no updates to perform")
return migrateEnd(tx, nil) // no migrations to perform return migrateEnd(tx, nil) // no migrations to perform
} }
log.Info("Database checks: will execute the following update files: " + strings.Join([]string(mig), ", "))
if amLeader {
log.Info("Database checks: will execute the following update files: " + strings.Join([]string(mig), ", "))
return migrateEnd(tx, mig.migrate(tx)) 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 { func processSQLfile(tx *sqlx.Tx, buf []byte) error {
stmts := getStatements(buf) stmts := getStatements(buf)

File diff suppressed because one or more lines are too long