Skip to content

Commit 05d8bf8

Browse files
committed
Use has_extension/schema/tablespace() to create nicer error messages
This is currently for the flex output only. Backporting to pgsql output is left as an exercise for the reader.
1 parent c236ed0 commit 05d8bf8

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/flex-table.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "flex-table.hpp"
1111
#include "format.hpp"
1212
#include "logging.hpp"
13+
#include "pgsql-capabilities.hpp"
1314
#include "pgsql-helper.hpp"
1415
#include "util.hpp"
1516

@@ -189,6 +190,30 @@ void table_connection_t::start(bool append)
189190
{
190191
assert(m_db_connection);
191192

193+
if (!has_schema(*m_db_connection, table().schema())) {
194+
throw std::runtime_error{
195+
"Schema '{0}' not available. "
196+
"Use 'CREATE SCHEMA \"{0}\";' to create it."_format(
197+
table().schema())};
198+
}
199+
200+
for (auto const &ts :
201+
{table().data_tablespace(), table().index_tablespace()}) {
202+
if (!has_tablespace(*m_db_connection, ts)) {
203+
throw std::runtime_error{
204+
"Tablespace '{0}' not available. "
205+
"Use 'CREATE TABLESPACE \"{0}\" ...;' to create it."_format(
206+
ts)};
207+
}
208+
}
209+
210+
if (table().has_hstore_column()) {
211+
if (!has_extension(*m_db_connection, "hstore")) {
212+
throw std::runtime_error{"Extension 'hstore' not available. Use "
213+
"'CREATE EXTENSION hstore;' to load it."};
214+
}
215+
}
216+
192217
m_db_connection->exec("SET client_min_messages = WARNING");
193218

194219
if (!append) {

src/flex-table.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ class flex_table_t
110110
return m_geom_column != std::numeric_limits<std::size_t>::max();
111111
}
112112

113+
bool has_hstore_column() const noexcept
114+
{
115+
auto const it = std::find_if(begin(), end(), [&](auto const &column) {
116+
return column.type() == table_column_type::hstore;
117+
});
118+
return it != end();
119+
}
120+
113121
/// Get the (first, if there are multiple) geometry column.
114122
flex_table_column_t const &geom_column() const noexcept
115123
{

0 commit comments

Comments
 (0)