浏览代码

Add a unique index to sql tables

jherve 1 年之前
父节点
当前提交
48802dd194
共有 1 个文件被更改,包括 16 次插入2 次删除
  1. 16 2
      src/de_quoi_parle_le_monde/storage.py

+ 16 - 2
src/de_quoi_parle_le_monde/storage.py

@@ -26,6 +26,12 @@ class Storage:
                 );
                 """
             )
+            await conn.execute(
+                """
+                CREATE UNIQUE INDEX IF NOT EXISTS main_articles_unique_idx_timestamp_site
+                ON main_articles (timestamp, site);
+            """
+            )
             await conn.execute(
                 """
                 CREATE TABLE IF NOT EXISTS top_articles (
@@ -38,13 +44,20 @@ class Storage:
                 );
                 """
             )
+            await conn.execute(
+                """
+                CREATE UNIQUE INDEX IF NOT EXISTS top_articles_unique_idx_timestamp_site_rank
+                ON top_articles (timestamp, site, rank);
+                """
+            )
 
     async def add_main_article(self, timestamp: str, site: str, article: MainArticle):
         async with aiosqlite.connect(self.conn_str) as conn:
             await conn.execute_insert(
                 """
                 INSERT INTO main_articles (timestamp, site, title, url)
-                VALUES (?, ?, ?, ?);
+                VALUES (?, ?, ?, ?)
+                ON CONFLICT DO NOTHING;
                 """,
                 [timestamp, site, article.title, article.url],
             )
@@ -55,7 +68,8 @@ class Storage:
             await conn.execute_insert(
                 """
                 INSERT INTO top_articles (timestamp, site, title, url, rank)
-                VALUES (?, ?, ?, ?, ?);
+                VALUES (?, ?, ?, ?, ?)
+                ON CONFLICT DO NOTHING;
                 """,
                 [timestamp, site, article.title, article.url, article.rank],
             )