MCPcopy
hub / github.com/dnote/dnote / findOrphanedNotes

Function findOrphanedNotes

pkg/cli/cmd/sync/sync.go:821–863  ·  view source on GitHub ↗

findOrphanedNotes returns a list of all orphaned notes

(db *database.DB)

Source from the content-addressed store, hash-verified

819
820// findOrphanedNotes returns a list of all orphaned notes
821func findOrphanedNotes(db *database.DB) (int, []struct{ noteUUID, bookUUID string }, error) {
822 var orphanCount int
823 err := db.QueryRow(`
824 SELECT COUNT(*) FROM notes n
825 WHERE NOT EXISTS (
826 SELECT 1 FROM books b
827 WHERE b.uuid = n.book_uuid
828 AND NOT b.deleted
829 )
830 `).Scan(&orphanCount)
831 if err != nil {
832 return 0, nil, err
833 }
834
835 if orphanCount == 0 {
836 return 0, nil, nil
837 }
838
839 rows, err := db.Query(`
840 SELECT n.uuid, n.book_uuid
841 FROM notes n
842 WHERE NOT EXISTS (
843 SELECT 1 FROM books b
844 WHERE b.uuid = n.book_uuid
845 AND NOT b.deleted
846 )
847 `)
848 if err != nil {
849 return orphanCount, nil, err
850 }
851 defer rows.Close()
852
853 var orphans []struct{ noteUUID, bookUUID string }
854 for rows.Next() {
855 var noteUUID, bookUUID string
856 if err := rows.Scan(&noteUUID, &bookUUID); err != nil {
857 continue
858 }
859 orphans = append(orphans, struct{ noteUUID, bookUUID string }{noteUUID, bookUUID})
860 }
861
862 return orphanCount, orphans, nil
863}
864
865func warnOrphanedNotes(tx *database.DB) {
866 count, orphans, err := findOrphanedNotes(tx)

Callers 2

warnOrphanedNotesFunction · 0.85
checkPostSyncIntegrityFunction · 0.85

Calls 3

QueryRowMethod · 0.65
QueryMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected