]> de.git.xonotic.org Git - xonotic/xonstatdb.git/blob - functions/merge_servers.sql
Add TKA to the full build.
[xonotic/xonstatdb.git] / functions / merge_servers.sql
1 create or replace function merge_servers(p_winner_server_id servers.server_id%TYPE, p_loser_server_id servers.server_id%TYPE) 
2 RETURNS void as
3 $$
4 declare
5     rowcount integer;
6     w_server record;
7     l_server record;
8 begin
9     raise notice 'Merging servers % and %', p_winner_server_id, p_loser_server_id;
10
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;
13
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.';
18     end if;
19
20     -- fill in the hashkey (everything else will be handled by xonstat)
21     if w_server.hashkey is null and l_server.hashkey is not null then
22         w_server.hashkey := l_server.hashkey;
23     end if;
24
25     -- games get moved to the new server
26     update games set server_id = p_winner_server_id where server_id = p_loser_server_id;
27
28     get diagnostics rowcount = ROW_COUNT;
29     raise notice '% game rows updated.', rowcount;
30
31     -- update attributes rescued from the losing server
32     update servers set
33         ip_addr = w_server.ip_addr,
34         hashkey = w_server.hashkey,
35         revision = w_server.revision
36     where server_id = p_winner_server_id;
37
38     -- now deactivate the old server
39     update servers set active_ind = false where server_id = p_loser_server_id;
40
41     raise notice 'Server #% deactivated.', p_loser_server_id;
42
43     insert into merged_servers(winning_server_id, losing_server_id) values(p_winner_server_id, p_loser_server_id);
44 end;
45 $$
46 language plpgsql;