summaryrefslogtreecommitdiff
path: root/bin/rtt-archive-backup
blob: 88e19b1908c8f1b4fea40f6bf8b9b4ad414fc871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash

sqlite3 archive.db < tools/archive-schema.sql

sqlite3 <<EOF

-- Make a copy of finished games in a separate archive database.

attach database 'db' as live;
attach database 'archive.db' as archive;

pragma live.busy_timeout=10000;

-- List finished (and rated) games in live that are not in archive.
create temporary view candidates as
	select
		game_id
	from
		live.rated_games_view
	where
		game_id not in (select game_id from archive.games)
;

begin immediate;

select 'ARCHIVING ' || count(*) || ' GAMES' from candidates;

insert or ignore into archive.users (user_id, name) select user_id, name
	from live.users;

insert into archive.players (game_id, role, user_id)
	select game_id, role, user_id
	from candidates join live.players using(game_id);

insert into archive.game_state (game_id, state)
	select game_id, state
	from candidates join live.game_state using(game_id);

insert into archive.game_replay (game_id, replay_id, role, action, arguments)
	select game_id, replay_id, role, action, arguments
	from candidates join live.game_replay using(game_id);

insert into archive.game_chat (game_id, chat_id, user_id, time, message)
	select game_id, chat_id, user_id, time, message
	from candidates join live.game_chat using(game_id);

insert into archive.games (game_id, title_id, scenario, options, player_count, ctime, mtime, moves, result)
	select game_id, title_id, scenario, options, player_count, ctime, mtime, moves, result
	from candidates join live.games using(game_id);

commit;

EOF