Esempio n. 1
0
    def test_sql(self):
        db = sm()

        try:
            db.query(SearchCharacter).first()
        finally:
            db.close()
            del db
Esempio n. 2
0
    def test_sql(self):
        db = sm()

        try:
            db.query(SearchCharacter).first()
        finally:
            db.close()
            del db
Esempio n. 3
0
def init_db():

    inspector = Inspector.from_engine(engine)
    if "alembic_version" in inspector.get_table_names():
        raise Exception("Database has already been initialised. Use \"alembic upgrade head\" instead.")

    engine.echo = True
    Base.metadata.create_all(bind=engine)

    alembic_cfg = Config(os.path.dirname(os.path.realpath(__file__)) + "/../../alembic.ini")
    command.stamp(alembic_cfg, "head")

    # Initialise search characters if necessary.
    db = sm()
    try:
        special_other_fandom = db.query(Fandom).filter(Fandom.id == 1).one()
        print("Special/other fandom found.")
    except NoResultFound:
        print("Special/other fandom not found, creating.")
        special_other_fandom = Fandom(name="Special/other")
        db.add(special_other_fandom)
        db.flush()
    try:
        special_other_group = db.query(SearchCharacterGroup).filter(SearchCharacterGroup.id == 1).one()
        print("Special/other group found.")
    except NoResultFound:
        print("Special/other group not found, creating.")
        special_other_group = SearchCharacterGroup(name="Special/other", fandom=special_other_fandom, order=1)
        db.add(special_other_group)
        db.flush()
    try:
        anonymous_other = db.query(SearchCharacter).filter(SearchCharacter.id == 1).one()
        print("Anon/other character found.")
    except NoResultFound:
        print("Anon/other character not found, creating.")
        special_other = SearchCharacter(title="Anonymous/other", group=special_other_group, order=1)
        db.add(special_other)
        db.flush()
    db.commit()

    # Initalise admin tiers if there are no tiers
    if not db.query(AdminTier).scalar():
        supertier = AdminTier(
            name='Hoofbeast tier'
        )
        db.add(supertier)
        db.commit()

        for permission in AdminTierPermission.permission.property.columns[0].type.enums:
            db.add(AdminTierPermission(
                admin_tier_id=supertier.id,
                permission=permission
            ))

        db.commit()
def session_scope():
    """Provide a transactional scope around a series of operations."""
    session = sm()
    try:
        yield session
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()
Esempio n. 5
0
def session_scope():
    """Provide a transactional scope around a series of operations."""
    session = sm()
    try:
        yield session
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()
def db_connect():
    if not hasattr(g, "db"):
        g.db = sm()
Esempio n. 7
0
 def db(self):
     if hasattr(self, "_db") and self._db is not None:
         return self._db
     else:
         self._db = sm()
         return self._db
Esempio n. 8
0
 def db(self):
     if hasattr(self, "_db") and self._db is not None:
         return self._db
     else:
         self._db = sm()
         return self._db
Esempio n. 9
0
    time.sleep(random.uniform(1, 2))

    if redis.get("lock:initdb") == lock:
        print("Got the init lock, initating database.")
        subprocess.call([
            "python3",
            os.path.dirname(os.path.realpath(__file__)) +
            "/../newparp/model/init_db.py"
        ])
    else:
        print("Didn't get the init lock, waiting 5 seconds.")
        time.sleep(5)


try:
    db = sm()
    redis = NewparpRedis(connection_pool=redis_pool)

    # Redis online check.
    while True:
        try:
            redis.info()
            break
        except RedisError as e:
            print("Encountered a Redis error, waiting a second.")
            print(str(e))
            time.sleep(1)

    # Database online and initation check.
    while True:
        try:
Esempio n. 10
0
def db():
    return sm()
Esempio n. 11
0
    lock = str(uuid.uuid4())
    redis.setex("lock:initdb", 60, lock)

    time.sleep(random.uniform(1, 2))

    if redis.get("lock:initdb") == lock:
        print("Got the init lock, initating database.")
        subprocess.call(["python3", os.path.dirname(os.path.realpath(__file__)) + "/../newparp/model/init_db.py"])
    else:
        print("Didn't get the init lock, waiting 5 seconds.")
        time.sleep(5)


try:
    db = sm()
    redis = NewparpRedis(connection_pool=redis_pool)

    # Redis online check.
    while True:
        try:
            redis.info()
            break
        except RedisError as e:
            print("Encountered a Redis error, waiting a second.")
            print(str(e))
            time.sleep(1)

    # Database online and initation check.
    while True:
        try:
Esempio n. 12
0
def db_connect():
    if not hasattr(g, "db"):
        g.db = sm()