trustieforge/db/migrate/20131107073302_stored_cours...

64 lines
1.6 KiB
Ruby

class StoredCourseProcess < ActiveRecord::Migration
def up
execute "
CREATE PROCEDURE sp_course_cursor ()
BEGIN
DECLARE course_uid int(10);
DECLARE _done tinyint(1) DEFAULT 0;
DECLARE cur_course CURSOR FOR
SELECT
`projects`.id
FROM `projects`
WHERE projects.status <> 9 AND projects.is_public = 1 AND projects.project_type = 1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET _done = 1;
OPEN cur_course;
loop_course:
LOOP
FETCH cur_course INTO course_uid;
IF _done = 1 THEN
LEAVE loop_course;
END IF;
BEGIN
DECLARE members_count int(4) DEFAULT 0;
DECLARE attachments_count int(4) DEFAULT 0;
DECLARE total_count int(4) DEFAULT 0;
SELECT
COUNT(*) INTO members_count
FROM `members` inner join `users`
WHERE `members`.`project_id` = course_uid AND `members`.`user_id` = `users`.`id` AND `users`.`type` = 'User' AND `users`.`status` = 1;
SELECT
COUNT(*) INTO attachments_count
FROM `attachments`
WHERE `attachments`.`container_id` = course_uid AND `attachments`.`container_type` = 'Project';
SET total_count = members_count + attachments_count;
UPDATE project_statuses
SET course_ac_para = total_count
WHERE project_statuses.project_id = course_uid;
COMMIT;
END;
END LOOP;
END;
"
execute "
CREATE EVENT IF NOT EXISTS e_project_status_course
ON SCHEDULE EVERY 1 DAY STARTS '2013-08-27 01:50:00'
ON COMPLETION PRESERVE
DO CALL sp_course_cursor();
"
execute "
SET GLOBAL event_scheduler = ON;
"
end
def down
execute " DROP PROCEDURE IF EXISTS sp_course_cursor;
"
execute "
DROP EVENT IF EXISTS e_project_status_course;
"
end
end