Artifact
eaca20cec6c53f8de5dafef75b09355bd7cf76f05fcf86a62917419899a1cbbb:
0000: 63 72 65 61 74 65 20 74 61 62 6c 65 20 72 73 73 create table rss
0010: 74 67 5f 75 70 64 61 74 65 73 20 28 6f 77 6e 65 tg_updates (owne
0020: 72 20 69 6e 74 65 67 65 72 2c 20 75 70 64 61 74 r integer, updat
0030: 65 20 6a 73 6f 6e 62 29 3b 0a 0a 63 72 65 61 74 e jsonb);..creat
0040: 65 20 75 6e 69 71 75 65 20 69 6e 64 65 78 20 72 e unique index r
0050: 73 73 74 67 5f 75 70 64 61 74 65 73 5f 5f 69 64 sstg_updates__id
0060: 20 6f 6e 20 72 73 73 74 67 5f 75 70 64 61 74 65 on rsstg_update
0070: 73 28 75 70 64 61 74 65 2d 3e 3e 27 75 70 64 61 s(update->>'upda
0080: 74 65 5f 69 64 27 29 3b 0a 0a 63 72 65 61 74 65 te_id');..create
0090: 20 74 61 62 6c 65 20 72 73 73 74 67 5f 73 6f 75 table rsstg_sou
00a0: 72 63 65 20 28 0a 09 73 6f 75 72 63 65 5f 69 64 rce (..source_id
00b0: 20 73 65 72 69 61 6c 2c 0a 09 63 68 61 6e 6e 65 serial,..channe
00c0: 6c 20 74 65 78 74 20 6e 6f 74 20 6e 75 6c 6c 2c l text not null,
00d0: 0a 09 63 68 61 6e 6e 65 6c 5f 69 64 20 69 6e 74 ..channel_id int
00e0: 65 67 65 72 20 6e 6f 74 20 6e 75 6c 6c 2c 0a 09 eger not null,..
00f0: 75 72 6c 20 74 65 78 74 20 6e 6f 74 20 6e 75 6c url text not nul
0100: 6c 2c 0a 09 6c 61 73 74 5f 73 63 72 61 70 65 20 l,..last_scrape
0110: 6e 6f 74 20 6e 75 6c 6c 20 74 69 6d 65 73 74 61 not null timesta
0120: 6d 70 74 7a 20 64 65 66 61 75 6c 74 20 6e 6f 77 mptz default now
0130: 28 29 2c 0a 09 65 6e 61 62 6c 65 64 20 62 6f 6f (),..enabled boo
0140: 6c 65 61 6e 20 6e 6f 74 20 6e 75 6c 6c 20 64 65 lean not null de
0150: 66 61 75 6c 74 20 66 61 6c 73 65 2c 0a 09 69 76 fault false,..iv
0160: 5f 68 61 73 68 20 74 65 78 74 2c 0a 09 6f 77 6e _hash text,..own
0170: 65 72 20 62 69 67 69 6e 74 20 6e 6f 74 20 6e 75 er bigint not nu
0180: 6c 6c 29 3b 0a 63 72 65 61 74 65 20 75 6e 69 71 ll);.create uniq
0190: 75 65 20 69 6e 64 65 78 20 72 73 73 74 67 5f 73 ue index rsstg_s
01a0: 6f 75 72 63 65 5f 5f 73 6f 75 72 63 65 5f 69 64 ource__source_id
01b0: 20 6f 6e 20 72 73 73 74 67 5f 73 6f 75 72 63 65 on rsstg_source
01c0: 28 73 6f 75 72 63 65 5f 69 64 29 3b 0a 63 72 65 (source_id);.cre
01d0: 61 74 65 20 75 6e 69 71 75 65 20 69 6e 64 65 78 ate unique index
01e0: 20 72 73 73 74 67 5f 73 6f 75 72 63 65 5f 5f 63 rsstg_source__c
01f0: 68 61 6e 6e 65 6c 5f 69 64 5f 5f 6f 77 6e 65 72 hannel_id__owner
0200: 20 6f 6e 20 72 73 73 74 67 5f 73 6f 75 72 63 65 on rsstg_source
0210: 28 63 68 61 6e 6e 65 6c 5f 69 64 2c 20 6f 77 6e (channel_id, own
0220: 65 72 29 3b 0a 63 72 65 61 74 65 20 69 6e 64 65 er);.create inde
0230: 78 20 72 73 73 74 67 5f 73 6f 75 72 63 65 5f 5f x rsstg_source__
0240: 6f 77 6e 65 72 20 6f 6e 20 72 73 73 74 67 5f 73 owner on rsstg_s
0250: 6f 75 72 63 65 28 6f 77 6e 65 72 29 3b 0a 0a 63 ource(owner);..c
0260: 72 65 61 74 65 20 74 61 62 6c 65 20 72 73 73 74 reate table rsst
0270: 67 5f 70 6f 73 74 20 28 0a 09 73 6f 75 72 63 65 g_post (..source
0280: 5f 69 64 20 69 6e 74 65 67 65 72 20 6e 6f 74 20 _id integer not
0290: 6e 75 6c 6c 2c 0a 09 64 61 74 65 20 69 6e 74 20 null,..date int
02a0: 6e 6f 74 20 6e 75 6c 6c 2c 0a 09 75 72 6c 20 74 not null,..url t
02b0: 65 78 74 20 6e 6f 74 20 6e 75 6c 6c 2c 0a 09 68 ext not null,..h
02c0: 6f 75 72 20 73 6d 61 6c 6c 69 6e 74 20 6e 6f 74 our smallint not
02d0: 20 6e 75 6c 6c 20 67 65 6e 65 72 61 74 65 64 20 null generated
02e0: 61 6c 77 61 79 73 20 61 73 20 28 65 78 74 72 61 always as (extra
02f0: 63 74 28 27 68 6f 75 72 27 20 66 72 6f 6d 20 20 ct('hour' from
0300: 70 6f 73 74 65 64 29 29 20 73 74 6f 72 65 64 2c posted)) stored,
0310: 0a 09 46 4f 52 45 49 47 4e 20 4b 45 59 20 28 73 ..FOREIGN KEY (s
0320: 6f 75 72 63 65 5f 69 64 29 20 52 45 46 45 52 45 ource_id) REFERE
0330: 4e 43 45 53 20 72 73 73 74 67 5f 73 6f 75 72 63 NCES rsstg_sourc
0340: 65 28 73 6f 75 72 63 65 5f 69 64 29 20 6f 6e 20 e(source_id) on
0350: 64 65 6c 65 74 65 20 63 61 73 63 61 64 65 2c 0a delete cascade,.
0360: 29 3b 0a 63 72 65 61 74 65 20 75 6e 69 71 75 65 );.create unique
0370: 20 69 6e 64 65 78 20 72 73 73 74 67 5f 70 6f 73 index rsstg_pos
0380: 74 5f 5f 75 72 6c 20 6f 6e 20 72 73 73 74 67 5f t__url on rsstg_
0390: 70 6f 73 74 28 75 72 6c 29 3b 0a 63 72 65 61 74 post(url);.creat
03a0: 65 20 69 6e 64 65 78 20 72 73 73 74 67 5f 70 6f e index rsstg_po
03b0: 73 74 5f 5f 68 6f 75 72 20 6f 6e 20 72 73 73 74 st__hour on rsst
03c0: 67 5f 70 6f 73 74 28 68 6f 75 72 29 3b 0a 63 72 g_post(hour);.cr
03d0: 65 61 74 65 20 69 6e 64 65 78 20 72 73 73 74 67 eate index rsstg
03e0: 5f 70 6f 73 74 5f 5f 70 6f 73 74 65 64 5f 68 6f _post__posted_ho
03f0: 75 72 20 6f 6e 20 72 73 73 74 67 5f 70 6f 73 74 ur on rsstg_post
0400: 28 70 6f 73 74 65 64 2c 68 6f 75 72 29 3b 0a 0a (posted,hour);..
0410: 63 72 65 61 74 65 20 6f 72 20 72 65 70 6c 61 63 create or replac
0420: 65 20 76 69 65 77 20 72 73 73 74 67 5f 6f 72 64 e view rsstg_ord
0430: 65 72 20 61 73 0a 09 73 65 6c 65 63 74 20 73 6f er as..select so
0440: 75 72 63 65 5f 69 64 2c 20 63 6f 61 6c 65 73 63 urce_id, coalesc
0450: 65 28 6c 61 73 74 5f 73 63 72 61 70 65 20 2b 20 e(last_scrape +
0460: 6d 61 6b 65 5f 69 6e 74 65 72 76 61 6c 28 30 2c make_interval(0,
0470: 30 2c 30 2c 30 2c 30 2c 28 36 30 20 2f 20 28 63 0,0,0,0,(60 / (c
0480: 6f 61 6c 65 73 63 65 28 61 63 74 69 76 69 74 79 oalesce(activity
0490: 2c 20 31 29 2f 37 20 2b 20 31 29 20 29 3a 3a 69 , 1)/7 + 1) )::i
04a0: 6e 74 65 67 65 72 29 2c 20 6e 6f 77 28 29 20 2d nteger), now() -
04b0: 20 69 6e 74 65 72 76 61 6c 20 27 31 20 6d 69 6e interval '1 min
04c0: 75 74 65 27 29 20 61 73 20 6e 65 78 74 5f 66 65 ute') as next_fe
04d0: 74 63 68 2c 20 6f 77 6e 65 72 0a 09 09 66 72 6f tch, owner...fro
04e0: 6d 20 72 73 73 74 67 5f 73 6f 75 72 63 65 20 6e m rsstg_source n
04f0: 61 74 75 72 61 6c 20 6c 65 66 74 20 6a 6f 69 6e atural left join
0500: 0a 09 09 28 73 65 6c 65 63 74 20 73 6f 75 72 63 ...(select sourc
0510: 65 5f 69 64 2c 20 63 6f 75 6e 74 28 2a 29 20 61 e_id, count(*) a
0520: 73 20 61 63 74 69 76 69 74 79 0a 09 09 09 66 72 s activity....fr
0530: 6f 6d 20 72 73 73 74 67 5f 70 6f 73 74 20 77 68 om rsstg_post wh
0540: 65 72 65 20 0a 09 09 09 09 68 6f 75 72 20 3d 20 ere .....hour =
0550: 65 78 74 72 61 63 74 28 27 68 6f 75 72 27 20 66 extract('hour' f
0560: 72 6f 6d 20 6e 6f 77 28 29 29 3a 3a 73 6d 61 6c rom now())::smal
0570: 6c 69 6e 74 0a 09 09 09 09 61 6e 64 20 70 6f 73 lint.....and pos
0580: 74 65 64 20 3e 20 6e 6f 77 28 29 20 2d 20 69 6e ted > now() - in
0590: 74 65 72 76 61 6c 20 27 37 20 64 61 79 73 27 0a terval '7 days'.
05a0: 09 09 09 67 72 6f 75 70 20 62 79 20 73 6f 75 72 ...group by sour
05b0: 63 65 5f 69 64 29 20 61 73 20 61 63 74 0a 09 09 ce_id) as act...
05c0: 77 68 65 72 65 20 65 6e 61 62 6c 65 64 0a 09 09 where enabled...
05d0: 6f 72 64 65 72 20 62 79 20 6e 65 78 74 5f 66 65 order by next_fe
05e0: 74 63 68 3b 0a tch;.