socialforge/db/migrate/20130828004955_stored_statu...

77 lines
2.2 KiB
Ruby
Raw Normal View History

2013-08-28 09:34:54 +08:00
class StoredStatusProcedure < ActiveRecord::Migration
def up
#sql = <<- END_OF_SQL_CODE
execute "
create procedure `sp_user_status_cursor`()
begin
declare v_uid bigint(22);
declare v int(10);
declare _done TINYINT(1) default 0;
declare cur_user cursor for select user_id, count(*) from changesets where user_id != '' group by user_id;
declare continue handler for not found set _done = 1;
open cur_user;
loop_xxx:loop
fetch cur_user into v_uid,v;
if _done=1 then
leave loop_xxx;
end if;
begin
update user_statuses set changesets_count = v where user_id = v_uid;
commit;
end;
end loop;
end;
"
execute "
create event if not exists e_test
on schedule every 1 day starts'2013_08_27 01:50:00'
on completion preserve
do call `sp_user_status_cursor`();
"
execute "
create procedure `sp_project_status_cursor`()
begin
declare v_uid bigint(22);
declare v int(10);
declare _done TINYINT(1) default 0;
2013-09-16 21:47:46 +08:00
declare cur_user cursor for select project_id,count(*) from (select project_id,repositories.id from repositories inner join changesets where repositories.id = changesets.repository_id and project_id in (SELECT `projects`.id FROM `projects` WHERE (((projects.status <> 9) AND (projects.is_public = 1)))))t group by project_id;
2013-08-28 09:34:54 +08:00
declare continue handler for not found set _done = 1;
open cur_user;
loop_xxx:loop
fetch cur_user into v_uid,v;
if _done=1 then
leave loop_xxx;
end if;
begin
update project_statuses set changesets_count = v where project_id = v_uid;
commit;
end;
end loop;
end;
"
execute "
create event if not exists e_project_status_test
on schedule every 1 day starts'2013_08_27 01:50:00'
on completion preserve
do call `sp_project_status_cursor`();
"
execute "
set global event_scheduler = on;
"
end
def down
execute " drop procedure if exists `sp_user_status_cursor`;
"
execute "
drop event if exists e_test;
"
execute "
drop procedure if exists `sp_project_status_cursor`;
"
execute "
drop event if exists e_project_status_test;
"
end
end