]> git.xonotic.org Git - xonotic/xonstatdb.git/blob - functions/merge_servers.sql
f929064fe58ed21809488d0ec99f433b14d88e3f
[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 "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;
23     end if;
24
25     if w_server.hashkey is null and l_server.hashkey is not null then
26         w_server.hashkey := l_server.hashkey;
27     end if;
28
29     if w_server.revision is null and l_server.revision is not null then
30         w_server.revision := l_server.revsion;
31     end if;
32
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;
35
36     get diagnostics rowcount = ROW_COUNT;
37     raise notice '% game rows updated.', rowcount;
38
39     -- update attributes rescued from the losing server
40     update servers set
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;
45
46     -- now deactivate the old server
47     update servers set active_ind = false where server_id = p_loser_server_id;
48
49     raise notice 'Server #% deactivated.', p_loser_server_id;
50 end;
51 $$
52 language plpgsql;