Exemple #1
0
    def postgresql_factory(request):
        """
        Fixture factory for PostgreSQL.

        :param FixtureRequest request: fixture request object
        :rtype: psycopg2.connection
        :returns: postgresql client
        """
        config = get_config(request)
        if not psycopg2:
            raise ImportError(
                'No module named psycopg2. Please install either '
                'psycopg2 or psycopg2-binary package for CPython '
                'or psycopg2cffi for Pypy.')
        proc_fixture = request.getfixturevalue(process_fixture_name)

        # _, config = try_import('psycopg2', request)
        pg_host = proc_fixture.host
        pg_port = proc_fixture.port
        pg_user = proc_fixture.user
        pg_options = proc_fixture.options
        pg_db = db_name or config['dbname']

        with DatabaseJanitor(pg_user, pg_host, pg_port, pg_db,
                             proc_fixture.version):
            connection = psycopg2.connect(dbname=pg_db,
                                          user=pg_user,
                                          host=pg_host,
                                          port=pg_port,
                                          options=pg_options)
            yield connection
            connection.close()
Exemple #2
0
 def version(self):
     """Get postgresql's version."""
     if not self._version:
         with psycopg2.connect(dbname='postgres',
                               user=self.user,
                               host=self.host,
                               port=self.port,
                               options=self.options) as connection:
             version = str(connection.server_version)
             self._version = parse_version('.'.join([
                 version[i:i + 2] for i in range(0, len(version), 2)
                 if int(version[i:i + 2])
             ]))
     return self._version
Exemple #3
0
 def version(self):
     """Get postgresql's version."""
     if not self._version:
         with psycopg2.connect(dbname='postgres',
                               user=self.user,
                               host=self.host,
                               port=self.port,
                               password=self.password,
                               options=self.options) as connection:
             version = str(connection.server_version)
             # Pad the version for releases before 10
             # if not we get 90524 instead of 090524
             if len(version) < 6:
                 version = '0' + version
             self._version = parse_version('.'.join([
                 version[i:i + 2] for i in range(0, len(version), 2)
                 if int(version[i:i + 2])
             ][:2]))
     return self._version