Example #1
0
def install_legacy_tokenizer(conn, config, **_):
    """ Setup legacy tokenizer.

        If no other tokenizer has been configured yet, then create the
        configuration for the backwards-compatible legacy tokenizer
    """
    if properties.get_property(conn, 'tokenizer') is None:
        with conn.cursor() as cur:
            for table in ('placex', 'location_property_osmline'):
                has_column = cur.scalar("""SELECT count(*) FROM information_schema.columns
                                           WHERE table_name = %s
                                           and column_name = 'token_info'""",
                                        (table, ))
            if has_column == 0:
                cur.execute('ALTER TABLE {} ADD COLUMN token_info JSONB'.format(table))
        tokenizer = tokenizer_factory.create_tokenizer(config, init_db=False,
                                                       module_name='legacy')

        tokenizer.migrate_database(config)
Example #2
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)
                has_run_migration = True

        if has_run_migration:
            LOG.warning('Updating SQL functions.')
            refresh.create_functions(conn, config, paths.sqllib_dir)

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

        conn.commit()

    return 0
def test_update_sql_functions(sql_preprocessor, temp_db_conn,
                              tokenizer_factory, test_config, table_factory,
                              monkeypatch, temp_db_cursor):
    monkeypatch.setenv('NOMINATIM_MAX_WORD_FREQUENCY', '1133')
    monkeypatch.setattr(legacy_tokenizer, '_check_module' , lambda m, c: None)
    tok = tokenizer_factory()
    tok.init_new_db(test_config)
    monkeypatch.undo()

    assert properties.get_property(temp_db_conn, legacy_tokenizer.DBCFG_MAXWORDFREQ) == '1133'

    table_factory('test', 'txt TEXT')

    func_file = test_config.lib_dir.sql / 'tokenizer' / 'legacy_tokenizer.sql'
    func_file.write_text("""INSERT INTO test VALUES ('{{max_word_freq}}'),
                                                   ('{{modulepath}}')""")

    tok.update_sql_functions(test_config)

    test_content = temp_db_cursor.row_set('SELECT * FROM test')
    assert test_content == set((('1133', ), (str(test_config.project_dir / 'module'), )))
Example #4
0
def test_get_property_unknown(prop_table, temp_db_conn, temp_db_cursor):
    temp_db_cursor.execute(
        "INSERT INTO nominatim_properties VALUES('other', 'bar')")

    assert properties.get_property(temp_db_conn, 'foo') is None
Example #5
0
def test_get_property_existing(prop_table, temp_db_conn, temp_db_cursor):
    temp_db_cursor.execute(
        "INSERT INTO nominatim_properties VALUES('foo', 'bar')")

    assert properties.get_property(temp_db_conn, 'foo') == 'bar'
Example #6
0
 def _get_db_property(name):
     return properties.get_property(temp_db_conn, name)
Example #7
0
def test_get_property_unknown(property_factory, temp_db_conn):
    property_factory('other', 'bar')

    assert properties.get_property(temp_db_conn, 'foo') is None
Example #8
0
def test_get_property_existing(property_factory, temp_db_conn):
    property_factory('foo', 'bar')

    assert properties.get_property(temp_db_conn, 'foo') == 'bar'
Example #9
0
 def init_from_project(self, _):
     """ Initialise the tokenizer from the project directory.
     """
     with connect(self.dsn) as conn:
         self.normalization = properties.get_property(
             conn, DBCFG_NORMALIZATION)
Example #10
0
 def _get_db_property(name):
     return properties.get_property(temp_db_conn,
                                    getattr(legacy_icu_tokenizer, name))
Example #11
0
 def get(self, name):
     """ Set a property in the table to the given value.
     """
     return properties.get_property(self.conn, name)