From 7b77762c02eadf05abdba633b4eecbf9a9f2e016 Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Sun, 15 Sep 2024 16:20:21 +0200 Subject: [PATCH] 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. --- src/osm2pgsql.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/osm2pgsql.cpp b/src/osm2pgsql.cpp index 1db3a623e..ee0b6976f 100644 --- a/src/osm2pgsql.cpp +++ b/src/osm2pgsql.cpp @@ -44,7 +44,7 @@ void show_memory_usage() } } -file_info run(options_t const &options, properties_t const &properties) +file_info run(options_t const &options, properties_t *properties) { auto const files = prepare_input_files( options.input_files, options.input_format, options.append); @@ -57,10 +57,15 @@ file_info run(options_t const &options, properties_t const &properties) middle->start(); auto output = output_t::create_output(middle->get_query_instance(), - thread_pool, options, properties); + thread_pool, options, *properties); middle->set_requirements(output->get_requirements()); + if (!options.append) { + properties->init_table(); + } + properties->store(); + osmdata_t osmdata{middle, output, options}; // Processing: In this phase the input file(s) are read and parsed, @@ -93,8 +98,8 @@ void check_db(options_t const &options) check_schema(options.output_dbschema); } -// This is called in "create" mode to store properties into the database. -void store_properties(properties_t *properties, options_t const &options) +// This is called in "create" mode to initialize properties. +void set_up_properties(properties_t *properties, options_t const &options) { properties->set_bool("attributes", options.extra_attributes); @@ -121,8 +126,6 @@ void store_properties(properties_t *properties, options_t const &options) std::filesystem::absolute(std::filesystem::path{options.style}) .string()); } - - properties->store(); } 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) properties->set_string("replication_" + s, value); } } - - properties->store(); } void check_updatable(properties_t const &properties) @@ -348,6 +349,7 @@ int main(int argc, char *argv[]) properties_t properties{options.connection_params, options.middle_dbschema}; + if (options.append) { if (!properties.load()) { throw std::runtime_error{ @@ -358,7 +360,7 @@ int main(int argc, char *argv[]) check_and_update_properties(&properties, &options); properties.store(); - auto const finfo = run(options, properties); + auto const finfo = run(options, &properties); if (finfo.last_timestamp.valid()) { auto const current_timestamp = @@ -369,17 +371,17 @@ int main(int argc, char *argv[]) osmium::Timestamp{current_timestamp})) { properties.set_string("current_timestamp", finfo.last_timestamp.to_iso()); - properties.store(); } } } else { - properties.init_table(); set_option_defaults(&options); - store_properties(&properties, options); - auto const finfo = run(options, properties); + set_up_properties(&properties, options); + auto const finfo = run(options, &properties); store_data_properties(&properties, finfo); } + properties.store(); + show_memory_usage(); log_info("osm2pgsql took {} overall.", util::human_readable_duration(timer_overall.stop()));