From e543b119e8ab1e7a86ee7af7711c49a6016029df Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Wed, 4 Jun 2025 11:11:17 -0600 Subject: [PATCH] forgetit csv --- main.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/main.go b/main.go index 0b133db..6118b94 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "path" "slices" "strings" + "sync" //_ "github.com/glebarez/sqlite" "github.com/google/uuid" @@ -130,7 +131,22 @@ func main() { } } } + break // TODO } + + /* + originalJelly, _ := sql.Open("sqlite", path.Join(data, jellyDB)) + defer originalJelly.Close() + originalLibrary, _ := sql.Open("sqlite", path.Join(data, libDB)) + defer originalLibrary.Close() + + if err := Diff(originalJelly, jellyfinDB); err != nil { + log.Fatal(err) + } + if err := Diff(originalLibrary, libraryDB); err != nil { + log.Fatal(err) + } + */ } func CloneForColumn[T any](db *sql.DB, table, column string, from, to T, fixed map[string]any) error { @@ -384,3 +400,69 @@ func Select[T any](db *sql.DB, q string, args ...any) ([]T, error) { return results, rows.Err() } + +func Diff(a, b *sql.DB) error { + tables, err := Tables(a) + if err != nil { + return err + } + + for _, table := range tables { + if err := DiffTable(a, b, table); err != nil { + log.Printf("DIFF ERR | (%s) %v", table, err) + } + } + return nil +} + +func DiffTable(a, b *sql.DB, table string) error { + orderBys, err := Select[string](a, `SELECT name FROM PRAGMA_TABLE_INFO($1) WHERE "pk" LIMIT 1`, table) + if err != nil { + return err + } else if len(orderBys) == 0 { + orderBys, err = Select[string](a, `SELECT name FROM PRAGMA_TABLE_INFO($1) LIMIT 1`, table) + if err != nil { + return err + } else if len(orderBys) == 0 { + return nil + } + } + orderBy := orderBys[0] + + columns, err := Columns(a, table) + if err != nil { + return err + } + + q := fmt.Sprintf(`SELECT %s FROM %q ORDER BY %q`, func() string { + quoted := []string{} + for _, col := range columns { + quoted = append(quoted, fmt.Sprintf("%q", col)) + } + return strings.Join(quoted, ", ") + }(), table, orderBy) + //log.Printf("DIFF | %s", q) + + wg := &sync.WaitGroup{} + defer wg.Wait() + var rowa []any + var rowb []any + wg.Add(1) + go func() { + defer wg.Done() + if err := ForEach(a, func(row []any) error { rowa = row; return nil }, q); err != nil { + log.Fatalf("failed foreach a: %v", err) + } + }() + wg.Add(1) + go func() { + defer wg.Done() + if err := ForEach(b, func(row []any) error { rowb = row; return nil }, q); err != nil { + log.Fatalf("failed foreach b: %v", err) + } + }() + + _, _ = rowa, rowb + + return nil +}