 CREATE INDEX downloads_state_downloads_id_pending on downloads(state,downloads_id) where state='pending';
 create index downloads_extracted on downloads(extracted, state, type) 
     where extracted = 'f' and state = 'success' and type = 'content';
-CREATE INDEX downloads_stories_to_be_extracted on downloads (stories_id) where extracted = false AND state = 'success' AND type = 'content';        
+
+CREATE INDEX downloads_stories_to_be_extracted
+    ON downloads (stories_id)
+    WHERE extracted = false AND state = 'success' AND type = 'content';        
 
 CREATE INDEX downloads_extracted_stories on downloads (stories_id) where type='content' and state='success';
 CREATE INDEX downloads_state_queued_or_fetching on downloads(state) where state='queued' or state='fetching';
 CREATE INDEX downloads_state_fetching ON downloads(state, downloads_id) where state = 'fetching';
 
-CREATE INDEX downloads_in_old_format ON downloads USING btree (downloads_id) WHERE ((state = 'success'::download_state) AND (path ~~ 'content/%'::text));
-
-CREATE INDEX file_status_downloads_time_new_format ON downloads USING btree (file_status, download_time) WHERE (relative_file_path ~~ 'mediacloud-%'::text);
-
-CREATE INDEX relative_file_paths_new_format_to_verify ON downloads USING btree (relative_file_path) WHERE ((((((file_status = 'tbd'::download_file_status) AND (relative_file_path <> 'tbd'::text)) AND (relative_file_path <> 'error'::text)) AND (relative_file_path <> 'na'::text)) AND (relative_file_path <> 'inline'::text)) AND (relative_file_path ~~ 'mediacloud-%'::text));
+CREATE INDEX downloads_in_old_format
+    ON downloads USING btree (downloads_id)
+    WHERE state = 'success'::download_state
+      AND path ~~ 'content/%'::text;
+
+CREATE INDEX file_status_downloads_time_new_format
+    ON downloads USING btree (file_status, download_time)
+    WHERE relative_file_path ~~ 'mediacloud-%'::text;
+
+CREATE INDEX relative_file_paths_new_format_to_verify
+    ON downloads USING btree (relative_file_path)
+    WHERE file_status = 'tbd'::download_file_status
+      AND relative_file_path <> 'tbd'::text
+      AND relative_file_path <> 'error'::text
+      AND relative_file_path <> 'na'::text
+      AND relative_file_path <> 'inline'::text
+      AND relative_file_path ~~ 'mediacloud-%'::text;
 
 create view downloads_media as select d.*, f.media_id as _media_id from downloads d, feeds f where d.feeds_id = f.feeds_id;
 

CREATE VIEW downloads_in_past_day as select * from downloads where download_time > now() - interval '1 day';
CREATE VIEW downloads_with_error_in_past_day as select * from downloads_in_past_day where state = 'error';

CREATE VIEW daily_stats as select * from (SELECT count(*) as daily_downloads from downloads_in_past_day) as dd, (select count(*) as daily_stories from stories_collected_in_past_day) ds , (select count(*) as downloads_to_be_extracted from downloads_to_be_extracted) dex, (select count(*) as download_errors from downloads_with_error_in_past_day ) er;

CREATE TABLE feedless_stories (
        stories_id integer,


    -- Salted hash of a password reset token (with Crypt::SaltedHash, algorithm => 'SHA-256',
    -- salt_len=>64) or NULL
    password_reset_token_hash TEXT  UNIQUE NULL
        CONSTRAINT password_reset_token_hash_sha256
            CHECK(LENGTH(password_reset_token_hash) = 137 OR password_reset_token_hash IS NULL),

    -- Timestamp of the last unsuccessful attempt to log in; used for delaying successive
    -- attempts in order to prevent brute-force attacks
