Skip to content

Commit

Permalink
Store properties in database only later after more checks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joto committed Sep 16, 2024
1 parent fbd3bfa commit 7b77762
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/osm2pgsql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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);

Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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{
Expand All @@ -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 =
Expand All @@ -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()));
Expand Down

0 comments on commit 7b77762

Please sign in to comment.