Example #1
0
def _create_postgresql_extension(configuration, extension):
    """Create a postgresql extension in a previously created database.

    Parameters
    ----------
    configuration: _pytest.config.Config
        The configuration object used by pytest
    extension: str
        Name of the extension to be created

    """

    connection = psycopg2.connect(
        database=configuration.getoption("--database-name-postgresql"),
        user=configuration.getoption("--database-user-postgresql"),
        password=configuration.getoption("--database-password-postgresql"),
        host=configuration.getoption("--database-host-postgresql"),
        port=configuration.getoption("--database-port-postgresql")
    )
    connection.set_isolation_level(
        psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
    cursor = connection.cursor()
    cursor.execute("CREATE EXTENSION {0}".format(extension))
    cursor.close()
    connection.close()
Example #2
0
def _create_postgresql_extension(configuration, extension):
    """Create a postgresql extension in a previously created database.

    Parameters
    ----------
    configuration: _pytest.config.Config
        The configuration object used by pytest
    extension: str
        Name of the extension to be created

    """

    connection = psycopg2.connect(
        database=configuration.getoption("--database-name-postgresql"),
        user=configuration.getoption("--database-user-postgresql"),
        password=configuration.getoption("--database-password-postgresql"),
        host=configuration.getoption("--database-host-postgresql"),
        port=configuration.getoption("--database-port-postgresql")
    )
    connection.set_isolation_level(
        psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
    cursor = connection.cursor()
    cursor.execute("CREATE EXTENSION {0}".format(extension))
    cursor.close()
    connection.close()
Example #3
0
def _recreate_postgresql_database(configuration):
    """Recreate a postgresql database.

    This function will try to create a new postgresql database for testing
    purposes. If the database already exists it is deleted and then recreated.

    Parameters
    ----------
    configuration: _pytest.config.Config
        The configuration object used by pytest

    Raises
    ------
    RuntimeError
        If a connection to the postgresql server cannot be made

    """

    connection = psycopg2.connect(
        database="postgres",
        user=configuration.getoption("--database-user-postgresql"),
        password=configuration.getoption("--database-password-postgresql"),
        host=configuration.getoption("--database-host-postgresql"),
        port=configuration.getoption("--database-port-postgresql")
    )
    connection.set_isolation_level(
        psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
    cursor = connection.cursor()
    db_name = configuration.getoption("--database-name-postgresql")
    cursor.execute("DROP DATABASE IF EXISTS {database}".format(
        database=db_name))
    cursor.execute("CREATE DATABASE {database}".format(database=db_name))
    cursor.execute(
        "SELECT COUNT(1) FROM pg_available_extensions WHERE name='postgis'")
    postgis_available = bool(cursor.fetchone()[0])
    cursor.close()
    connection.close()
    if postgis_available:
        _create_postgresql_extension(configuration, extension="postgis")
    else:
        _create_postgresql_extension(configuration, extension="plpythonu")
Example #4
0
def _recreate_postgresql_database(configuration):
    """Recreate a postgresql database.

    This function will try to create a new postgresql database for testing
    purposes. If the database already exists it is deleted and then recreated.

    Parameters
    ----------
    configuration: _pytest.config.Config
        The configuration object used by pytest

    Raises
    ------
    RuntimeError
        If a connection to the postgresql server cannot be made

    """

    connection = psycopg2.connect(
        database="postgres",
        user=configuration.getoption("--database-user-postgresql"),
        password=configuration.getoption("--database-password-postgresql"),
        host=configuration.getoption("--database-host-postgresql"),
        port=configuration.getoption("--database-port-postgresql")
    )
    connection.set_isolation_level(
        psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
    cursor = connection.cursor()
    db_name = configuration.getoption("--database-name-postgresql")
    cursor.execute("DROP DATABASE IF EXISTS {database}".format(
        database=db_name))
    cursor.execute("CREATE DATABASE {database}".format(database=db_name))
    cursor.execute(
        "SELECT COUNT(1) FROM pg_available_extensions WHERE name='postgis'")
    postgis_available = bool(cursor.fetchone()[0])
    cursor.close()
    connection.close()
    if postgis_available:
        _create_postgresql_extension(configuration, extension="postgis")
    else:
        _create_postgresql_extension(configuration, extension="plpythonu")
Example #5
0
def _repository_exists(repository_url, table_name):
    """Test if the database already exists.

    Parameters
    ----------
    repository_url: str
        URL for the repository, as used by SQLAlchemy engines
    table_name: str
        Name of the table that is to be used to store pycsw records

    Returns
    -------
    bool
        Whether the repository exists or not.

    """

    if repository_url.startswith("sqlite"):
        repository_path = repository_url.replace("sqlite:///", "")
        result = os.path.isfile(repository_path)
    elif repository_url.startswith("postgresql"):
        db_info = _parse_postgresql_repository_url(repository_url)
        try:
            connection = psycopg2.connect(user=db_info["user"],
                                          password=db_info["password"],
                                          host=db_info["host"],
                                          port=db_info["port"],
                                          database=db_info["database"])
            cursor = connection.cursor()
            cursor.execute("SELECT COUNT(1) FROM {table_name}".format(
                table_name=table_name))
        except (psycopg2.OperationalError, psycopg2.ProgrammingError):
            # database or table does not exist yet
            result = False
        else:
            result = True
    else:
        raise NotImplementedError
    return result
Example #6
0
def _repository_exists(repository_url, table_name):
    """Test if the database already exists.

    Parameters
    ----------
    repository_url: str
        URL for the repository, as used by SQLAlchemy engines
    table_name: str
        Name of the table that is to be used to store pycsw records

    Returns
    -------
    bool
        Whether the repository exists or not.

    """

    if repository_url.startswith("sqlite"):
        repository_path = repository_url.replace("sqlite:///", "")
        result = os.path.isfile(repository_path)
    elif repository_url.startswith("postgresql"):
        db_info = _parse_postgresql_repository_url(repository_url)
        try:
            connection = psycopg2.connect(user=db_info["user"],
                                          password=db_info["password"],
                                          host=db_info["host"],
                                          port=db_info["port"],
                                          database=db_info["database"])
            cursor = connection.cursor()
            cursor.execute("SELECT COUNT(1) FROM {table_name}".format(
                table_name=table_name))
        except (psycopg2.OperationalError, psycopg2.ProgrammingError):
            # database or table does not exist yet
            result = False
        else:
            result = True
    else:
        raise NotImplementedError
    return result