Repos / pytaku / 118a983ebe
commit 118a983ebe49bb8674211ff1cc582326d58574ab
Author: Bùi Thành Nhân <hi@imnhan.com>
Date: Sun Aug 2 15:23:48 2020 +0700
re-fetch title data if older than 6 hours
diff --git a/src/pytaku/database/migrations/latest_schema.sql b/src/pytaku/database/migrations/latest_schema.sql
index eac8301..d770928 100644
--- a/src/pytaku/database/migrations/latest_schema.sql
+++ b/src/pytaku/database/migrations/latest_schema.sql
@@ -8,6 +8,7 @@ CREATE TABLE title (
chapters text,
alt_names text,
descriptions text,
+ updated_at text default (datetime('now')),
unique(id, site)
);
@@ -20,6 +21,7 @@ CREATE TABLE chapter (
name text,
pages text,
groups text,
+ updated_at text default (datetime('now')),
foreign key (title_id, site) references title (id, site),
unique(id, title_id, site),
diff --git a/src/pytaku/database/migrations/m0001.sql b/src/pytaku/database/migrations/m0001.sql
index c8d0fbd..7730151 100644
--- a/src/pytaku/database/migrations/m0001.sql
+++ b/src/pytaku/database/migrations/m0001.sql
@@ -5,6 +5,7 @@ create table title (
chapters text,
alt_names text,
descriptions text,
+ updated_at text default (datetime('now')),
unique(id, site)
);
@@ -18,6 +19,7 @@ create table chapter (
name text,
pages text,
groups text,
+ updated_at text default (datetime('now')),
foreign key (title_id, site) references title (id, site),
unique(id, title_id, site),
diff --git a/src/pytaku/persistence.py b/src/pytaku/persistence.py
index 7bdc074..663a75e 100644
--- a/src/pytaku/persistence.py
+++ b/src/pytaku/persistence.py
@@ -21,7 +21,13 @@ def save_title(title):
:chapters,
:alt_names,
:descriptions
- );
+ ) ON CONFLICT (id, site) DO UPDATE SET
+ name=excluded.name,
+ chapters=excluded.chapters,
+ alt_names=excluded.alt_names,
+ descriptions=excluded.descriptions,
+ updated_at=datetime('now')
+ ;
""",
{
"id": title["id"],
@@ -41,7 +47,8 @@ def load_title(title_id):
"""
SELECT id, name, site, chapters, alt_names, descriptions
FROM title
- WHERE id = ?;
+ WHERE id = ?
+ AND datetime(updated_at) > datetime('now', '-6 hours');
""",
(title_id,),
)