simplify subscribing/unsubscribing podcasts
[mygpo.git] / install / update-04.sql
blobae62699eb05f164ef3ac646d6e7814fb3586f8fe
1 -- replaced by mygpo.api.models.Device.latest_actions()
2 DROP VIEW IF EXISTS sync_group_subscription_log;
3 DROP VIEW IF EXISTS sync_group_current_subscription;
6 DELIMITER $$
7 DROP PROCEDURE IF EXISTS update_toplist $$
8 CREATE PROCEDURE update_toplist()
9 BEGIN
10     DECLARE deadlock INT DEFAULT 0;
11         DECLARE attempts INT DEFAULT 0;
13     DROP TABLE IF EXISTS toplist_temp;
14     CREATE TABLE toplist_temp (
15             podcast_id INT PRIMARY KEY REFERENCES podcast (id),
16             subscription_count INT NOT NULL DEFAULT 0,
17             INDEX(podcast_id)
18     );
20     try_loop:WHILE (attempts<3) DO
21     BEGIN
22         DECLARE deadlock_detected CONDITION FOR 1213;
23             DECLARE EXIT HANDLER FOR deadlock_detected
24                 BEGIN
25                     ROLLBACK;
26                     SET deadlock=1;
27                 END;
28             SET deadlock=0;
29                
30             START TRANSACTION;
31             DELETE FROM toplist_temp;
32             INSERT INTO toplist_temp (SELECT a.podcast_id, COUNT(*) AS count_subscription
33                         FROM (SELECT DISTINCT podcast_id, user_id 
34                             FROM public_subscription) a 
35                         GROUP BY podcast_id);
36             DELETE FROM toplist;
37             INSERT INTO toplist (SELECT podcast_id, subscription_count FROM toplist_temp
38                         ORDER BY subscription_count DESC LIMIT 100);
39             
40             COMMIT;
41         END;
42         IF deadlock=0 THEN
43                 LEAVE try_loop;
44             ELSE
45                 SET attempts=attempts+1;
46             END IF;
47             END WHILE try_loop;
49         IF deadlock=1 THEN
50             call FAIL('Toplist is not updated!');
51         END IF;
52         DROP TABLE IF EXISTS toplist_temp;
54 END $$
55 DELIMITER ;
57 CREATE UNIQUE INDEX unique_subscription_log ON subscription_log (device_id, podcast_id, timestamp);
58 CREATE UNIQUE INDEX unique_episode_lg ON episode_log (user_id, episode_id, timestamp);
60 DROP TRIGGER IF EXISTS episode_trig_unique;
62 DELIMITER //
63 CREATE TRIGGER episode_trig_unique BEFORE INSERT ON episode
64 FOR EACH ROW
65 BEGIN
66     declare help_url INT;
67     set help_url = 0;
68   
69        SELECT count(a.id) into help_url FROM episode a where a.url=new.url and a.podcast_id=new.podcast_id;
71     IF help_url > 0 THEN
72         call Fail('This episode already exists!');
73     END IF;
75 END;//
76 DELIMITER ;
79 ALTER TABLE episode_log MODIFY action ENUM ('download', 'play', 'delete', 'new') NOT NULL;