Skip to content

Commit

Permalink
DB path merger (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall authored Apr 12, 2024
2 parents e68f54f + ef77b73 commit 0258df8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
2 changes: 1 addition & 1 deletion frontend/src/Starr/Database/Migrator/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions pkg/app/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 19 additions & 1 deletion pkg/starrs/migrator.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package starrs

import (
"errors"
"fmt"
"path/filepath"
"strconv"
"strings"

"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. */
Expand Down Expand Up @@ -296,7 +299,22 @@ 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 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.
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)
Expand Down
10 changes: 5 additions & 5 deletions pkg/starrs/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions pkg/translations/locales/en/messages.gotext.json
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,38 @@
],
"fuzzy": true
},
{
"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": [
{
"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}'.",
Expand Down

0 comments on commit 0258df8

Please sign in to comment.