mirror of
https://github.com/ddworken/hishtory.git
synced 2025-06-19 19:37:59 +02:00
Add automatic retrying of DB functions to fix DB locked errors from hishtory init and hishtory install for #119
This commit is contained in:
parent
047ff97cfc
commit
4352f61123
@ -21,6 +21,7 @@ import (
|
|||||||
"github.com/ddworken/hishtory/shared"
|
"github.com/ddworken/hishtory/shared"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
var offlineInit *bool
|
var offlineInit *bool
|
||||||
@ -40,8 +41,8 @@ var installCmd = &cobra.Command{
|
|||||||
if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") == "" {
|
if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") == "" {
|
||||||
db, err := hctx.OpenLocalSqliteDb()
|
db, err := hctx.OpenLocalSqliteDb()
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
var count int64
|
count, err := countStoredEntries(db)
|
||||||
lib.CheckFatalError(db.Model(&data.HistoryEntry{}).Count(&count).Error)
|
lib.CheckFatalError(err)
|
||||||
if count < 10 {
|
if count < 10 {
|
||||||
fmt.Println("Importing existing shell history...")
|
fmt.Println("Importing existing shell history...")
|
||||||
ctx := hctx.MakeContext()
|
ctx := hctx.MakeContext()
|
||||||
@ -64,8 +65,8 @@ var initCmd = &cobra.Command{
|
|||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
db, err := hctx.OpenLocalSqliteDb()
|
db, err := hctx.OpenLocalSqliteDb()
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
var count int64
|
count, err := countStoredEntries(db)
|
||||||
lib.CheckFatalError(db.Model(&data.HistoryEntry{}).Count(&count).Error)
|
lib.CheckFatalError(err)
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
fmt.Printf("Your current hishtory profile has saved history entries, are you sure you want to run `init` and reset?\nNote: This won't clear any imported history entries from your existing shell\n[y/N]")
|
fmt.Printf("Your current hishtory profile has saved history entries, are you sure you want to run `init` and reset?\nNote: This won't clear any imported history entries from your existing shell\n[y/N]")
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
@ -121,6 +122,13 @@ var uninstallCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func countStoredEntries(db *gorm.DB) (int64, error) {
|
||||||
|
return lib.RetryingDbFunctionWithResult(func() (int64, error) {
|
||||||
|
var count int64
|
||||||
|
return count, db.Model(&data.HistoryEntry{}).Count(&count).Error
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func warnIfUnsupportedBashVersion() error {
|
func warnIfUnsupportedBashVersion() error {
|
||||||
_, err := exec.LookPath("bash")
|
_, err := exec.LookPath("bash")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -182,7 +190,9 @@ func install(secretKey string, offline bool) error {
|
|||||||
// Handles people running `hishtory update` when the DB needs updating.
|
// Handles people running `hishtory update` when the DB needs updating.
|
||||||
func handleDbUpgrades(ctx context.Context) error {
|
func handleDbUpgrades(ctx context.Context) error {
|
||||||
db := hctx.GetDb(ctx)
|
db := hctx.GetDb(ctx)
|
||||||
return db.Exec(`UPDATE history_entries SET entry_id = lower(hex(randomblob(12))) WHERE entry_id IS NULL`).Error
|
return lib.RetryingDbFunction(func() error {
|
||||||
|
return db.Exec(`UPDATE history_entries SET entry_id = lower(hex(randomblob(12))) WHERE entry_id IS NULL`).Error
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handles people running `hishtory update` from an old version of hishtory that
|
// Handles people running `hishtory update` from an old version of hishtory that
|
||||||
|
Loading…
x
Reference in New Issue
Block a user