2 Functions for bringing auxiliary data in the database up-to-date.
7 from psycopg2.extras import execute_values
9 from ..db.utils import execute_file
11 def update_postcodes(conn, sql_dir):
12 """ Recalculate postcode centroids and add, remove and update entries in the
13 location_postcode table. `conn` is an opne connection to the database.
15 execute_file(conn, sql_dir / 'update-postcodes.sql')
18 def recompute_word_counts(conn, sql_dir):
19 """ Compute the frequency of full-word search terms.
21 execute_file(conn, sql_dir / 'words_from_search_name.sql')
24 def _add_address_level_rows_from_entry(rows, entry):
25 """ Converts a single entry from the JSON format for address rank
26 descriptions into a flat format suitable for inserting into a
27 PostgreSQL table and adds these lines to `rows`.
29 countries = entry.get('countries') or (None, )
30 for key, values in entry['tags'].items():
31 for value, ranks in values.items():
32 if isinstance(ranks, list):
33 rank_search, rank_address = ranks
35 rank_search = rank_address = ranks
38 for country in countries:
39 rows.append((country, key, value, rank_search, rank_address))
41 def load_address_levels(conn, table, levels):
42 """ Replace the `address_levels` table with the contents of `levels'.
44 A new table is created any previously existing table is dropped.
45 The table has the following columns:
46 country, class, type, rank_search, rank_address
50 _add_address_level_rows_from_entry(rows, entry)
52 with conn.cursor() as cur:
53 cur.execute('DROP TABLE IF EXISTS {}'.format(table))
55 cur.execute("""CREATE TABLE {} (country_code varchar(2),
59 rank_address SMALLINT)""".format(table))
61 execute_values(cur, "INSERT INTO {} VALUES %s".format(table), rows)
63 cur.execute('CREATE UNIQUE INDEX ON {} (country_code, class, type)'.format(table))
67 def load_address_levels_from_file(conn, config_file):
68 """ Replace the `address_levels` table with the contents of the config
71 with config_file.open('r') as fdesc:
72 load_address_levels(conn, 'address_levels', json.load(fdesc))
74 PLPGSQL_BASE_MODULES = (
83 PLPGSQL_TABLE_MODULES = (
84 ('place', 'place_triggers.sql'),
85 ('placex', 'placex_triggers.sql'),
86 ('location_postcode', 'postcode_triggers.sql')
89 def _get_standard_function_sql(conn, config, sql_dir, enable_diff_updates, enable_debug):
90 """ Read all applicable SQLs containing PL/pgSQL functions, replace
91 placefolders and execute them.
93 sql_func_dir = sql_dir / 'functions'
96 # Get the basic set of functions that is always imported.
97 for sql_file in PLPGSQL_BASE_MODULES:
98 with (sql_func_dir / sql_file).open('r') as fdesc:
101 # Some files require the presence of a certain table
102 for table, fname in PLPGSQL_TABLE_MODULES:
103 if conn.table_exists(table):
104 with (sql_func_dir / fname).open('r') as fdesc:
107 # Replace placeholders.
108 sql = sql.replace('{modulepath}',
109 config.DATABASE_MODULE_PATH or str((config.project_dir / 'module').resolve()))
111 if enable_diff_updates:
112 sql = sql.replace('RETURN NEW; -- %DIFFUPDATES%', '--')
115 sql = sql.replace('--DEBUG:', '')
117 if config.get_bool('LIMIT_REINDEXING'):
118 sql = sql.replace('--LIMIT INDEXING:', '')
120 if not config.get_bool('USE_US_TIGER_DATA'):
121 sql = sql.replace('-- %NOTIGERDATA% ', '')
123 if not config.get_bool('USE_AUX_LOCATION_DATA'):
124 sql = sql.replace('-- %NOAUXDATA% ', '')
126 reverse_only = 'false' if conn.table_exists('search_name') else 'true'
128 return sql.replace('%REVERSE-ONLY%', reverse_only)
131 def replace_partition_string(sql, partitions):
132 """ Replace a partition template with the actual partition code.
134 for match in re.findall('^-- start(.*?)^-- end', sql, re.M | re.S):
136 for part in partitions:
137 repl += match.replace('-partition-', str(part))
138 sql = sql.replace(match, repl)
142 def _get_partition_function_sql(conn, sql_dir):
143 """ Create functions that work on partition tables.
145 with conn.cursor() as cur:
146 cur.execute('SELECT distinct partition FROM country_name')
147 partitions = set([0])
149 partitions.add(row[0])
151 with (sql_dir / 'partition-functions.src.sql').open('r') as fdesc:
154 return replace_partition_string(sql, sorted(partitions))
156 def create_functions(conn, config, sql_dir,
157 enable_diff_updates=True, enable_debug=False):
158 """ (Re)create the PL/pgSQL functions.
160 sql = _get_standard_function_sql(conn, config, sql_dir,
161 enable_diff_updates, enable_debug)
162 sql += _get_partition_function_sql(conn, sql_dir)
164 with conn.cursor() as cur: