1 create or replace function merge_servers(p_winner_server_id servers.server_id%TYPE, p_loser_server_id servers.server_id%TYPE)
9 raise notice 'Merging servers % and %', p_winner_server_id, p_loser_server_id;
11 select * into w_server from servers where server_id = p_winner_server_id;
12 select * into l_server from servers where server_id = p_loser_server_id;
14 -- hashkey check: if both have hashkeys and they are different, we
15 -- shouldn't merge them!
16 if w_server.hashkey is not null and l_server.hashkey is not null and w_server.hashkey != l_server.hashkey then
17 raise exception 'Both servers have hashkeys and they are different! Not merging.';
20 -- fill in the "important" missing attributes
21 if w_server.ip_addr is null and l_server.ip_addr is not null then
22 w_server.ip_addr := l_server.ip_addr;
25 if w_server.hashkey is null and l_server.hashkey is not null then
26 w_server.hashkey := l_server.hashkey;
29 if w_server.revision is null and l_server.revision is not null then
30 w_server.revision := l_server.revsion;
33 -- games get moved to the new server
34 update games set server_id = p_winner_server_id where server_id = p_loser_server_id;
36 get diagnostics rowcount = ROW_COUNT;
37 raise notice '% game rows updated.', rowcount;
39 -- update attributes rescued from the losing server
41 ip_addr = w_server.ip_addr,
42 hashkey = w_server.hashkey,
43 revision = w_server.revision
44 where server_id = p_winner_server_id;
46 -- now deactivate the old server
47 update servers set active_ind = false where server_id = p_loser_server_id;
49 raise notice 'Server #% deactivated.', p_loser_server_id;