Skip to content

Commit

Permalink
Use CASCADE for entries foreign keys.
Browse files Browse the repository at this point in the history
Closes #9.
  • Loading branch information
lemon24 committed Feb 10, 2018
1 parent cfad2be commit 1a56d29
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
40 changes: 39 additions & 1 deletion reader/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def ddl_transaction(db):
db.isolation_level = isolation_level


VERSION = 5
VERSION = 6


class InvalidVersion(Exception):
Expand Down Expand Up @@ -88,6 +88,8 @@ def create_db(db):
read INTEGER,
PRIMARY KEY (id, feed),
FOREIGN KEY (feed) REFERENCES feeds(url)
ON UPDATE CASCADE
ON DELETE CASCADE
);
""")
db.execute("INSERT INTO version VALUES (?);", (VERSION, ))
Expand Down Expand Up @@ -148,11 +150,47 @@ def update_from_4_to_5(db):
""")


def update_from_5_to_6(db):
db.execute("""
ALTER TABLE entries
RENAME TO old_entries;
""")
db.execute("""
CREATE TABLE entries (
id TEXT NOT NULL,
feed TEXT NOT NULL,
title TEXT,
link TEXT,
updated TIMESTAMP,
published TIMESTAMP,
summary TEXT,
content TEXT,
enclosures TEXT,
read INTEGER,
PRIMARY KEY (id, feed),
FOREIGN KEY (feed) REFERENCES feeds(url)
ON UPDATE CASCADE
ON DELETE CASCADE
);
""")
db.execute("""
INSERT INTO entries
SELECT
id, feed, title, link, updated, published,
summary, content, enclosures, read
FROM old_entries;
""")
db.execute("""
DROP TABLE old_entries;
""")


MIGRATIONS = {
1: update_from_1_to_2,
2: update_from_2_to_3,
3: update_from_3_to_4,
4: update_from_4_to_5,
5: update_from_5_to_6,
}


Expand Down
4 changes: 0 additions & 4 deletions reader/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ def add_feed(self, url):

def remove_feed(self, url):
with self.db:
self.db.execute("""
DELETE FROM entries
WHERE feed = :url;
""", locals())
self.db.execute("""
DELETE FROM feeds
WHERE url = :url;
Expand Down

0 comments on commit 1a56d29

Please sign in to comment.