Remove Persona since it was shut down. :(
[xonotic/xonstat.git] / xonstat / models / __init__.py
1 """
2 Model initialization and mapping.
3 """
4
5 from sqlalchemy import MetaData
6 from sqlalchemy.ext.declarative import declarative_base
7 from sqlalchemy.orm import scoped_session, sessionmaker, mapper
8
9 from xonstat.models.game import *
10 from xonstat.models.main import *
11 from xonstat.models.map import *
12 from xonstat.models.player import *
13 from xonstat.models.server import *
14
15 DBSession = scoped_session(sessionmaker())
16 Base = declarative_base()
17
18
19 def initialize_db(engine=None):
20     """
21     Initialize the database using reflection.
22
23     :param engine: The SQLAlchemy engine instance to bind.
24     :return: None
25     """
26     DBSession.configure(bind=engine)
27
28     Base.metadata.bind = engine
29     Base.metadata.create_all(engine)
30
31     # Since the schema is actually defined elsewhere, we use reflection to determine the
32     # structure of the models instead of specifying them over again in Python.
33     metadata = MetaData(bind=engine)
34     metadata.reflect()
35
36     # Assign all the tables to objects
37     achievements_table = metadata.tables['achievements']
38     cd_achievement_table = metadata.tables['cd_achievement']
39     cd_game_type_table = metadata.tables['cd_game_type']
40     cd_weapon_table = metadata.tables['cd_weapon']
41     db_version_table = metadata.tables['db_version']
42     games_table = metadata.tables['games']
43     hashkeys_table = metadata.tables['hashkeys']
44     maps_table = metadata.tables['maps']
45     player_game_stats_table = metadata.tables['player_game_stats']
46     players_table = metadata.tables['players']
47     player_weapon_stats_table = metadata.tables['player_weapon_stats']
48     servers_table = metadata.tables['servers']
49     player_nicks_table = metadata.tables['player_nicks']
50     player_elos_table = metadata.tables['player_elos']
51     player_ranks_table = metadata.tables['player_ranks']
52     player_captimes_table = metadata.tables['player_map_captimes']
53     summary_stats_table = metadata.tables['summary_stats']
54     team_game_stats_table = metadata.tables['team_game_stats']
55     player_game_anticheats_table = metadata.tables['player_game_anticheats']
56     player_groups_table = metadata.tables['player_groups']
57     active_players_table = metadata.tables['active_players_mv']
58     active_servers_table = metadata.tables['active_servers_mv']
59     active_maps_table = metadata.tables['active_maps_mv']
60     player_medals_table = metadata.tables['player_medals']
61
62     # Map the tables and the objects together
63     mapper(PlayerAchievement, achievements_table)
64     mapper(Achievement, cd_achievement_table)
65     mapper(GameType, cd_game_type_table)
66     mapper(Weapon, cd_weapon_table)
67     mapper(Game, games_table)
68     mapper(Hashkey, hashkeys_table)
69     mapper(Map, maps_table)
70     mapper(PlayerGameStat, player_game_stats_table)
71     mapper(Player, players_table)
72     mapper(PlayerWeaponStat, player_weapon_stats_table)
73     mapper(Server, servers_table)
74     mapper(PlayerNick, player_nicks_table)
75     mapper(PlayerElo, player_elos_table)
76     mapper(PlayerRank, player_ranks_table)
77     mapper(PlayerCaptime, player_captimes_table)
78     mapper(SummaryStat, summary_stats_table)
79     mapper(TeamGameStat, team_game_stats_table)
80     mapper(PlayerGameAnticheat, player_game_anticheats_table)
81     mapper(PlayerGroups, player_groups_table)
82     mapper(ActivePlayer, active_players_table)
83     mapper(ActiveServer, active_servers_table)
84     mapper(ActiveMap, active_maps_table)
85     mapper(PlayerMedal, player_medals_table)