From d4e706daefe9b978a5db95901c9c986f8d58c2a2 Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Mon, 26 Sep 2022 11:27:53 +1000 Subject: [PATCH] Only set video algorithm after migration 12 (#2946) --- internal/manager/checksum.go | 34 ----------- internal/manager/config/config.go | 13 ++-- .../manager/config/config_concurrency_test.go | 1 - internal/manager/manager.go | 7 --- internal/manager/post_migrate.go | 8 --- pkg/sqlite/migrations/12_postmigrate.go | 60 +++++++++++++++++++ ui/v2.5/src/docs/en/Changelog/v0170.md | 1 + 7 files changed, 67 insertions(+), 57 deletions(-) delete mode 100644 internal/manager/post_migrate.go create mode 100644 pkg/sqlite/migrations/12_postmigrate.go diff --git a/internal/manager/checksum.go b/internal/manager/checksum.go index 53f368913..cbe9d85d8 100644 --- a/internal/manager/checksum.go +++ b/internal/manager/checksum.go @@ -4,43 +4,9 @@ import ( "context" "errors" - "github.com/stashapp/stash/internal/manager/config" - "github.com/stashapp/stash/pkg/logger" "github.com/stashapp/stash/pkg/models" - "github.com/stashapp/stash/pkg/txn" ) -type SceneCounter interface { - Count(ctx context.Context) (int, error) -} - -func setInitialMD5Config(ctx context.Context, txnManager txn.Manager, counter SceneCounter) { - // if there are no scene files in the database, then default the - // VideoFileNamingAlgorithm config setting to oshash and calculateMD5 to - // false, otherwise set them to true for backwards compatibility purposes - var count int - if err := txn.WithTxn(ctx, txnManager, func(ctx context.Context) error { - var err error - count, err = counter.Count(ctx) - return err - }); err != nil { - logger.Errorf("Error while counting scenes: %s", err.Error()) - return - } - - usingMD5 := count != 0 - defaultAlgorithm := models.HashAlgorithmOshash - if usingMD5 { - defaultAlgorithm = models.HashAlgorithmMd5 - } - - config := config.GetInstance() - config.SetChecksumDefaultValues(defaultAlgorithm, usingMD5) - if err := config.Write(); err != nil { - logger.Errorf("Error while writing configuration file: %s", err.Error()) - } -} - type SceneMissingHashCounter interface { CountMissingChecksum(ctx context.Context) (int, error) CountMissingOSHash(ctx context.Context) (int, error) diff --git a/internal/manager/config/config.go b/internal/manager/config/config.go index fdbb45bba..85ac935fb 100644 --- a/internal/manager/config/config.go +++ b/internal/manager/config/config.go @@ -326,6 +326,12 @@ func (i *Instance) Set(key string, value interface{}) { i.main.Set(key, value) } +func (i *Instance) SetDefault(key string, value interface{}) { + i.Lock() + defer i.Unlock() + i.main.SetDefault(key, value) +} + func (i *Instance) SetPassword(value string) { // if blank, don't bother hashing; we want it to be blank if value == "" { @@ -1304,13 +1310,6 @@ func (i *Instance) Validate() error { return nil } -func (i *Instance) SetChecksumDefaultValues(defaultAlgorithm models.HashAlgorithm, usingMD5 bool) { - i.Lock() - defer i.Unlock() - i.main.SetDefault(VideoFileNamingAlgorithm, defaultAlgorithm) - i.main.SetDefault(CalculateMD5, usingMD5) -} - func (i *Instance) setDefaultValues(write bool) error { // read data before write lock scope defaultDatabaseFilePath := i.GetDefaultDatabaseFilePath() diff --git a/internal/manager/config/config_concurrency_test.go b/internal/manager/config/config_concurrency_test.go index 5ed8deac3..a7f1455e6 100644 --- a/internal/manager/config/config_concurrency_test.go +++ b/internal/manager/config/config_concurrency_test.go @@ -108,7 +108,6 @@ func TestConcurrentConfigAccess(t *testing.T) { i.Set(DisableDropdownCreatePerformer, i.GetDisableDropdownCreate().Performer) i.Set(DisableDropdownCreateStudio, i.GetDisableDropdownCreate().Studio) i.Set(DisableDropdownCreateTag, i.GetDisableDropdownCreate().Tag) - i.SetChecksumDefaultValues(i.GetVideoFileNamingAlgorithm(), i.IsCalculateMD5()) i.Set(AutostartVideoOnPlaySelected, i.GetAutostartVideoOnPlaySelected()) i.Set(ContinuePlaylistDefault, i.GetContinuePlaylistDefault()) i.Set(PythonPath, i.GetPythonPath()) diff --git a/internal/manager/manager.go b/internal/manager/manager.go index 7260abb28..be871461d 100644 --- a/internal/manager/manager.go +++ b/internal/manager/manager.go @@ -484,10 +484,6 @@ func (s *Manager) PostInit(ctx context.Context) error { return err } - if database.Ready() == nil { - s.PostMigrate(ctx) - } - return nil } @@ -671,9 +667,6 @@ func (s *Manager) Migrate(ctx context.Context, input MigrateInput) error { return errors.New(errStr) } - // perform post-migration operations - s.PostMigrate(ctx) - // if no backup path was provided, then delete the created backup if input.BackupPath == "" { if err := os.Remove(backupPath); err != nil { diff --git a/internal/manager/post_migrate.go b/internal/manager/post_migrate.go deleted file mode 100644 index acc93ae69..000000000 --- a/internal/manager/post_migrate.go +++ /dev/null @@ -1,8 +0,0 @@ -package manager - -import "context" - -// PostMigrate is executed after migrations have been executed. -func (s *Manager) PostMigrate(ctx context.Context) { - setInitialMD5Config(ctx, s.Repository, s.Repository.Scene) -} diff --git a/pkg/sqlite/migrations/12_postmigrate.go b/pkg/sqlite/migrations/12_postmigrate.go new file mode 100644 index 000000000..e3e8e3f0a --- /dev/null +++ b/pkg/sqlite/migrations/12_postmigrate.go @@ -0,0 +1,60 @@ +package migrations + +import ( + "context" + + "github.com/jmoiron/sqlx" + "github.com/stashapp/stash/internal/manager/config" + "github.com/stashapp/stash/pkg/logger" + "github.com/stashapp/stash/pkg/models" + "github.com/stashapp/stash/pkg/sqlite" +) + +func post12(ctx context.Context, db *sqlx.DB) error { + m := schema12Migrator{ + migrator: migrator{ + db: db, + }, + } + + return m.migrateConfig(ctx) +} + +type schema12Migrator struct { + migrator +} + +func (m *schema12Migrator) migrateConfig(ctx context.Context) error { + // if there are no scene files in the database, then default the + // VideoFileNamingAlgorithm config setting to oshash and calculateMD5 to + // false, otherwise set them to true for backwards compatibility purposes + var count int + if err := m.withTxn(ctx, func(tx *sqlx.Tx) error { + query := "SELECT COUNT(*) from `scenes`" + + return tx.Get(&count, query) + }); err != nil { + return err + } + + usingMD5 := count != 0 + defaultAlgorithm := models.HashAlgorithmOshash + if usingMD5 { + logger.Infof("Defaulting video file naming algorithm to %s", models.HashAlgorithmMd5) + defaultAlgorithm = models.HashAlgorithmMd5 + } + + c := config.GetInstance() + + c.SetDefault(config.VideoFileNamingAlgorithm, defaultAlgorithm) + c.SetDefault(config.CalculateMD5, usingMD5) + if err := c.Write(); err != nil { + logger.Errorf("Error while writing configuration file: %s", err.Error()) + } + + return nil +} + +func init() { + sqlite.RegisterPostMigration(12, post12) +} diff --git a/ui/v2.5/src/docs/en/Changelog/v0170.md b/ui/v2.5/src/docs/en/Changelog/v0170.md index 37892d36a..751434442 100644 --- a/ui/v2.5/src/docs/en/Changelog/v0170.md +++ b/ui/v2.5/src/docs/en/Changelog/v0170.md @@ -18,6 +18,7 @@ After migrating, please run a scan on your entire library to populate missing da * Moved Changelogs to Settings page. ([#2726](https://github.com/stashapp/stash/pull/2726)) ### 🐛 Bug fixes +* Fix generated file naming algorithm being set incorrectly in certain circumstances. ([#2496](https://github.com/stashapp/stash/pull/2946)) * Fix continue queue checkbox value not persisting. ([#2895](https://github.com/stashapp/stash/pull/2895)) * Fix `autostartVideoOnPlaySelected` option not applying when navigating from scene queue. ([#2896](https://github.com/stashapp/stash/pull/2896)) * Fix incorrect gallery value in Scene edit tab after navigating from scene queue. ([#2897](https://github.com/stashapp/stash/pull/2897))