From b7ba449a948e0007f57ed7b0212f1ef3766454e3 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Thu, 11 Apr 2024 21:09:43 -0700 Subject: [PATCH 1/2] Allow path merging --- .../src/Starr/Database/Migrator/Index.svelte | 2 +- pkg/app/setup.go | 6 ++-- pkg/starrs/migrator.go | 18 ++++++++++- pkg/starrs/sqlite3.go | 10 +++--- .../locales/en/messages.gotext.json | 32 +++++++++++++++++++ 5 files changed, 58 insertions(+), 10 deletions(-) diff --git a/frontend/src/Starr/Database/Migrator/Index.svelte b/frontend/src/Starr/Database/Migrator/Index.svelte index 0986a0b..7d914f6 100644 --- a/frontend/src/Starr/Database/Migrator/Index.svelte +++ b/frontend/src/Starr/Database/Migrator/Index.svelte @@ -12,7 +12,7 @@ import Form from "./Form.svelte" let info: any - if (instance && instance.DBPath) { + $: if (instance && instance.DBPath) { MigratorInfo(instance).then( rep => info = rep, err => toast("error", err), diff --git a/pkg/app/setup.go b/pkg/app/setup.go index 4684026..6cf92f8 100644 --- a/pkg/app/setup.go +++ b/pkg/app/setup.go @@ -84,11 +84,11 @@ func (a *App) setupMenu() { } // toggleMenuItem powers the app 'Hide' menu. -func (a *App) toggleMenuItem(item string, checked bool) { +func (a *App) toggleMenuItem(item string, value bool) { settings := a.config.Settings() - settings.Hide[item] = checked + settings.Hide[item] = value name := "Hide." + item - msg := a.log.Translate("Saved: '%s' Value: %v", name, checked) + msg := a.log.Translate("Saved: '%s' Value: %v", name, value) settings, err := a.config.Write(settings) if err != nil { diff --git a/pkg/starrs/migrator.go b/pkg/starrs/migrator.go index f58bf32..f7e3350 100644 --- a/pkg/starrs/migrator.go +++ b/pkg/starrs/migrator.go @@ -1,6 +1,7 @@ package starrs import ( + "errors" "fmt" "path/filepath" "strconv" @@ -8,6 +9,8 @@ import ( "github.com/Notifiarr/toolbarr/pkg/mnd" wr "github.com/wailsapp/wails/v2/pkg/runtime" + "modernc.org/sqlite" + sqlite3 "modernc.org/sqlite/lib" ) /* Root Folders filesystem paths migrator for sqlite3 db. */ @@ -296,7 +299,20 @@ func (s *Starrs) updateRootFolder(appTable AppTable, sql *sqlConn, oldPath, newP _, err = sql.Update("RootFolders", "Path", newPath, fmt.Sprintf("Path='%s'", oldPath)) if err != nil { - return "", err + var sqlErr *sqlite.Error + if !errors.As(err, &sqlErr) { + return "", err + } else if sqlErr.Code() != sqlite3.SQLITE_CONSTRAINT_UNIQUE { + return "", sqlErr + } + + // The root folder already exists, so user may be trying to merge them. Ask. + question := s.log.Translate("Would you like to merge %s with %s?\n", newPath, oldPath) + if !s.app.Ask(s.log.Translate("Root Folder Already Exists"), question) { + return "", sqlErr + } + // Merging them. Delete the old path now. + sql.Delete("RootFolders", fmt.Sprintf("Path='%s'", oldPath)) } msg := s.log.Translate("Success! Changed Root Folder from '%s' to '%s'.", oldPath, newPath) diff --git a/pkg/starrs/sqlite3.go b/pkg/starrs/sqlite3.go index 2bea826..6d24dc4 100644 --- a/pkg/starrs/sqlite3.go +++ b/pkg/starrs/sqlite3.go @@ -103,12 +103,12 @@ func (s *sqlConn) ItemPaths(ctx context.Context, table, column string) (map[int6 // ItemPaths returns the ID=>Path mapping from any table. func (s *sqlConn) GetEntries(ctx context.Context, tcd *TableColumn) ([]*Entry, error) { - sql := fmt.Sprintf("SELECT Id AS id, %s AS name, %s As path FROM %s", tcd.Name, tcd.Column, tcd.Table) - s.log.Debugf("Running Query: %s", sql) + query := fmt.Sprintf("SELECT Id AS id, %s AS name, %s As path FROM %s", tcd.Name, tcd.Column, tcd.Table) + s.log.Debugf("Running Query: %s", query) - rows, err := s.conn.QueryxContext(ctx, sql) + rows, err := s.conn.QueryxContext(ctx, query) if err != nil { - return nil, fmt.Errorf("%s: %w", sql, err) + return nil, fmt.Errorf("%s: %w", query, err) } defer rows.Close() @@ -117,7 +117,7 @@ func (s *sqlConn) GetEntries(ctx context.Context, tcd *TableColumn) ([]*Entry, e for rows.Next() { var row Entry if err = rows.StructScan(&row); err != nil { //nolint:musttag - return nil, fmt.Errorf("%s: %w", sql, err) + return nil, fmt.Errorf("%s: %w", query, err) } output = append(output, &row) diff --git a/pkg/translations/locales/en/messages.gotext.json b/pkg/translations/locales/en/messages.gotext.json index 26d7156..68a9923 100644 --- a/pkg/translations/locales/en/messages.gotext.json +++ b/pkg/translations/locales/en/messages.gotext.json @@ -1670,6 +1670,38 @@ ], "fuzzy": true }, + { + "id": "Would you like to merge {NewPath} with {OldPath}?", + "message": "Would you like to merge {NewPath} with {OldPath}?", + "translation": "Would you like to merge {NewPath} with {OldPath}?", + "translatorComment": "Copied from source.", + "placeholders": [ + { + "id": "NewPath", + "string": "%[1]s", + "type": "string", + "underlyingType": "string", + "argNum": 1, + "expr": "newPath" + }, + { + "id": "OldPath", + "string": "%[2]s", + "type": "string", + "underlyingType": "string", + "argNum": 2, + "expr": "oldPath" + } + ], + "fuzzy": true + }, + { + "id": "Root Folder Already Exists", + "message": "Root Folder Already Exists", + "translation": "Root Folder Already Exists", + "translatorComment": "Copied from source.", + "fuzzy": true + }, { "id": "Success! Changed Root Folder from '{OldPath}' to '{NewPath}'.", "message": "Success! Changed Root Folder from '{OldPath}' to '{NewPath}'.", From ef77b7339fbc8862235e92ffbe4beac0c48e6eec Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Thu, 11 Apr 2024 21:18:55 -0700 Subject: [PATCH 2/2] better looking question --- pkg/starrs/migrator.go | 6 ++++-- pkg/translations/locales/en/messages.gotext.json | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/starrs/migrator.go b/pkg/starrs/migrator.go index f7e3350..258f622 100644 --- a/pkg/starrs/migrator.go +++ b/pkg/starrs/migrator.go @@ -307,12 +307,14 @@ func (s *Starrs) updateRootFolder(appTable AppTable, sql *sqlConn, oldPath, newP } // The root folder already exists, so user may be trying to merge them. Ask. - question := s.log.Translate("Would you like to merge %s with %s?\n", newPath, oldPath) + question := s.log.Translate("Would you like to merge these paths?\n%s\n%s\n", newPath, oldPath) if !s.app.Ask(s.log.Translate("Root Folder Already Exists"), question) { return "", sqlErr } // Merging them. Delete the old path now. - sql.Delete("RootFolders", fmt.Sprintf("Path='%s'", oldPath)) + if _, err := sql.Delete("RootFolders", fmt.Sprintf("Path='%s'", oldPath)); err != nil { + return "", err + } } msg := s.log.Translate("Success! Changed Root Folder from '%s' to '%s'.", oldPath, newPath) diff --git a/pkg/translations/locales/en/messages.gotext.json b/pkg/translations/locales/en/messages.gotext.json index 68a9923..28a5421 100644 --- a/pkg/translations/locales/en/messages.gotext.json +++ b/pkg/translations/locales/en/messages.gotext.json @@ -1671,9 +1671,9 @@ "fuzzy": true }, { - "id": "Would you like to merge {NewPath} with {OldPath}?", - "message": "Would you like to merge {NewPath} with {OldPath}?", - "translation": "Would you like to merge {NewPath} with {OldPath}?", + "id": "Would you like to merge these paths?\n{NewPath}\n{OldPath}", + "message": "Would you like to merge these paths?\n{NewPath}\n{OldPath}", + "translation": "Would you like to merge these paths?\n{NewPath}\n{OldPath}", "translatorComment": "Copied from source.", "placeholders": [ {