예제 #1
0
 def test_psycopg2_nonempty_connection_string_w_query_two(self):
     dialect = psycopg2_dialect.dialect()
     url_string = "postgresql://*****:*****@/DB?host=hostA"
     u = url.make_url(url_string)
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [])
     eq_(cparams["host"], "hostA")
예제 #2
0
 def test_psycopg2_nonempty_connection_string_w_query_three(self):
     dialect = psycopg2_dialect.dialect()
     url_string = ("postgresql://*****:*****@/DB"
                   "?host=hostA:portA&host=hostB&host=hostC")
     u = url.make_url(url_string)
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [])
     eq_(cparams["host"], "hostA:portA,hostB,hostC")
예제 #3
0
 def test_psycopg2_nonempty_connection_string_w_query(self):
     dialect = psycopg2_dialect.dialect()
     u = url.make_url("postgresql://somehost/?any_random_thing=yes")
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [])
     eq_(cparams, {"host": "somehost", "any_random_thing": "yes"})
예제 #4
0
 def test_psycopg2_empty_connection_string_w_query_one(self):
     dialect = psycopg2_dialect.dialect()
     u = url.make_url("postgresql:///?service=swh-log")
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [])
     eq_(cparams, {"service": "swh-log"})
예제 #5
0
 def test_psycopg2_nonempty_connection_string(self):
     dialect = psycopg2_dialect.dialect()
     u = url.make_url("postgresql://host")
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [])
     eq_(cparams, {"host": "host"})
예제 #6
0
 def test_psycopg2_nonempty_connection_string_w_query(self):
     dialect = psycopg2_dialect.dialect()
     u = url.make_url("postgresql://somehost/?any_random_thing=yes")
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [])
     eq_(cparams, {"host": "somehost", "any_random_thing": "yes"})
예제 #7
0
 def test_psycopg2_empty_connection_string_w_query_one(self):
     dialect = psycopg2_dialect.dialect()
     u = url.make_url("postgresql:///?service=swh-log")
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [])
     eq_(cparams, {"service": "swh-log"})
예제 #8
0
 def test_psycopg2_nonempty_connection_string(self):
     dialect = psycopg2_dialect.dialect()
     u = url.make_url("postgresql://host")
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [])
     eq_(cparams, {"host": "host"})
예제 #9
0
 def test_psycopg2_empty_connection_string_w_query_two(self):
     dialect = psycopg2_dialect.dialect()
     u = url.make_url("postgresql+psycopg2:///?any_random_thing=yes")
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [])
     eq_(cparams, {"any_random_thing": "yes"})
예제 #10
0
 def test_psycopg2_empty_connection_string(self):
     dialect = psycopg2_dialect.dialect()
     u = url.make_url("postgresql+psycopg2://")
     cargs, cparams = dialect.create_connect_args(u)
     eq_(cargs, [""])
     eq_(cparams, {})
예제 #11
0
from sqlalchemy.orm import sessionmaker
from sqlalchemy.dialects.postgresql import psycopg2

from config import settings
from db.engines import ENGINES, DEFAULT_POOL_SIZE, DEFAULT_MAX_OVERFLOW
from db.sessions import MasterSlaveSession

logger = structlog.getLogger('daemon.' + __name__)

db_factory = sessionmaker(class_=MasterSlaveSession,
                          engines=ENGINES,
                          autoflush=True)
SET_APPLICATION_NAME = settings.db.get('set_application_name', True)

# asyncpgsa has default dialect that do not contain `on_conflic_*` statement
DEFAULT_DIALECT = psycopg2.dialect(json_serializer=ujson.dumps,
                                   json_deserializer=ujson.loads)
DEFAULT_DIALECT.implicit_returning = True
DEFAULT_DIALECT.use_native_hstore = True


class ConnectionManager:
    """Database connection manager."""

    __slots__ = ('_connection_class', '_loop', '_dsn', '_connection',
                 '_statement_cache_size')

    def __init__(self, loop=None, statement_cache_size=0):
        """Initialize ConnectionManager instance."""
        class SynctoolConnection(SAConnection):
            def __init__(self, *args, **kwargs):
                super().__init__(*args, dialect=DEFAULT_DIALECT, **kwargs)