Skip to content

Commit 3d4bb81

Browse files
authored
Merge pull request #1875 from joto/earlier-db-compat-checks
Earlier database compatibility checks
2 parents 6a6f91e + f03b3ef commit 3d4bb81

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

src/flex-table-column.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "flex-table-column.hpp"
1111
#include "format.hpp"
12+
#include "pgsql-capabilities.hpp"
1213
#include "util.hpp"
1314

1415
#include <algorithm>
@@ -82,7 +83,14 @@ flex_table_column_t::flex_table_column_t(std::string name,
8283
: m_name(std::move(name)), m_type_name(lowercase(type)),
8384
m_sql_type(std::move(sql_type)),
8485
m_type(get_column_type_from_string(m_type_name))
85-
{}
86+
{
87+
if (m_type == table_column_type::hstore) {
88+
if (!has_extension("hstore")) {
89+
throw std::runtime_error{"Extension 'hstore' not available. Use "
90+
"'CREATE EXTENSION hstore;' to load it."};
91+
}
92+
}
93+
}
8694

8795
void flex_table_column_t::set_projection(char const *projection)
8896
{

src/flex-table.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,6 @@ bool flex_table_t::has_id_column() const noexcept
7474
(m_columns[0].type() == table_column_type::id_num);
7575
}
7676

77-
bool flex_table_t::has_hstore_column() const noexcept
78-
{
79-
auto const it = std::find_if(begin(), end(), [&](auto const &column) {
80-
return column.type() == table_column_type::hstore;
81-
});
82-
return it != end();
83-
}
84-
8577
bool flex_table_t::matches_type(osmium::item_type type) const noexcept
8678
{
8779
// This table takes any type -> okay
@@ -261,29 +253,6 @@ void table_connection_t::start(bool append)
261253
{
262254
assert(m_db_connection);
263255

264-
if (!has_schema(table().schema())) {
265-
throw fmt_error("Schema '{0}' not available."
266-
" Use 'CREATE SCHEMA \"{0}\";' to create it.",
267-
table().schema());
268-
}
269-
270-
for (auto const &ts :
271-
{table().data_tablespace(), table().index_tablespace()}) {
272-
if (!has_tablespace(ts)) {
273-
throw fmt_error(
274-
"Tablespace '{0}' not available."
275-
" Use 'CREATE TABLESPACE \"{0}\" ...;' to create it.",
276-
ts);
277-
}
278-
}
279-
280-
if (table().has_hstore_column()) {
281-
if (!has_extension("hstore")) {
282-
throw std::runtime_error{"Extension 'hstore' not available. Use "
283-
"'CREATE EXTENSION hstore;' to load it."};
284-
}
285-
}
286-
287256
m_db_connection->exec("SET client_min_messages = WARNING");
288257

289258
if (!append) {

src/flex-table.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,24 @@ class flex_table_t
6464
return m_index_tablespace;
6565
}
6666

67-
void set_schema(std::string const &schema) noexcept { m_schema = schema; }
67+
void set_schema(std::string schema) noexcept
68+
{
69+
m_schema = std::move(schema);
70+
}
6871

6972
void set_cluster_by_geom(bool cluster) noexcept
7073
{
7174
m_cluster_by_geom = cluster;
7275
}
7376

74-
void set_data_tablespace(std::string const &tablespace) noexcept
77+
void set_data_tablespace(std::string tablespace) noexcept
7578
{
76-
m_data_tablespace = tablespace;
79+
m_data_tablespace = std::move(tablespace);
7780
}
7881

79-
void set_index_tablespace(std::string const &tablespace) noexcept
82+
void set_index_tablespace(std::string tablespace) noexcept
8083
{
81-
m_index_tablespace = tablespace;
84+
m_index_tablespace = std::move(tablespace);
8285
}
8386

8487
osmium::item_type id_type() const noexcept { return m_id_type; }
@@ -104,8 +107,6 @@ class flex_table_t
104107
return m_geom_column != std::numeric_limits<std::size_t>::max();
105108
}
106109

107-
bool has_hstore_column() const noexcept;
108-
109110
/// Get the (first, if there are multiple) geometry column.
110111
flex_table_column_t const &geom_column() const noexcept
111112
{

src/output-flex.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,16 @@ int output_flex_t::app_as_geometrycollection()
434434
return 1;
435435
}
436436

437+
static void check_tablespace(std::string const &tablespace)
438+
{
439+
if (!has_tablespace(tablespace)) {
440+
throw fmt_error(
441+
"Tablespace '{0}' not available."
442+
" Use 'CREATE TABLESPACE \"{0}\" ...;' to create it.",
443+
tablespace);
444+
}
445+
}
446+
437447
flex_table_t &output_flex_t::create_flex_table()
438448
{
439449
std::string const table_name =
@@ -455,6 +465,11 @@ flex_table_t &output_flex_t::create_flex_table()
455465
if (lua_isstring(lua_state(), -1)) {
456466
std::string const schema = lua_tostring(lua_state(), -1);
457467
check_identifier(schema, "schema field");
468+
if (!has_schema(schema)) {
469+
throw fmt_error("Schema '{0}' not available."
470+
" Use 'CREATE SCHEMA \"{0}\";' to create it.",
471+
schema);
472+
}
458473
new_table.set_schema(schema);
459474
}
460475
lua_pop(lua_state(), 1);
@@ -486,6 +501,7 @@ flex_table_t &output_flex_t::create_flex_table()
486501
if (lua_isstring(lua_state(), -1)) {
487502
std::string const tablespace = lua_tostring(lua_state(), -1);
488503
check_identifier(tablespace, "data_tablespace field");
504+
check_tablespace(tablespace);
489505
new_table.set_data_tablespace(tablespace);
490506
}
491507
lua_pop(lua_state(), 1);
@@ -495,6 +511,7 @@ flex_table_t &output_flex_t::create_flex_table()
495511
if (lua_isstring(lua_state(), -1)) {
496512
std::string const tablespace = lua_tostring(lua_state(), -1);
497513
check_identifier(tablespace, "index_tablespace field");
514+
check_tablespace(tablespace);
498515
new_table.set_index_tablespace(tablespace);
499516
}
500517
lua_pop(lua_state(), 1);

0 commit comments

Comments
 (0)