Skip to content

Commit 36e7f1b

Browse files
committed
Add function to check existence of database schema and use it
1 parent 880f96b commit 36e7f1b

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

src/flex-lua-table.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ static flex_table_t &create_flex_table(lua_State *lua_state,
4949
if (lua_isstring(lua_state, -1)) {
5050
std::string const schema = lua_tostring(lua_state, -1);
5151
check_identifier(schema, "schema field");
52-
if (!has_schema(schema)) {
53-
throw fmt_error("Schema '{0}' not available."
54-
" Use 'CREATE SCHEMA \"{0}\";' to create it.",
55-
schema);
56-
}
52+
check_schema(schema);
5753
new_table.set_schema(schema);
5854
}
5955
lua_pop(lua_state, 1);

src/osm2pgsql.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ void check_db(options_t const &options)
8484

8585
init_database_capabilities(db_connection);
8686

87+
check_schema(options.middle_dbschema);
88+
check_schema(options.output_dbschema);
89+
8790
// If we are in append mode and the middle nodes table isn't there,
8891
// it probably means we used a flat node store when we created this
8992
// database. Check for that and stop if it looks like we are missing

src/pgsql-capabilities.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ bool has_index_method(std::string const &value)
150150
return capabilities().index_methods.count(value);
151151
}
152152

153+
void check_schema(std::string const &schema)
154+
{
155+
if (has_schema(schema)) {
156+
return;
157+
}
158+
159+
throw fmt_error("Schema '{0}' not available."
160+
" Use 'CREATE SCHEMA \"{0}\";' to create it.",
161+
schema);
162+
}
163+
153164
uint32_t get_database_version() noexcept
154165
{
155166
return capabilities().database_version;

src/pgsql-capabilities.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ bool has_schema(std::string const &value);
2222
bool has_tablespace(std::string const &value);
2323
bool has_index_method(std::string const &value);
2424

25+
void check_schema(std::string const &schema);
26+
2527
/// Get PostgreSQL version in the format (major * 10000 + minor).
2628
uint32_t get_database_version() noexcept;
2729

0 commit comments

Comments
 (0)