diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 6666af8a8c..a748a8c45b 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -1485,7 +1485,18 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): return result def rename_tag(self, old_id, new_name): - new_name = new_name.strip() + # It is possible that new_name is in fact a set of names. Split it on + # comma to find out. If it is, then rename the first one and append the + # rest + new_names = [t.strip() for t in new_name.strip().split(',') if t.strip()] + new_name = new_names[0] + new_names = new_names[1:] + + # get the list of books that reference the tag being changed + books = self.conn.get('''SELECT book from books_tags_link + WHERE tag=?''', (old_id,)) + books = [b[0] for b in books] + new_id = self.conn.get( '''SELECT id from tags WHERE name=?''', (new_name,), all=False) @@ -1501,9 +1512,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): # all the changes. To get around this, we first delete any links # to the new_id from books referencing the old_id, so that # renaming old_id to new_id will be unique on the book - books = self.conn.get('''SELECT book from books_tags_link - WHERE tag=?''', (old_id,)) - for (book_id,) in books: + for book_id in books: self.conn.execute('''DELETE FROM books_tags_link WHERE book=? and tag=?''', (book_id, new_id)) @@ -1512,7 +1521,13 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): WHERE tag=?''',(new_id, old_id,)) # Get rid of the no-longer used publisher self.conn.execute('DELETE FROM tags WHERE id=?', (old_id,)) - self.dirty_books_referencing('tags', new_id, commit=False) + + if new_names: + # have some left-over names to process. Add them to the book. + for book_id in books: + self.set_tags(book_id, new_names, append=True, notify=False, + commit=False) + self.dirtied(books, commit=False) self.conn.commit() def delete_tag_using_id(self, id):