Beispiel #1
0
def test_create_functions(temp_db_cursor, conn, def_config, sql_tmp_path):
    sqlfile = sql_tmp_path / 'functions.sql'
    sqlfile.write_text("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
                          AS $$
                          BEGIN
                            RETURN 43;
                          END;
                          $$ LANGUAGE plpgsql IMMUTABLE;
                       """)

    create_functions(conn, def_config)

    assert temp_db_cursor.scalar('SELECT test()') == 43
    def test_create_functions(self, temp_db_cursor):
        self.write_functions(
            """CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
                              AS $$
                              BEGIN
                                RETURN 43;
                              END;
                              $$ LANGUAGE plpgsql IMMUTABLE;
                           """)

        create_functions(self.conn, self.config)

        assert temp_db_cursor.scalar('SELECT test()') == 43
Beispiel #3
0
def migrate(config, paths):
    """ Check for the current database version and execute migrations,
        if necesssary.
    """
    with connect(config.get_libpq_dsn()) as conn:
        if conn.table_exists('nominatim_properties'):
            db_version_str = properties.get_property(conn, 'database_version')
        else:
            db_version_str = None

        if db_version_str is not None:
            parts = db_version_str.split('.')
            db_version = tuple(
                [int(x) for x in parts[:2] + parts[2].split('-')])

            if db_version == NOMINATIM_VERSION:
                LOG.warning("Database already at latest version (%s)",
                            db_version_str)
                return 0

            LOG.info("Detected database version: %s", db_version_str)
        else:
            db_version = _guess_version(conn)

        has_run_migration = False
        for version, func in _MIGRATION_FUNCTIONS:
            if db_version <= version:
                LOG.warning("Runnning: %s (%s)",
                            func.__doc__.split('\n', 1)[0],
                            '{0[0]}.{0[1]}.{0[2]}-{0[3]}'.format(version))
                kwargs = dict(conn=conn, config=config, paths=paths)
                func(**kwargs)
                conn.commit()
                has_run_migration = True

        if has_run_migration:
            LOG.warning('Updating SQL functions.')
            refresh.create_functions(conn, config)
            tokenizer = tokenizer_factory.get_tokenizer_for_db(config)
            tokenizer.update_sql_functions(config)

        properties.set_property(
            conn, 'database_version',
            '{0[0]}.{0[1]}.{0[2]}-{0[3]}'.format(NOMINATIM_VERSION))

        conn.commit()

    return 0
    def test_create_functions_with_template(self, temp_db_cursor, dbg, ret):
        self.write_functions(
            """CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
                              AS $$
                              BEGIN
                                {% if debug %}
                                RETURN 43;
                                {% else %}
                                RETURN 22;
                                {% endif %}
                              END;
                              $$ LANGUAGE plpgsql IMMUTABLE;
                           """)

        create_functions(self.conn, self.config, enable_debug=dbg)

        assert temp_db_cursor.scalar('SELECT test()') == ret
Beispiel #5
0
def test_create_functions_with_template(temp_db_cursor, conn, def_config, sql_tmp_path,
                                        dbg, ret):
    sqlfile = sql_tmp_path / 'functions.sql'
    sqlfile.write_text("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
                          AS $$
                          BEGIN
                            {% if debug %}
                            RETURN 43;
                            {% else %}
                            RETURN 22;
                            {% endif %}
                          END;
                          $$ LANGUAGE plpgsql IMMUTABLE;
                       """)

    create_functions(conn, def_config, enable_debug=dbg)

    assert temp_db_cursor.scalar('SELECT test()') == ret