xgi / castero

TUI podcast client for the terminal
MIT License
616 stars 37 forks source link

ON DELETE CASCADE not working #155

Closed tistatos closed 3 years ago

tistatos commented 3 years ago

When a feed is deleted, the episodes are not removed from the episode table.

Steps to reproduce:

  1. Add feed in castero
  2. delete feed
  3. Restart castero
  4. add feed again

You will now have duplicates of all the episodes in the feed.

The issue seems to be that the setting PRAGMA foreign_key = ON before assigning the database file_conn connection to self._conn

class Database():

    ...

    def __init__(self):

                ...

        file_conn = sqlite3.connect(self.PATH, check_same_thread=False)
        file_conn.execute("PRAGMA foreign_keys = ON")

        if self._using_memory:
            memory_conn = sqlite3.connect(":memory:", check_same_thread=False)
            self._copy_database(file_conn, memory_conn)
            self._conn = memory_conn
        else:
            self._conn = file_conn

                ...

        foreign_key_enabled = self._conn.execute('pragma foreign_keys').fetchone()[0]
        assert foreign_key_enabled == 1 # This will fail!!!
tistatos commented 3 years ago

A issue related to when this is fixed: if a feed is removed, but an episode from the feed is still in the queue when castero quits - it will revert the deletion of the feed due to a foreign key constraining failing