Artifact
ba3c3933f82174732a123687c5b6eef9aa0025abd47b77b94f7da549e9793371:
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 29 ..username text)
0100: 3b 0a 63 72 65 61 74 65 20 75 6e 69 71 75 65 20 ;.create unique
0110: 69 6e 64 65 78 20 72 73 73 74 67 5f 63 68 61 6e index rsstg_chan
0120: 6e 65 6c 5f 5f 75 73 65 72 6e 61 6d 65 20 6f 6e nel__username on
0130: 20 72 73 73 74 67 5f 63 68 61 6e 6e 65 6c 28 75 rsstg_channel(u
0140: 73 65 72 6e 61 6d 65 29 3b 0a 0a 63 72 65 61 74 sername);..creat
0150: 65 20 74 61 62 6c 65 20 72 73 73 74 67 5f 73 6f e table rsstg_so
0160: 75 72 63 65 20 28 0a 09 73 6f 75 72 63 65 5f 69 urce (..source_i
0170: 64 20 73 65 72 69 61 6c 2c 0a 09 63 68 61 6e 6e d serial,..chann
0180: 65 6c 5f 69 64 20 69 6e 74 65 67 65 72 20 6e 6f el_id integer no
0190: 74 20 6e 75 6c 6c 2c 0a 09 75 72 6c 20 74 65 78 t null,..url tex
01a0: 74 20 6e 6f 74 20 6e 75 6c 6c 2c 0a 09 6c 61 73 t not null,..las
01b0: 74 5f 66 65 74 63 68 20 74 69 6d 65 73 74 61 6d t_fetch timestam
01c0: 70 74 7a 2c 0a 09 6c 61 73 74 5f 73 63 72 61 70 ptz,..last_scrap
01d0: 65 20 74 69 6d 65 73 74 61 6d 70 74 7a 20 64 65 e timestamptz de
01e0: 66 61 75 6c 74 20 6e 6f 77 28 29 2c 0a 09 65 6e fault now(),..en
01f0: 61 62 6c 65 64 20 62 6f 6f 6c 65 61 6e 20 64 65 abled boolean de
0200: 66 61 75 6c 74 20 66 61 6c 73 65 2c 0a 09 69 76 fault false,..iv
0210: 5f 68 61 73 68 20 74 65 78 74 2c 0a 09 6f 77 6e _hash text,..own
0220: 65 72 20 62 69 67 69 6e 74 20 6e 6f 74 20 6e 75 er bigint not nu
0230: 6c 6c 29 3b 0a 63 72 65 61 74 65 20 75 6e 69 71 ll);.create uniq
0240: 75 65 20 69 6e 64 65 78 20 72 73 73 74 67 5f 73 ue index rsstg_s
0250: 6f 75 72 63 65 5f 5f 73 6f 75 72 63 65 5f 69 64 ource__source_id
0260: 20 6f 6e 20 72 73 73 74 67 5f 73 6f 75 72 63 65 on rsstg_source
0270: 28 73 6f 75 72 63 65 5f 69 64 29 3b 0a 63 72 65 (source_id);.cre
0280: 61 74 65 20 75 6e 69 71 75 65 20 69 6e 64 65 78 ate unique index
0290: 20 72 73 73 74 67 5f 73 6f 75 72 63 65 5f 5f 63 rsstg_source__c
02a0: 68 61 6e 6e 65 6c 5f 69 64 5f 5f 6f 77 6e 65 72 hannel_id__owner
02b0: 20 6f 6e 20 72 73 73 74 67 5f 73 6f 75 72 63 65 on rsstg_source
02c0: 28 63 68 61 6e 6e 65 6c 5f 69 64 2c 20 6f 77 6e (channel_id, own
02d0: 65 72 29 3b 0a 63 72 65 61 74 65 20 69 6e 64 65 er);.create inde
02e0: 78 20 72 73 73 74 67 5f 73 6f 75 72 63 65 5f 5f x rsstg_source__
02f0: 6f 77 6e 65 72 20 6f 6e 20 72 73 73 74 67 5f 73 owner on rsstg_s
0300: 6f 75 72 63 65 28 6f 77 6e 65 72 29 3b 0a 0a 63 ource(owner);..c
0310: 72 65 61 74 65 20 74 61 62 6c 65 20 72 73 73 74 reate table rsst
0320: 67 5f 70 6f 73 74 20 28 0a 09 73 6f 75 72 63 65 g_post (..source
0330: 5f 69 64 20 69 6e 74 65 67 65 72 20 6e 6f 74 20 _id integer not
0340: 6e 75 6c 6c 2c 0a 09 64 61 74 65 20 69 6e 74 20 null,..date int
0350: 6e 6f 74 20 6e 75 6c 6c 2c 0a 09 75 72 6c 20 74 not null,..url t
0360: 65 78 74 20 6e 6f 74 20 6e 75 6c 6c 2c 0a 09 68 ext not null,..h
0370: 6f 75 72 20 73 6d 61 6c 6c 69 6e 74 20 67 65 6e our smallint gen
0380: 65 72 61 74 65 64 20 61 6c 77 61 79 73 20 61 73 erated always as
0390: 20 28 65 78 74 72 61 63 74 28 27 68 6f 75 72 27 (extract('hour'
03a0: 20 66 72 6f 6d 20 20 70 6f 73 74 65 64 29 29 20 from posted))
03b0: 73 74 6f 72 65 64 2c 0a 09 46 4f 52 45 49 47 4e stored,..FOREIGN
03c0: 20 4b 45 59 20 28 73 6f 75 72 63 65 5f 69 64 29 KEY (source_id)
03d0: 20 52 45 46 45 52 45 4e 43 45 53 20 72 73 73 74 REFERENCES rsst
03e0: 67 5f 73 6f 75 72 63 65 28 73 6f 75 72 63 65 5f g_source(source_
03f0: 69 64 29 0a 29 3b 0a 63 72 65 61 74 65 20 75 6e id).);.create un
0400: 69 71 75 65 20 69 6e 64 65 78 20 72 73 73 74 67 ique index rsstg
0410: 5f 70 6f 73 74 5f 5f 75 72 6c 20 6f 6e 20 72 73 _post__url on rs
0420: 73 74 67 5f 70 6f 73 74 28 75 72 6c 29 3b 0a 63 stg_post(url);.c
0430: 72 65 61 74 65 20 69 6e 64 65 78 20 72 73 73 74 reate index rsst
0440: 67 5f 70 6f 73 74 5f 5f 68 6f 75 72 20 6f 6e 20 g_post__hour on
0450: 72 73 73 74 67 5f 70 6f 73 74 28 68 6f 75 72 29 rsstg_post(hour)
0460: 3b 0a 0a 63 72 65 61 74 65 20 6f 72 20 72 65 70 ;..create or rep
0470: 6c 61 63 65 20 76 69 65 77 20 72 73 73 74 67 5f lace view rsstg_
0480: 6f 72 64 65 72 20 61 73 0a 09 73 65 6c 65 63 74 order as..select
0490: 20 73 6f 75 72 63 65 5f 69 64 2c 20 63 6f 61 6c source_id, coal
04a0: 65 73 63 65 28 6c 61 73 74 5f 73 63 72 61 70 65 esce(last_scrape
04b0: 20 2b 20 6d 61 6b 65 5f 69 6e 74 65 72 76 61 6c + make_interval
04c0: 28 30 2c 30 2c 30 2c 30 2c 30 2c 28 36 30 20 2f (0,0,0,0,0,(60 /
04d0: 20 63 6f 61 6c 65 73 63 65 28 61 63 74 69 76 69 coalesce(activi
04e0: 74 79 2c 20 31 29 29 3a 3a 69 6e 74 65 67 65 72 ty, 1))::integer
04f0: 29 2c 20 6e 6f 77 28 29 20 2d 20 69 6e 74 65 72 ), now() - inter
0500: 76 61 6c 20 27 31 20 6d 69 6e 75 74 65 27 29 20 val '1 minute')
0510: 61 73 20 6e 65 78 74 5f 66 65 74 63 68 0a 09 09 as next_fetch...
0520: 66 72 6f 6d 20 72 73 73 74 67 5f 73 6f 75 72 63 from rsstg_sourc
0530: 65 20 6e 61 74 75 72 61 6c 20 6c 65 66 74 20 6a e natural left j
0540: 6f 69 6e 0a 09 09 28 73 65 6c 65 63 74 20 73 6f oin...(select so
0550: 75 72 63 65 5f 69 64 2c 20 63 6f 75 6e 74 28 2a urce_id, count(*
0560: 29 20 61 73 20 61 63 74 69 76 69 74 79 0a 09 09 ) as activity...
0570: 09 66 72 6f 6d 20 72 73 73 74 67 5f 70 6f 73 74 .from rsstg_post
0580: 20 77 68 65 72 65 20 0a 09 09 09 09 68 6f 75 72 where .....hour
0590: 20 3d 20 65 78 74 72 61 63 74 28 27 68 6f 75 72 = extract('hour
05a0: 27 20 66 72 6f 6d 20 6e 6f 77 28 29 29 0a 09 09 ' from now())...
05b0: 09 67 72 6f 75 70 20 62 79 20 73 6f 75 72 63 65 .group by source
05c0: 5f 69 64 29 20 61 73 20 61 63 74 0a 09 09 77 68 _id) as act...wh
05d0: 65 72 65 20 65 6e 61 62 6c 65 64 0a 09 09 6f 72 ere enabled...or
05e0: 64 65 72 20 62 79 20 6e 65 78 74 5f 66 65 74 63 der by next_fetc
05f0: 68 3b 0a h;.