-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsql.rs
384 lines (368 loc) · 10.9 KB
/
sql.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Copyright 2023 Miklos Vajna
*
* SPDX-License-Identifier: MIT
*/
#![deny(warnings)]
#![warn(clippy::all)]
#![warn(missing_docs)]
//! Database schema creation / migration.
use anyhow::Context as _;
pub fn init(conn: &mut rusqlite::Connection) -> anyhow::Result<()> {
let tx = conn.transaction()?;
let user_version: i64 = {
let mut stmt = tx.prepare("pragma user_version")?;
let mut rows = stmt.query([])?;
let row = rows.next()?.context("no row")?;
row.get(0).context("no col")?
};
if user_version < 1 {
tx.execute(
"create table ref_housenumbers (
county_code text not null,
settlement_code text not null,
street text not null,
housenumber text not null,
comment text not null
)",
[],
)?;
tx.execute(
"create index idx_ref_housenumbers
on ref_housenumbers (county_code, settlement_code, street)",
[],
)?;
tx.execute(
"create table ref_streets (
county_code text not null,
settlement_code text not null,
street text not null
)",
[],
)?;
tx.execute(
"create index idx_ref_streets
on ref_streets (county_code, settlement_code)",
[],
)?;
tx.execute(
"create table osm_housenumber_coverages (
relation_name text primary key not null,
coverage text not null,
last_modified text not null
)",
[],
)?;
tx.execute(
"create table osm_street_coverages (
relation_name text primary key not null,
coverage text not null,
last_modified text not null
)",
[],
)?;
tx.execute(
"create table stats_invalid_addr_cities (
osm_id text not null,
osm_type text not null,
postcode text not null,
city text not null,
street text not null,
housenumber text not null,
user text not null
)",
[],
)?;
tx.execute(
"create table mtimes (
page text primary key not null,
last_modified text not null
)",
[],
)?;
}
if user_version < 2 {
tx.execute(
"alter table stats_invalid_addr_cities add column
timestamp text not null default ''",
[],
)?;
tx.execute(
"alter table stats_invalid_addr_cities add column
fixme text not null default ''",
[],
)?;
}
if user_version < 3 {
// Tracks the number of rows in the stats_invalid_addr_cities table over time.
tx.execute(
"create table stats_invalid_addr_cities_counts (
date text primary key not null,
count text not null
)",
[],
)?;
}
if user_version < 4 {
// Tracks the number of OSM house numbers over time.
tx.execute(
"create table stats_counts (
date text primary key not null,
count text not null
)",
[],
)?;
}
if user_version < 5 {
// Tracks the number of OSM house number editors over time.
tx.execute(
"create table stats_usercounts (
date text primary key not null,
count text not null
)",
[],
)?;
}
if user_version < 6 {
// Tracks lint results for a relation.
tx.execute(
"create table relation_lints (
id integer primary key autoincrement,
relation_name text not null,
street_name text not null,
source text not null,
housenumber text not null,
reason text not null
)",
[],
)?;
}
if user_version < 7 {
// OSM link for relation_lints rows.
tx.execute(
"alter table relation_lints add column
object_id text not null default ''",
[],
)?;
tx.execute(
"alter table relation_lints add column
object_type text not null default ''",
[],
)?;
}
if user_version < 8 {
// Tracks house numbers of cities over time.
tx.execute(
"create table stats_citycounts (
date text not null,
city text not null,
count text not null,
unique(date, city)
)",
[],
)?;
}
if user_version < 9 {
// Tracks house numbers of cities over time.
tx.execute(
"create table stats_topusers (
date text not null,
user text not null,
count text not null,
unique(date, user)
)",
[],
)?;
}
if user_version < 10 {
// Tracks house numbers of ZIP areas over time.
tx.execute(
"create table stats_zipcounts (
date text not null,
zip text not null,
count text not null,
unique(date, zip)
)",
[],
)?;
}
if user_version < 11 {
// Tracks streets from OSM for a relation.
tx.execute(
"create table osm_streets (
relation text not null,
osm_id text not null,
name text not null,
highway text not null,
service text not null,
surface text not null,
leisure text not null,
osm_type text not null,
unique(relation, osm_id)
)",
[],
)?;
tx.execute(
"create index idx_osm_streets
on osm_streets (relation)",
[],
)?;
}
if user_version < 12 {
// Tracks housenumbers from OSM for a relation.
tx.execute(
"create table osm_housenumbers (
relation text not null,
osm_id text not null,
street text not null,
housenumber text not null,
postcode text not null,
place text not null,
housename text not null,
conscriptionnumber text not null,
flats text not null,
floor text not null,
door text not null,
unit text not null,
name text not null,
osm_type text not null,
unique(relation, osm_id)
)",
[],
)?;
tx.execute(
"create index idx_osm_housenumbers
on osm_housenumbers (relation)",
[],
)?;
}
if user_version < 13 {
// Tracks the number of additional streets for a relation.
tx.execute_batch(
"create table additional_streets_counts (
relation text not null,
count text not null,
unique(relation)
);
create index idx_additional_streets_counts
on additional_streets_counts(relation);",
)?;
}
if user_version < 14 {
// Tracks the number of additional housenumbers for a relation.
tx.execute_batch(
"create table additional_housenumbers_counts (
relation text not null,
count text not null,
unique(relation)
);
create index idx_additional_housenumbers_counts
on additional_housenumbers_counts(relation);",
)?;
}
if user_version < 15 {
// Tracks housenumbers for the whole country.
tx.execute_batch(
"create table whole_country (
postcode text not null,
city text not null,
street text not null,
housenumber text not null,
user text not null,
osm_id text not null,
osm_type text not null,
timestamp text not null,
place text not null,
unit text not null,
name text not null,
fixme text not null
);",
)?;
}
if user_version < 16 {
// Per-relation cache for the missing-housenumbers analysis.
tx.execute_batch(
"create table missing_housenumbers_cache (
relation text not null,
json text not null,
unique(relation)
);
create index idx_missing_housenumbers_cache
on missing_housenumbers_cache(relation);",
)?;
}
if user_version < 17 {
// Per-relation cache for the additional-housenumbers analysis.
tx.execute_batch(
"create table additional_housenumbers_cache (
relation text not null,
json text not null,
unique(relation)
);
create index idx_additional_housenumbers_cache
on additional_housenumbers_cache(relation);",
)?;
}
if user_version < 18 {
// Tracks various counters.
tx.execute(
"create table counts (
category text not null,
count text not null,
unique(category)
);",
[],
)?;
}
if user_version < 19 {
// Tracks various stat JSON data.
tx.execute(
"create table stats_jsons (
category text not null,
json text not null,
unique(category)
);",
[],
)?;
}
if user_version < 20 {
// Tracks all sellements in a country.
tx.execute(
"create table stats_settlements (
osm_id text not null,
osm_type text not null,
name text not null
);",
[],
)?;
}
if user_version < 21 {
// Speeds up access on whole_country.city.
tx.execute(
"create index idx_whole_country_cities
on whole_country(city);",
[],
)?;
}
tx.execute("pragma user_version = 21", [])?;
tx.commit()?;
Ok(())
}
/// Ignores a primary key constraint violation error, but not other errors.
pub fn ignore_primary_key_constraint(
result: Result<usize, rusqlite::Error>,
) -> Result<(), rusqlite::Error> {
match result {
Err(rusqlite::Error::SqliteFailure(
rusqlite::ffi::Error {
code: rusqlite::ErrorCode::ConstraintViolation,
extended_code: rusqlite::ffi::SQLITE_CONSTRAINT_PRIMARYKEY,
},
_,
)) => Ok(()),
Err(err) => Err(err),
Ok(_) => Ok(()),
}
}
#[cfg(test)]
mod tests;