2 Model initialization and mapping.
5 from sqlalchemy import MetaData
6 from sqlalchemy.ext.declarative import declarative_base
7 from sqlalchemy.orm import scoped_session, sessionmaker, mapper
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 *
15 DBSession = scoped_session(sessionmaker())
16 Base = declarative_base()
19 def initialize_db(engine=None):
21 Initialize the database using reflection.
23 :param engine: The SQLAlchemy engine instance to bind.
26 DBSession.configure(bind=engine)
28 Base.metadata.bind = engine
29 Base.metadata.create_all(engine)
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)
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 player_game_frag_matrix_table = metadata.tables['player_game_frag_matrix']
62 player_glickos_base_table = metadata.tables['player_glickos_base']
63 player_glickos_current_table = metadata.tables['player_glickos_current']
65 # Map the tables and the objects together
66 mapper(PlayerAchievement, achievements_table)
67 mapper(Achievement, cd_achievement_table)
68 mapper(GameType, cd_game_type_table)
69 mapper(Weapon, cd_weapon_table)
70 mapper(Game, games_table)
71 mapper(Hashkey, hashkeys_table)
72 mapper(Map, maps_table)
73 mapper(PlayerGameStat, player_game_stats_table)
74 mapper(Player, players_table)
75 mapper(PlayerWeaponStat, player_weapon_stats_table)
76 mapper(Server, servers_table)
77 mapper(PlayerNick, player_nicks_table)
78 mapper(PlayerElo, player_elos_table)
79 mapper(PlayerRank, player_ranks_table)
80 mapper(PlayerCaptime, player_captimes_table)
81 mapper(SummaryStat, summary_stats_table)
82 mapper(TeamGameStat, team_game_stats_table)
83 mapper(PlayerGameAnticheat, player_game_anticheats_table)
84 mapper(PlayerGroups, player_groups_table)
85 mapper(ActivePlayer, active_players_table)
86 mapper(ActiveServer, active_servers_table)
87 mapper(ActiveMap, active_maps_table)
88 mapper(PlayerMedal, player_medals_table)
89 mapper(PlayerGameFragMatrix, player_game_frag_matrix_table)
90 mapper(PlayerGlicko, player_glickos_current_table)
91 mapper(PlayerGlickoBase, player_glickos_base_table)