From 57307bfd010468a50e0ebba0d88ea06c0410912e Mon Sep 17 00:00:00 2001 From: Stash Dev Date: Sun, 10 Feb 2019 12:47:34 -0800 Subject: [PATCH] Fixed database locking issue when importing --- database/database.go | 13 ++++++------- manager/task_export.go | 2 ++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/database/database.go b/database/database.go index 590e29341..90dda0e5f 100644 --- a/database/database.go +++ b/database/database.go @@ -9,6 +9,7 @@ import ( _ "github.com/mattn/go-sqlite3" "github.com/stashapp/stash/logger" "github.com/stashapp/stash/utils" + "os" ) var DB *sqlx.DB @@ -19,7 +20,8 @@ func Initialize(databasePath string) { // https://github.com/mattn/go-sqlite3 conn, err := sqlx.Open("sqlite3", "file:"+databasePath+"?_fk=true") - conn.SetMaxOpenConns(10) + conn.SetMaxOpenConns(25) + conn.SetMaxIdleConns(4) if err != nil { logger.Fatalf("db.Open(): %q\n", err) } @@ -27,12 +29,9 @@ func Initialize(databasePath string) { } func Reset(databasePath string) { - _, _ = DB.Exec("PRAGMA writable_schema = 1;") - _, _ = DB.Exec("delete from sqlite_master where type in ('table', 'index', 'trigger');") - _, _ = DB.Exec("PRAGMA writable_schema = 0;") - _, _ = DB.Exec("VACUUM;") - _, _ = DB.Exec("PRAGMA INTEGRITY_CHECK;") - runMigrations(databasePath) + _ = DB.Close() + _ = os.Remove(databasePath) + Initialize(databasePath) } // Migrate the database diff --git a/manager/task_export.go b/manager/task_export.go index cc5ed8409..da4f5a84f 100644 --- a/manager/task_export.go +++ b/manager/task_export.go @@ -41,6 +41,7 @@ func (t *ExportTask) Start(wg *sync.WaitGroup) { func (t *ExportTask) ExportScenes(ctx context.Context) { tx := database.DB.MustBeginTx(ctx, nil) + defer tx.Commit() qb := models.NewSceneQueryBuilder() studioQB := models.NewStudioQueryBuilder() galleryQB := models.NewGalleryQueryBuilder() @@ -321,6 +322,7 @@ func (t *ExportTask) ExportStudios(ctx context.Context) { func (t *ExportTask) ExportScrapedItems(ctx context.Context) { tx := database.DB.MustBeginTx(ctx, nil) + defer tx.Commit() qb := models.NewScrapedItemQueryBuilder() sqb := models.NewStudioQueryBuilder() scrapedItems, err := qb.All()