Skip to content

Commit 7b77762

Browse files
committed
Store properties in database only later after more checks
If osm2pgsql fails during initialization, for instance because the input file or the style file was not found, it had already initialized the osm2pgsql_properties table in the database. We now write the properties table later to reduce the chances that a small error on the command line destroys an existing database this way.
1 parent fbd3bfa commit 7b77762

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/osm2pgsql.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void show_memory_usage()
4444
}
4545
}
4646

47-
file_info run(options_t const &options, properties_t const &properties)
47+
file_info run(options_t const &options, properties_t *properties)
4848
{
4949
auto const files = prepare_input_files(
5050
options.input_files, options.input_format, options.append);
@@ -57,10 +57,15 @@ file_info run(options_t const &options, properties_t const &properties)
5757
middle->start();
5858

5959
auto output = output_t::create_output(middle->get_query_instance(),
60-
thread_pool, options, properties);
60+
thread_pool, options, *properties);
6161

6262
middle->set_requirements(output->get_requirements());
6363

64+
if (!options.append) {
65+
properties->init_table();
66+
}
67+
properties->store();
68+
6469
osmdata_t osmdata{middle, output, options};
6570

6671
// Processing: In this phase the input file(s) are read and parsed,
@@ -93,8 +98,8 @@ void check_db(options_t const &options)
9398
check_schema(options.output_dbschema);
9499
}
95100

96-
// This is called in "create" mode to store properties into the database.
97-
void store_properties(properties_t *properties, options_t const &options)
101+
// This is called in "create" mode to initialize properties.
102+
void set_up_properties(properties_t *properties, options_t const &options)
98103
{
99104
properties->set_bool("attributes", options.extra_attributes);
100105

@@ -121,8 +126,6 @@ void store_properties(properties_t *properties, options_t const &options)
121126
std::filesystem::absolute(std::filesystem::path{options.style})
122127
.string());
123128
}
124-
125-
properties->store();
126129
}
127130

128131
void store_data_properties(properties_t *properties, file_info const &finfo)
@@ -139,8 +142,6 @@ void store_data_properties(properties_t *properties, file_info const &finfo)
139142
properties->set_string("replication_" + s, value);
140143
}
141144
}
142-
143-
properties->store();
144145
}
145146

146147
void check_updatable(properties_t const &properties)
@@ -348,6 +349,7 @@ int main(int argc, char *argv[])
348349

349350
properties_t properties{options.connection_params,
350351
options.middle_dbschema};
352+
351353
if (options.append) {
352354
if (!properties.load()) {
353355
throw std::runtime_error{
@@ -358,7 +360,7 @@ int main(int argc, char *argv[])
358360
check_and_update_properties(&properties, &options);
359361
properties.store();
360362

361-
auto const finfo = run(options, properties);
363+
auto const finfo = run(options, &properties);
362364

363365
if (finfo.last_timestamp.valid()) {
364366
auto const current_timestamp =
@@ -369,17 +371,17 @@ int main(int argc, char *argv[])
369371
osmium::Timestamp{current_timestamp})) {
370372
properties.set_string("current_timestamp",
371373
finfo.last_timestamp.to_iso());
372-
properties.store();
373374
}
374375
}
375376
} else {
376-
properties.init_table();
377377
set_option_defaults(&options);
378-
store_properties(&properties, options);
379-
auto const finfo = run(options, properties);
378+
set_up_properties(&properties, options);
379+
auto const finfo = run(options, &properties);
380380
store_data_properties(&properties, finfo);
381381
}
382382

383+
properties.store();
384+
383385
show_memory_usage();
384386
log_info("osm2pgsql took {} overall.",
385387
util::human_readable_duration(timer_overall.stop()));

0 commit comments

Comments
 (0)