aboutsummaryrefslogtreecommitdiff
path: root/setup.sql
blob: 78587de029bda0e2d7637be9d6f0b87061747dd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Copyright (c) 2023-present Arjun Satarkar <me@arjunsatarkar.net>.
Licensed under the GNU Affero General Public License v3.0. See LICENSE.txt in
the root of this repository for the text of the license.
*/
PRAGMA foreign_keys = ON;

CREATE TABLE IF NOT EXISTS tagrss_info(info_key TEXT PRIMARY KEY, value TEXT) STRICT;

INSERT
    OR REPLACE INTO tagrss_info(info_key, value)
VALUES
    ("version", "0.10.0");

CREATE TABLE IF NOT EXISTS feed_count(
    id INTEGER PRIMARY KEY CHECK (id = 0),
    count INTEGER CHECK(count >= 0)
) STRICT;

INSERT
    OR IGNORE INTO feed_count(id, count)
VALUES
    (0, 0);

CREATE TABLE IF NOT EXISTS feeds(
    id INTEGER PRIMARY KEY,
    source TEXT UNIQUE,
    title TEXT
) STRICT;

CREATE TRIGGER IF NOT EXISTS trig_feeds__increment_feed_count_after_insert
AFTER
INSERT
    ON feeds BEGIN
UPDATE
    feed_count
SET
    count = count + 1;

END;

CREATE TRIGGER IF NOT EXISTS trig_feeds__decrement_feed_count_after_delete
AFTER
    DELETE ON feeds BEGIN
UPDATE
    feed_count
SET
    count = count - 1;

END;

CREATE TABLE IF NOT EXISTS feed_tags(
    feed_id INTEGER REFERENCES feeds(id) ON DELETE CASCADE,
    tag TEXT
) STRICT;

CREATE INDEX IF NOT EXISTS idx_feed_tags__feed_id__tag ON feed_tags(feed_id, tag);

CREATE INDEX IF NOT EXISTS idx_feed_tags__tag__feed_id ON feed_tags(tag, feed_id);

CREATE TABLE IF NOT EXISTS entry_count(
    id INTEGER PRIMARY KEY CHECK (id = 0),
    count INTEGER CHECK(count >= 0)
) STRICT;

INSERT
    OR IGNORE INTO entry_count(id, count)
VALUES
    (0, 0);

CREATE TABLE IF NOT EXISTS entries(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    feed_id INTEGER REFERENCES feeds(id) ON DELETE CASCADE,
    title TEXT,
    link TEXT,
    epoch_published INTEGER,
    epoch_updated INTEGER,
    epoch_downloaded INTEGER
) STRICT;

CREATE INDEX IF NOT EXISTS idx_entries__feed_id__title__link__epoch_published__epoch_updated ON entries(
    feed_id,
    title,
    link,
    epoch_published,
    epoch_updated
);

CREATE TRIGGER IF NOT EXISTS trig_entries__ensure_unique_with_identical_nulls_before_insert BEFORE
INSERT
    ON entries BEGIN
SELECT
    RAISE(IGNORE)
WHERE
    EXISTS (
        SELECT
            1
        FROM
            entries
        WHERE
            feed_id = NEW.feed_id
            AND title IS NEW.title
            AND link IS NEW.link
            AND epoch_published IS NEW.epoch_published
            AND epoch_updated IS NEW.epoch_updated
    );

END;

CREATE TRIGGER IF NOT EXISTS trig_entries__increment_entry_count_after_insert
AFTER
INSERT
    ON entries BEGIN
UPDATE
    entry_count
SET
    count = count + 1;

END;

CREATE TRIGGER IF NOT EXISTS trig_entries__decrement_entry_count_after_delete
AFTER
    DELETE ON entries BEGIN
UPDATE
    entry_count
SET
    count = count - 1;

END;