mirror of
https://github.com/documize/community.git
synced 2025-07-19 13:19:43 +02:00
PostgreSQL prep
Update of vendored SQL libs and refactoring of store provider layer.
This commit is contained in:
parent
d0e005f638
commit
b455e5eaf5
105 changed files with 10949 additions and 2376 deletions
|
@ -11,100 +11,100 @@
|
|||
|
||||
package database
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"time"
|
||||
// import (
|
||||
// "crypto/rand"
|
||||
// "time"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
// "github.com/documize/community/core/env"
|
||||
// "github.com/jmoiron/sqlx"
|
||||
// )
|
||||
|
||||
// Lock will try to lock the database instance to the running process.
|
||||
// Uses a "random" delay as a por man's database cluster-aware process.
|
||||
// We skip delay if there are no scripts to process.
|
||||
func Lock(runtime *env.Runtime, scriptsToProcess int) (bool, error) {
|
||||
// Wait for random period of time.
|
||||
b := make([]byte, 2)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
wait := ((time.Duration(b[0]) << 8) | time.Duration(b[1])) * time.Millisecond / 10 // up to 6.5 secs wait
|
||||
// // Lock will try to lock the database instance to the running process.
|
||||
// // Uses a "random" delay as a por man's database cluster-aware process.
|
||||
// // We skip delay if there are no scripts to process.
|
||||
// func Lock(runtime *env.Runtime, scriptsToProcess int) (bool, error) {
|
||||
// // Wait for random period of time.
|
||||
// b := make([]byte, 2)
|
||||
// _, err := rand.Read(b)
|
||||
// if err != nil {
|
||||
// return false, err
|
||||
// }
|
||||
// wait := ((time.Duration(b[0]) << 8) | time.Duration(b[1])) * time.Millisecond / 10 // up to 6.5 secs wait
|
||||
|
||||
// Why delay if nothing to process?
|
||||
if scriptsToProcess > 0 {
|
||||
time.Sleep(wait)
|
||||
}
|
||||
// // Why delay if nothing to process?
|
||||
// if scriptsToProcess > 0 {
|
||||
// time.Sleep(wait)
|
||||
// }
|
||||
|
||||
// Start transaction fotr lock process.
|
||||
tx, err := runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
runtime.Log.Error("Database: unable to start transaction", err)
|
||||
return false, err
|
||||
}
|
||||
// // Start transaction fotr lock process.
|
||||
// tx, err := runtime.Db.Beginx()
|
||||
// if err != nil {
|
||||
// runtime.Log.Error("Database: unable to start transaction", err)
|
||||
// return false, err
|
||||
// }
|
||||
|
||||
// Lock the database.
|
||||
_, err = tx.Exec(runtime.StoreProvider.QueryStartLock())
|
||||
if err != nil {
|
||||
runtime.Log.Error("Database: unable to lock tables", err)
|
||||
return false, err
|
||||
}
|
||||
// // Lock the database.
|
||||
// _, err = tx.Exec(runtime.StoreProvider.QueryStartLock())
|
||||
// if err != nil {
|
||||
// runtime.Log.Error("Database: unable to lock tables", err)
|
||||
// return false, err
|
||||
// }
|
||||
|
||||
// Unlock the database at the end of this function.
|
||||
defer func() {
|
||||
_, err = tx.Exec(runtime.StoreProvider.QueryFinishLock())
|
||||
if err != nil {
|
||||
runtime.Log.Error("Database: unable to unlock tables", err)
|
||||
}
|
||||
tx.Commit()
|
||||
}()
|
||||
// // Unlock the database at the end of this function.
|
||||
// defer func() {
|
||||
// _, err = tx.Exec(runtime.StoreProvider.QueryFinishLock())
|
||||
// if err != nil {
|
||||
// runtime.Log.Error("Database: unable to unlock tables", err)
|
||||
// }
|
||||
// tx.Commit()
|
||||
// }()
|
||||
|
||||
// Try to record this process as leader of database migration process.
|
||||
_, err = tx.Exec(runtime.StoreProvider.QueryInsertProcessID())
|
||||
if err != nil {
|
||||
runtime.Log.Info("Database: marked as slave process awaiting upgrade")
|
||||
return false, nil
|
||||
}
|
||||
// // Try to record this process as leader of database migration process.
|
||||
// _, err = tx.Exec(runtime.StoreProvider.QueryInsertProcessID())
|
||||
// if err != nil {
|
||||
// runtime.Log.Info("Database: marked as slave process awaiting upgrade")
|
||||
// return false, nil
|
||||
// }
|
||||
|
||||
// We are the leader!
|
||||
runtime.Log.Info("Database: marked as database upgrade process leader")
|
||||
return true, err
|
||||
}
|
||||
// // We are the leader!
|
||||
// runtime.Log.Info("Database: marked as database upgrade process leader")
|
||||
// return true, err
|
||||
// }
|
||||
|
||||
// Unlock completes process that was started with Lock().
|
||||
func Unlock(runtime *env.Runtime, tx *sqlx.Tx, err error, amLeader bool) error {
|
||||
if amLeader {
|
||||
defer func() {
|
||||
doUnlock(runtime)
|
||||
}()
|
||||
// // Unlock completes process that was started with Lock().
|
||||
// func Unlock(runtime *env.Runtime, tx *sqlx.Tx, err error, amLeader bool) error {
|
||||
// if amLeader {
|
||||
// defer func() {
|
||||
// doUnlock(runtime)
|
||||
// }()
|
||||
|
||||
if tx != nil {
|
||||
if err == nil {
|
||||
tx.Commit()
|
||||
runtime.Log.Info("Database: is ready")
|
||||
return nil
|
||||
}
|
||||
tx.Rollback()
|
||||
}
|
||||
// if tx != nil {
|
||||
// if err == nil {
|
||||
// tx.Commit()
|
||||
// runtime.Log.Info("Database: is ready")
|
||||
// return nil
|
||||
// }
|
||||
// tx.Rollback()
|
||||
// }
|
||||
|
||||
runtime.Log.Error("Database: install/upgrade failed", err)
|
||||
// runtime.Log.Error("Database: install/upgrade failed", err)
|
||||
|
||||
return err
|
||||
}
|
||||
// return err
|
||||
// }
|
||||
|
||||
return nil // not the leader, so ignore errors
|
||||
}
|
||||
// return nil // not the leader, so ignore errors
|
||||
// }
|
||||
|
||||
// Helper method for defer function called from Unlock().
|
||||
func doUnlock(runtime *env.Runtime) error {
|
||||
tx, err := runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec(runtime.StoreProvider.QueryDeleteProcessID())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// // Helper method for defer function called from Unlock().
|
||||
// func doUnlock(runtime *env.Runtime) error {
|
||||
// tx, err := runtime.Db.Beginx()
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// _, err = tx.Exec(runtime.StoreProvider.QueryDeleteProcessID())
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
// return tx.Commit()
|
||||
// }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue