Beispiel #1
0
def is_migrated():
    """
    If the database is running the latest migration

    Returns:
        (bool) True if up to date, false otherwise
    """
    migrated = True

    try:
        # @todo rseekely What we really want to do here is test the desired
        # version number against the version number currently in the db. But
        # that doesn't exist at the momment, so for now we'll just see if the
        # game table exists like above.
        db.query("SELECT game_id FROM game LIMIT 1")
    # pylint: disable=no-member
    except psycopg2.errors.UndefinedTable:
        migrated = False

    return migrated
Beispiel #2
0
def get_unprocessed_game_ids():
    """
    Get the ids of games which have unprocessed game events

    Returns:
        (list) of game ids
    """
    rows = db.query(
        "SELECT DISTINCT game_id FROM game_event WHERE processed = 0")
    game_ids = [row["game_id"] for row in rows]

    return game_ids
Beispiel #3
0
def get_unprocessed_game_events(game_id):
    """
    Retrieve the unprocessed game events from a specific game

    Returns:
        (list) of unprocessed game ids
    """
    rows = db.query(
        "SELECT data FROM game_event WHERE processed = 0 AND game_id = %s",
        [game_id])

    events = [json.loads(row["data"]) for row in rows]

    return events
Beispiel #4
0
def migrate(schema="quagen/sql/schema.sql"):
    """
    Installs the database schema (if missing) and runs any outstanding SQL
    migrations.

    Args:
        schema (string): Path to database schema SQL
    """
    logging.info("Running database migrations")

    # If the game table is missing, we'll assume the entire schema is missing.
    # We'd be in a pretty funky state if this wasn't the case.
    try:
        db.query("SELECT game_id FROM game LIMIT 1")
    # pylint: disable=no-member
    except psycopg2.errors.UndefinedTable:
        logging.info("Schema missing, running database init")
        connection = db.get_connection()
        cursor = connection.cursor()
        cursor.execute(open(schema, "r").read())
        connection.commit()

    logging.info("Database up to date")
Beispiel #5
0
def get_game(game_id):
    """
    Retrieves a Game from the database

    Attr:
        game_id (str): Id of the game to retrieve

    Returns:
        Game object
    """
    game = None
    row = db.query("SELECT game_id, data  FROM game WHERE game_id = %s",
                   [game_id], True)

    if row is not None:
        data = json.loads(row["data"])
        game = Game(data)

    return game