Bug 386606

Summary: tags search is broken after moving tags (invalid lft/rgt columns)
Product: [Applications] digikam Reporter: Kusi <kusi>
Component: Database-MysqlAssignee: Digikam Developers <digikam-bugs-null>
Status: RESOLVED FIXED    
Severity: normal CC: kusi, metzpinguin
Priority: NOR    
Version: 5.6.0   
Target Milestone: ---   
Platform: openSUSE   
OS: Linux   
Latest Commit: Version Fixed In: 5.8.0
Sentry Crash Report:

Description Kusi 2017-11-06 23:13:32 UTC
The modified preorder tree traversal fields Tags.lft & Tags.rgt become invalid when you move a tag in the tags hierarchy.

Example: assume the 4 tags Monkey, Lifesaver, blue, red with the following hierarchy

- Monkey
- Lifesaver -> blue
- Lifesaver -> red

When you select the tag "Lifesaver", all pictures with the tags blue and red show up. Now you move tag "blue" to the tag root, such that you have the following hierarchy

- Monkey
- blue
- Lifesaver -> red

When you select the tag "Lifesaver", also the picture with tag "blue" is selected, which is a bug

The example above is used in the script digikam_with_sample_db from https://github.com/githubkusi/digikam_controlled_environment. The repo contains some handy tools for database investigations in a controlled, repeatable environment

The reason for the bug are invalid columns Tags.lft/rgt. These columns are not set correctly when moving tags (maybe there are other problematic situations).

looking at current master, I see that there is a move_tagstree for sqlite, but not for mysql in the file dbconfig.xml.cmake.in. This is probably the issue. 

Anyways, adding mysql support for moving tags wouldn't heal an already broken mysql db. I assume that basically everybody using a mysql backend for a few years has a broken tags search functionality by now, which can only be solved by recalculating the lft/rgt fields from scratch. I've written a script which recalculates proper traversal tree columns and works with the example above

https://github.com/githubkusi/digikam_rebuild_mptt

The code is not yet bullet-proof (no db locking during the fix, no sanity checks), but something like this should go into the db maintenance tools.

The issue was already reported here: https://bugs.kde.org/show_bug.cgi?id=338050
which was fixed in 5.1. However, the issue is still present in 5.6 for me.
Comment 1 Kusi 2017-11-07 08:01:58 UTC
related with https://bugs.kde.org/show_bug.cgi?id=383326
Comment 2 Maik Qualmann 2017-11-10 18:37:20 UTC
Git commit 74ea34d9fa0247a91c341ee99e6f001caa8020c3 by Maik Qualmann.
Committed on 10/11/2017 at 18:35.
Pushed by mqualmann into branch 'master'.

fix updating lft/rgt columns in the Tags table with MySQL
Related: bug 383326
FIXED-IN: 5.8.0

M  +0    -11   app/dragdrop/tagdragdrop.cpp
M  +26   -10   data/database/dbconfig.xml.cmake.in
M  +10   -12   libs/database/coredb/coredb.cpp
M  +0    -7    libs/database/coredb/coredb.h

https://commits.kde.org/digikam/74ea34d9fa0247a91c341ee99e6f001caa8020c3
Comment 3 Maik Qualmann 2017-11-19 13:52:25 UTC
Currently, the update of the lft/rgt columns works correctly if only a single tag is moved. There is still a problem if moving a tag tree.

Maik
Comment 4 Maik Qualmann 2017-11-19 19:20:01 UTC
Git commit 74a0d267525c3bb670cf1ea95f87c27f50e7bb50 by Maik Qualmann.
Committed on 19/11/2017 at 19:18.
Pushed by mqualmann into branch 'master'.

fix updating lft/rgt columns if moved a tag tree
FIXED-IN: 5.8.0

M  +2    -1    NEWS
M  +13   -10   data/database/dbconfig.xml.cmake.in
M  +1    -1    libs/database/coredb/coredb.cpp

https://commits.kde.org/digikam/74a0d267525c3bb670cf1ea95f87c27f50e7bb50