2 DROP TRIGGER IF EXISTS subscription_log_trigger;
5 CREATE TRIGGER subscription_log_trigger BEFORE INSERT ON subscription_log
11 SELECT count(a.user_id) into help_id FROM current_subscription a
12 where a.device_id = new.device_id
13 and a.podcast_id = new.podcast_id;
15 IF new.action = 1 THEN
18 call Fail('This subscription already exists!');
22 call Fail('This subscription not exists!');
29 DROP TRIGGER IF EXISTS podcast_trig_url_unique;
32 CREATE TRIGGER podcast_trig_url_unique BEFORE INSERT ON podcast
38 SELECT count(a.url) into help_url FROM podcast a where a.url=new.url;
41 call Fail('This URL already exists!');
47 ALTER TABLE user ADD COLUMN id INT PRIMARY KEY AUTO_INCREMENT;
49 DROP VIEW IF EXISTS public_subscription;
50 CREATE VIEW public_subscription AS
53 ifnull(s.public, true) AS pub_subscription,
54 ifnull(u.public_profile,true) AS pub_profile
55 FROM (current_subscription cs
56 LEFT JOIN subscription s
57 ON cs.podcast_id = s.podcast_id
58 AND cs.user_id = s.user_id)
60 ON s.user_id = u.user_ptr_id
61 HAVING pub_subscription=1
65 DROP PROCEDURE IF EXISTS update_toplist $$
66 CREATE PROCEDURE update_toplist()
68 DECLARE deadlock INT DEFAULT 0;
69 DECLARE attempts INT DEFAULT 0;
71 try_loop:WHILE (attempts<3) DO
73 DECLARE deadlock_detected CONDITION FOR 1213;
74 DECLARE EXIT HANDLER FOR deadlock_detected
83 INSERT INTO toplist (SELECT a.podcast_id, COUNT(*) AS count_subscription
84 FROM (SELECT DISTINCT podcast_id, user_id
85 FROM public_subscription) a
93 SET attempts=attempts+1;
98 call FAIL('Toplist is not updated!');
104 DROP VIEW IF EXISTS current_subscription;
106 CREATE VIEW current_subscription AS SELECT device_id, podcast_id, c.user_ptr_id AS user_id, a.timestamp as subscribed_since, sum(a.action) as summe
107 FROM (subscription_log a JOIN device b on a.device_id=b.id)
108 JOIN user c on b.user_id=c.user_ptr_id
109 GROUP BY a.podcast_id, b.user_id
114 create index url_index on podcast ( url (50) );
115 create index title_index on podcast ( title (50) );
116 create index last_update_index on podcast (last_update);
119 create index user_ptr_index on user (user_ptr_id);
120 create index public_profile_index on user (public_profile);
123 create index name_index on device (name);
124 create index user_index on device (user_id);
125 create index type_index on device (type);
126 create index uid_index on device (uid);
127 create index sync_g_index on device (sync_group_id);
130 create index podcast_index on subscription_log(podcast_id);
131 create index action_index on subscription_log(action);
132 create index timestamp_index on subscription_log(timestamp);
133 create index device_index on subscription_log(device_id);
135 -- converting the column subscription_log.action from varchar to tinyint(1)
136 alter table subscription_log add column action_tmp tinyint(1);
137 update subscription_log set action_tmp = 1 where action = 'subscribe';
138 update subscription_log set action_tmp = -1 where action = 'unsubscribe';
139 alter table subscription_log drop column action;
140 alter table subscription_log change action_tmp action tinyint(1);