Example #1
0
def database(env, tell_sentry):
    dburl = env.database_url
    maxconn = env.database_maxconn
    try:
        db = DB(dburl, maxconn=maxconn)
    except psycopg2.OperationalError as e:
        tell_sentry(e, {})
        pg_dir = os.environ.get('OPENSHIFT_PG_DATA_DIR')
        if pg_dir:
            # We know where the postgres data is, try to start the server ourselves
            r = call(['pg_ctl', '-D', pg_dir, 'start', '-w', '-t', '15'])
            if r == 0:
                return database(env, tell_sentry)
        db = NoDB()

    models = (
        _AccountElsewhere,
        AccountElsewhere,
        _Community,
        Community,
        ExchangeRoute,
        Participant,
        Repository,
    )
    for model in models:
        db.register_model(model)
    liberapay.billing.payday.Payday.db = db

    use_qc = not env.override_query_cache
    qc1 = QueryCache(db, threshold=(1 if use_qc else 0))
    qc5 = QueryCache(db, threshold=(5 if use_qc else 0))

    return {'db': db, 'db_qc1': qc1, 'db_qc5': qc5}
Example #2
0
def database(env, tell_sentry):
    dburl = env.database_url
    maxconn = env.database_maxconn
    try:
        db = DB(dburl, maxconn=maxconn)
    except psycopg2.OperationalError as e:
        tell_sentry(e, {})
        pg_dir = os.environ.get('OPENSHIFT_PG_DATA_DIR')
        if pg_dir:
            # We know where the postgres data is, try to start the server ourselves
            r = call(['pg_ctl', '-D', pg_dir, 'start', '-w', '-t', '15'])
            if r == 0:
                return database(env, tell_sentry)
        db = NoDB()

    models = (
        _AccountElsewhere, AccountElsewhere, _Community, Community,
        ExchangeRoute, Participant, Repository,
    )
    for model in models:
        db.register_model(model)
    liberapay.billing.payday.Payday.db = db

    def adapt_money(m):
        return AsIs('(%s,%s)::currency_amount' % (adapt(m.amount), adapt(m.currency)))
    register_adapter(Money, adapt_money)

    def cast_currency_amount(v, cursor):
        return None if v in (None, '(,)') else Money(*v[1:-1].split(','))
    try:
        oid = db.one("SELECT 'currency_amount'::regtype::oid")
        register_type(new_type((oid,), _str('currency_amount'), cast_currency_amount))
    except psycopg2.ProgrammingError:
        pass

    def adapt_money_basket(b):
        return AsIs('(%s,%s)::currency_basket' % (b.amounts['EUR'], b.amounts['USD']))
    register_adapter(MoneyBasket, adapt_money_basket)

    def cast_currency_basket(v, cursor):
        if v is None:
            return None
        eur, usd = v[1:-1].split(',')
        return MoneyBasket(EUR=Decimal(eur), USD=Decimal(usd))
    try:
        oid = db.one("SELECT 'currency_basket'::regtype::oid")
        register_type(new_type((oid,), _str('currency_basket'), cast_currency_basket))
    except psycopg2.ProgrammingError:
        pass

    use_qc = not env.override_query_cache
    qc1 = QueryCache(db, threshold=(1 if use_qc else 0))
    qc5 = QueryCache(db, threshold=(5 if use_qc else 0))

    return {'db': db, 'db_qc1': qc1, 'db_qc5': qc5}
Example #3
0
def database(env, tell_sentry):
    dburl = env.database_url
    maxconn = env.database_maxconn
    try:
        db = DB(dburl, maxconn=maxconn)
    except psycopg2.OperationalError as e:
        tell_sentry(e, {}, allow_reraise=False)
        db = NoDB()
        return {'db': db, 'db_qc1': db, 'db_qc5': db}

    models = (
        _AccountElsewhere, AccountElsewhere, _Community, Community,
        Encrypted, ExchangeRoute, Participant, Repository,
    )
    for model in models:
        db.register_model(model)
    liberapay.billing.payday.Payday.db = db

    def adapt_set(s):
        return adapt(tuple(s))
    register_adapter(set, adapt_set)

    def adapt_money(m):
        return AsIs('(%s,%s)::currency_amount' % (adapt(m.amount), adapt(m.currency)))
    register_adapter(Money, adapt_money)

    def cast_currency_amount(v, cursor):
        return None if v in (None, '(,)') else Money(*v[1:-1].split(','))
    try:
        oid = db.one("SELECT 'currency_amount'::regtype::oid")
        register_type(new_type((oid,), 'currency_amount', cast_currency_amount))
    except psycopg2.ProgrammingError:
        pass

    def adapt_money_basket(b):
        return AsIs(
            "_wrap_amounts('%s'::jsonb)" %
            json.dumps({k: str(v) for k, v in b.amounts.items() if v}).replace("'", "''")
        )
    register_adapter(MoneyBasket, adapt_money_basket)

    def cast_currency_basket(v, cursor):
        if v is None:
            return None
        parts = v[1:-1].split(',', 2)
        if len(parts) == 2:
            eur, usd = parts
            obj = None
        else:
            eur, usd, obj = parts
        if obj:
            amounts = json.loads(obj[1:-1].replace('""', '"') if obj[0] == '"' else obj)
            amounts = {k: Decimal(str(v)) for k, v in amounts.items()}
        else:
            amounts = {}
            if eur:
                amounts['EUR'] = Decimal(eur)
            if usd:
                amounts['USD'] = Decimal(usd)
        return MoneyBasket(**amounts)
    try:
        oid = db.one("SELECT 'currency_basket'::regtype::oid")
        register_type(new_type((oid,), 'currency_basket', cast_currency_basket))
    except psycopg2.ProgrammingError:
        pass

    use_qc = not env.override_query_cache
    qc1 = QueryCache(db, threshold=(1 if use_qc else 0))
    qc5 = QueryCache(db, threshold=(5 if use_qc else 0))

    return {'db': db, 'db_qc1': qc1, 'db_qc5': qc5}