Просмотр исходного кода

Use FK to feature_articles in main and top article tables

jherve 1 год назад
Родитель
Сommit
7e4166f9e3
2 измененных файлов с 24 добавлено и 20 удалено
  1. 4 4
      src/de_quoi_parle_le_monde/main.py
  2. 20 16
      src/de_quoi_parle_le_monde/storage.py

+ 4 - 4
src/de_quoi_parle_le_monde/main.py

@@ -41,12 +41,12 @@ class ArchiveDownloader:
         site_id = await storage.add_site(collection.url)
         snapshot_id = await storage.add_snapshot(site_id, main_page.snapshot.id, dt)
 
-        main = await storage.add_featured_article(main_page.main_article.article)
-        await storage.add_main_article(snapshot_id, main_page.main_article)
+        main_id = await storage.add_featured_article(main_page.main_article.article)
+        await storage.add_main_article(snapshot_id, main_id)
 
         for t in main_page.top_articles:
-            article = await storage.add_featured_article(t.article)
-            await storage.add_top_article(snapshot_id, t)
+            article_id = await storage.add_featured_article(t.article)
+            await storage.add_top_article(snapshot_id, article_id, t)
 
 
 async def main(dler: ArchiveDownloader):

+ 20 - 16
src/de_quoi_parle_le_monde/storage.py

@@ -70,8 +70,7 @@ class Storage:
                 CREATE TABLE IF NOT EXISTS main_articles (
                     id INTEGER PRIMARY KEY AUTOINCREMENT,
                     snapshot_id INTEGER REFERENCES snapshots (id) ON DELETE CASCADE,
-                    title TEXT,
-                    url TEXT
+                    featured_article_id INTEGER REFERENCES featured_articles (id) ON DELETE CASCADE
                 );
                 """
             )
@@ -86,8 +85,7 @@ class Storage:
                 CREATE TABLE IF NOT EXISTS top_articles (
                     id INTEGER PRIMARY KEY AUTOINCREMENT,
                     snapshot_id INTEGER REFERENCES snapshots (id) ON DELETE CASCADE,
-                    title TEXT,
-                    url TEXT,
+                    featured_article_id INTEGER REFERENCES featured_articles (id) ON DELETE CASCADE,
                     rank INTEGER
                 );
                 """
@@ -104,17 +102,20 @@ class Storage:
                 CREATE VIEW IF NOT EXISTS main_articles_view AS
                     SELECT
                         si.id AS site_id,
-                        si.original_url AS original_url,
                         s.id AS snapshot_id,
+                        fa.id AS featured_article_id,
+                        si.original_url AS original_url,
                         s.timestamp_virtual,
-                        m.title,
-                        m.url
+                        fa.title,
+                        fa.url
                     FROM
                         main_articles as m
                     JOIN
                         snapshots AS s ON s.id = m.snapshot_id
                     JOIN
                         sites AS si ON si.id = s.site_id
+                    JOIN
+                        featured_articles AS fa ON m.featured_article_id = fa.id
                 """
             )
 
@@ -123,11 +124,12 @@ class Storage:
                 CREATE VIEW IF NOT EXISTS top_articles_view AS
                     SELECT
                         si.id AS site_id,
-                        si.original_url AS original_url,
                         s.id AS snapshot_id,
+                        fa.id AS featured_article_id,
+                        si.original_url AS original_url,
                         s.timestamp_virtual,
-                        t.title,
-                        t.url,
+                        fa.title,
+                        fa.url,
                         t.rank
                     FROM
                         top_articles as t
@@ -135,6 +137,8 @@ class Storage:
                         snapshots AS s ON s.id = t.snapshot_id
                     JOIN
                         sites AS si ON si.id = s.site_id
+                    JOIN
+                        featured_articles AS fa ON t.featured_article_id = fa.id
                 """
             )
 
@@ -202,21 +206,21 @@ class Storage:
             await conn.commit()
             return id_
 
-    async def add_main_article(self, snapshot_id: int, article: MainArticle):
+    async def add_main_article(self, snapshot_id: int, article_id: int):
         async with aiosqlite.connect(self.conn_str) as conn:
             await conn.execute_insert(
-                self._insert_stmt("main_articles", ["snapshot_id", "title", "url"]),
-                [snapshot_id, article.article.title, article.article.url],
+                self._insert_stmt("main_articles", ["snapshot_id", "featured_article_id"]),
+                [snapshot_id, article_id],
             )
             await conn.commit()
 
-    async def add_top_article(self, snapshot_id: int, article: TopArticle):
+    async def add_top_article(self, snapshot_id: int, article_id: int, article: TopArticle):
         async with aiosqlite.connect(self.conn_str) as conn:
             await conn.execute_insert(
                 self._insert_stmt(
-                    "top_articles", ["snapshot_id", "title", "url", "rank"]
+                    "top_articles", ["snapshot_id", "featured_article_id", "rank"]
                 ),
-                [snapshot_id, article.article.title, article.article.url, article.rank],
+                [snapshot_id, article_id, article.rank],
             )
             await conn.commit()