Artifact
06336e3838fc9d414b291273873f3470682d563ace10b4f2adbdb59bfa470a03:
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 2d 2d 20 63 72 65 te_id');..-- cre
0090: 61 74 65 20 74 61 62 6c 65 20 72 73 73 74 67 5f ate table rsstg_
00a0: 75 73 65 72 73 20 28 69 64 20 69 6e 74 65 67 65 users (id intege
00b0: 72 29 3b 0a 63 72 65 61 74 65 20 74 61 62 6c 65 r);.create table
00c0: 20 72 73 73 74 67 5f 63 68 61 6e 6e 65 6c 20 28 rsstg_channel (
00d0: 0a 09 63 68 61 6e 6e 65 6c 5f 69 64 20 62 69 67 ..channel_id big
00e0: 69 6e 74 20 70 72 69 6d 61 72 79 20 6b 65 79 2c int primary key,
00f0: 0a 09 75 73 65 72 6e 61 6d 65 20 74 65 78 74 20 ..username text
0100: 6e 6f 74 20 6e 75 6c 6c 29 3b 0a 63 72 65 61 74 not null);.creat
0110: 65 20 75 6e 69 71 75 65 20 69 6e 64 65 78 20 72 e unique index r
0120: 73 73 74 67 5f 63 68 61 6e 6e 65 6c 5f 5f 75 73 sstg_channel__us
0130: 65 72 6e 61 6d 65 20 6f 6e 20 72 73 73 74 67 5f ername on rsstg_
0140: 63 68 61 6e 6e 65 6c 28 75 73 65 72 6e 61 6d 65 channel(username
0150: 29 3b 0a 0a 63 72 65 61 74 65 20 74 61 62 6c 65 );..create table
0160: 20 72 73 73 74 67 5f 73 6f 75 72 63 65 20 28 0a rsstg_source (.
0170: 09 73 6f 75 72 63 65 5f 69 64 20 73 65 72 69 61 .source_id seria
0180: 6c 2c 0a 09 63 68 61 6e 6e 65 6c 5f 69 64 20 69 l,..channel_id i
0190: 6e 74 65 67 65 72 20 6e 6f 74 20 6e 75 6c 6c 2c nteger not null,
01a0: 0a 09 75 72 6c 20 74 65 78 74 20 6e 6f 74 20 6e ..url text not n
01b0: 75 6c 6c 2c 0a 09 6c 61 73 74 5f 73 63 72 61 70 ull,..last_scrap
01c0: 65 20 6e 6f 74 20 6e 75 6c 6c 20 74 69 6d 65 73 e not null times
01d0: 74 61 6d 70 74 7a 20 64 65 66 61 75 6c 74 20 6e tamptz default n
01e0: 6f 77 28 29 2c 0a 09 65 6e 61 62 6c 65 64 20 62 ow(),..enabled b
01f0: 6f 6f 6c 65 61 6e 20 6e 6f 74 20 6e 75 6c 6c 20 oolean not null
0200: 64 65 66 61 75 6c 74 20 66 61 6c 73 65 2c 0a 09 default false,..
0210: 69 76 5f 68 61 73 68 20 74 65 78 74 2c 0a 09 6f iv_hash text,..o
0220: 77 6e 65 72 20 62 69 67 69 6e 74 20 6e 6f 74 20 wner bigint not
0230: 6e 75 6c 6c 29 3b 0a 63 72 65 61 74 65 20 75 6e null);.create un
0240: 69 71 75 65 20 69 6e 64 65 78 20 72 73 73 74 67 ique index rsstg
0250: 5f 73 6f 75 72 63 65 5f 5f 73 6f 75 72 63 65 5f _source__source_
0260: 69 64 20 6f 6e 20 72 73 73 74 67 5f 73 6f 75 72 id on rsstg_sour
0270: 63 65 28 73 6f 75 72 63 65 5f 69 64 29 3b 0a 63 ce(source_id);.c
0280: 72 65 61 74 65 20 75 6e 69 71 75 65 20 69 6e 64 reate unique ind
0290: 65 78 20 72 73 73 74 67 5f 73 6f 75 72 63 65 5f ex rsstg_source_
02a0: 5f 63 68 61 6e 6e 65 6c 5f 69 64 5f 5f 6f 77 6e _channel_id__own
02b0: 65 72 20 6f 6e 20 72 73 73 74 67 5f 73 6f 75 72 er on rsstg_sour
02c0: 63 65 28 63 68 61 6e 6e 65 6c 5f 69 64 2c 20 6f ce(channel_id, o
02d0: 77 6e 65 72 29 3b 0a 63 72 65 61 74 65 20 69 6e wner);.create in
02e0: 64 65 78 20 72 73 73 74 67 5f 73 6f 75 72 63 65 dex rsstg_source
02f0: 5f 5f 6f 77 6e 65 72 20 6f 6e 20 72 73 73 74 67 __owner on rsstg
0300: 5f 73 6f 75 72 63 65 28 6f 77 6e 65 72 29 3b 0a _source(owner);.
0310: 0a 63 72 65 61 74 65 20 74 61 62 6c 65 20 72 73 .create table rs
0320: 73 74 67 5f 70 6f 73 74 20 28 0a 09 73 6f 75 72 stg_post (..sour
0330: 63 65 5f 69 64 20 69 6e 74 65 67 65 72 20 6e 6f ce_id integer no
0340: 74 20 6e 75 6c 6c 2c 0a 09 64 61 74 65 20 69 6e t null,..date in
0350: 74 20 6e 6f 74 20 6e 75 6c 6c 2c 0a 09 75 72 6c t not null,..url
0360: 20 74 65 78 74 20 6e 6f 74 20 6e 75 6c 6c 2c 0a text not null,.
0370: 09 68 6f 75 72 20 73 6d 61 6c 6c 69 6e 74 20 6e .hour smallint n
0380: 6f 74 20 6e 75 6c 6c 20 67 65 6e 65 72 61 74 65 ot null generate
0390: 64 20 61 6c 77 61 79 73 20 61 73 20 28 65 78 74 d always as (ext
03a0: 72 61 63 74 28 27 68 6f 75 72 27 20 66 72 6f 6d ract('hour' from
03b0: 20 20 70 6f 73 74 65 64 29 29 20 73 74 6f 72 65 posted)) store
03c0: 64 2c 0a 09 46 4f 52 45 49 47 4e 20 4b 45 59 20 d,..FOREIGN KEY
03d0: 28 73 6f 75 72 63 65 5f 69 64 29 20 52 45 46 45 (source_id) REFE
03e0: 52 45 4e 43 45 53 20 72 73 73 74 67 5f 73 6f 75 RENCES rsstg_sou
03f0: 72 63 65 28 73 6f 75 72 63 65 5f 69 64 29 0a 29 rce(source_id).)
0400: 3b 0a 63 72 65 61 74 65 20 75 6e 69 71 75 65 20 ;.create unique
0410: 69 6e 64 65 78 20 72 73 73 74 67 5f 70 6f 73 74 index rsstg_post
0420: 5f 5f 75 72 6c 20 6f 6e 20 72 73 73 74 67 5f 70 __url on rsstg_p
0430: 6f 73 74 28 75 72 6c 29 3b 0a 63 72 65 61 74 65 ost(url);.create
0440: 20 69 6e 64 65 78 20 72 73 73 74 67 5f 70 6f 73 index rsstg_pos
0450: 74 5f 5f 68 6f 75 72 20 6f 6e 20 72 73 73 74 67 t__hour on rsstg
0460: 5f 70 6f 73 74 28 68 6f 75 72 29 3b 0a 63 72 65 _post(hour);.cre
0470: 61 74 65 20 69 6e 64 65 78 20 72 73 73 74 67 5f ate index rsstg_
0480: 70 6f 73 74 5f 5f 70 6f 73 74 65 64 5f 68 6f 75 post__posted_hou
0490: 72 20 6f 6e 20 72 73 73 74 67 5f 70 6f 73 74 28 r on rsstg_post(
04a0: 70 6f 73 74 65 64 2c 68 6f 75 72 29 3b 0a 0a 63 posted,hour);..c
04b0: 72 65 61 74 65 20 6f 72 20 72 65 70 6c 61 63 65 reate or replace
04c0: 20 76 69 65 77 20 72 73 73 74 67 5f 6f 72 64 65 view rsstg_orde
04d0: 72 20 61 73 0a 09 73 65 6c 65 63 74 20 73 6f 75 r as..select sou
04e0: 72 63 65 5f 69 64 2c 20 63 6f 61 6c 65 73 63 65 rce_id, coalesce
04f0: 28 6c 61 73 74 5f 73 63 72 61 70 65 20 2b 20 6d (last_scrape + m
0500: 61 6b 65 5f 69 6e 74 65 72 76 61 6c 28 30 2c 30 ake_interval(0,0
0510: 2c 30 2c 30 2c 30 2c 28 36 30 20 2f 20 28 63 6f ,0,0,0,(60 / (co
0520: 61 6c 65 73 63 65 28 61 63 74 69 76 69 74 79 2c alesce(activity,
0530: 20 31 29 2f 37 20 2b 20 31 29 20 29 3a 3a 69 6e 1)/7 + 1) )::in
0540: 74 65 67 65 72 29 2c 20 6e 6f 77 28 29 20 2d 20 teger), now() -
0550: 69 6e 74 65 72 76 61 6c 20 27 31 20 6d 69 6e 75 interval '1 minu
0560: 74 65 27 29 20 61 73 20 6e 65 78 74 5f 66 65 74 te') as next_fet
0570: 63 68 0a 09 09 66 72 6f 6d 20 72 73 73 74 67 5f ch...from rsstg_
0580: 73 6f 75 72 63 65 20 6e 61 74 75 72 61 6c 20 6c source natural l
0590: 65 66 74 20 6a 6f 69 6e 0a 09 09 28 73 65 6c 65 eft join...(sele
05a0: 63 74 20 73 6f 75 72 63 65 5f 69 64 2c 20 63 6f ct source_id, co
05b0: 75 6e 74 28 2a 29 20 61 73 20 61 63 74 69 76 69 unt(*) as activi
05c0: 74 79 0a 09 09 09 66 72 6f 6d 20 72 73 73 74 67 ty....from rsstg
05d0: 5f 70 6f 73 74 20 77 68 65 72 65 20 0a 09 09 09 _post where ....
05e0: 09 68 6f 75 72 20 3d 20 65 78 74 72 61 63 74 28 .hour = extract(
05f0: 27 68 6f 75 72 27 20 66 72 6f 6d 20 6e 6f 77 28 'hour' from now(
0600: 29 29 3a 3a 73 6d 61 6c 6c 69 6e 74 0a 09 09 09 ))::smallint....
0610: 09 61 6e 64 20 70 6f 73 74 65 64 20 3e 20 6e 6f .and posted > no
0620: 77 28 29 20 2d 20 69 6e 74 65 72 76 61 6c 20 27 w() - interval '
0630: 37 20 64 61 79 73 27 0a 09 09 09 67 72 6f 75 70 7 days'....group
0640: 20 62 79 20 73 6f 75 72 63 65 5f 69 64 29 20 61 by source_id) a
0650: 73 20 61 63 74 0a 09 09 77 68 65 72 65 20 65 6e s act...where en
0660: 61 62 6c 65 64 0a 09 09 6f 72 64 65 72 20 62 79 abled...order by
0670: 20 6e 65 78 74 5f 66 65 74 63 68 3b 0a next_fetch;.