- with transaction.atomic():
- cursor = connection.cursor()
- if connection.vendor == 'postgres':
- cursor.execute("TRUNCATE catalogue_book_ancestor")
- cursor.execute("""
- WITH RECURSIVE ancestry AS (
- SELECT book.id, book.parent_id
- FROM catalogue_book AS book
- WHERE book.parent_id IS NOT NULL
- UNION
- SELECT ancestor.id, book.parent_id
- FROM ancestry AS ancestor, catalogue_book AS book
- WHERE ancestor.parent_id = book.id
- AND book.parent_id IS NOT NULL
- )
- INSERT INTO catalogue_book_ancestor
- (from_book_id, to_book_id)
- SELECT id, parent_id
- FROM ancestry
- ORDER BY id;
- """)
- else:
- cursor.execute("DELETE FROM catalogue_book_ancestor")
- for b in cls.objects.exclude(parent=None):
- parent = b.parent
- while parent is not None:
- b.ancestor.add(parent)
- parent = parent.parent
+ cursor = connection.cursor()
+ if connection.vendor == 'postgres':
+ cursor.execute("TRUNCATE catalogue_book_ancestor")
+ cursor.execute("""
+ WITH RECURSIVE ancestry AS (
+ SELECT book.id, book.parent_id
+ FROM catalogue_book AS book
+ WHERE book.parent_id IS NOT NULL
+ UNION
+ SELECT ancestor.id, book.parent_id
+ FROM ancestry AS ancestor, catalogue_book AS book
+ WHERE ancestor.parent_id = book.id
+ AND book.parent_id IS NOT NULL
+ )
+ INSERT INTO catalogue_book_ancestor
+ (from_book_id, to_book_id)
+ SELECT id, parent_id
+ FROM ancestry
+ ORDER BY id;
+ """)
+ else:
+ cursor.execute("DELETE FROM catalogue_book_ancestor")
+ for b in cls.objects.exclude(parent=None):
+ parent = b.parent
+ while parent is not None:
+ b.ancestor.add(parent)
+ parent = parent.parent