Skip to content

Commit d887a2e

Browse files
authored
Merge pull request #2027 from joto/schema-qualification
Set default schema to 'public' and use qualified names everywhere
2 parents 583023d + e59440c commit d887a2e

21 files changed

+65
-78
lines changed

docs/osm2pgsql.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ mandatory for short options too.
165165

166166
\--middle-schema=SCHEMA
167167
: Use PostgreSQL schema SCHEMA for all tables, indexes, and functions in
168-
the middle (default is no schema, i.e. the `public` schema is used).
168+
the middle (default is `public`).
169169

170170
\--middle-way-node-index-id-shift=SHIFT
171171
: Set ID shift for way node bucket index in middle. Experts only. See
@@ -275,8 +275,7 @@ mandatory for short options too.
275275

276276
\--output-pgsql-schema=SCHEMA
277277
: Use PostgreSQL schema SCHEMA for all tables, indexes, and functions in
278-
the pgsql output (default is no schema, i.e. the `public` schema
279-
is used).
278+
the pgsql output (default is `public`).
280279

281280
# EXPIRE OPTIONS
282281

src/command-line-parser.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ Middle options:\n\
181181
--cache-strategy=STRATEGY Deprecated. Not used any more.\n\
182182
-x|--extra-attributes Include attributes (user name, user id, changeset\n\
183183
id, timestamp and version) for each object in the database.\n\
184-
--middle-schema=SCHEMA Schema to use for middle tables (default: none).\n\
184+
--middle-schema=SCHEMA Schema to use for middle tables (default: 'public').\n\
185185
--middle-way-node-index-id-shift=SHIFT Set ID shift for bucket index.\n\
186186
--middle-database-format=FORMAT Set middle db format (default: legacy).\n\
187187
--middle-with-nodes Store tagged nodes in db (new middle db format only).\n\
@@ -213,7 +213,7 @@ Pgsql output options:\n\
213213
-K|--keep-coastlines Keep coastline data rather than filtering it out.\n\
214214
Default: discard objects tagged natural=coastline.\n\
215215
--output-pgsql-schema=SCHEMA Schema to use for pgsql output tables\n\
216-
(default: none).\n\
216+
(default: 'public').\n\
217217
--reproject-area Compute area column using web mercator coordinates.\n\
218218
\n\
219219
Expiry options:\n\
@@ -716,11 +716,17 @@ options_t parse_command_line(int argc, char *argv[])
716716
return options;
717717
case 215: // --middle-schema
718718
options.middle_dbschema = optarg;
719+
if (options.middle_dbschema.empty()) {
720+
throw std::runtime_error{"Schema can not be empty."};
721+
}
719722
check_identifier(options.middle_dbschema,
720723
"--middle-schema parameter");
721724
break;
722725
case 216: // --output-pgsql-schema
723726
options.output_dbschema = optarg;
727+
if (options.output_dbschema.empty()) {
728+
throw std::runtime_error{"Schema can not be empty."};
729+
}
724730
check_identifier(options.output_dbschema,
725731
"--output-pgsql-schema parameter");
726732
break;

src/db-copy.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
*/
2929
struct db_target_descr_t
3030
{
31-
/// Schema of the target table (can be empty for default schema)
32-
std::string schema;
31+
/// Schema of the target table.
32+
std::string schema{"public"};
3333
/// Name of the target table for the copy operation.
3434
std::string name;
3535
/// Name of id column used when deleting objects.

src/expire-output.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "tile.hpp"
1414

15+
#include <cassert>
1516
#include <cstdint>
1617
#include <string>
1718
#include <utility>
@@ -39,6 +40,7 @@ class expire_output_t
3940

4041
void set_schema_and_table(std::string schema, std::string table)
4142
{
43+
assert(!schema.empty());
4244
m_schema = std::move(schema);
4345
m_table = std::move(table);
4446
}
@@ -78,8 +80,8 @@ class expire_output_t
7880
/// The filename (if any) for output
7981
std::string m_filename;
8082

81-
/// The schema (if any) for output
82-
std::string m_schema;
83+
/// The schema for output
84+
std::string m_schema{"public"};
8385

8486
/// The table (if any) for output
8587
std::string m_table;

src/flex-lua-expire-output.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ create_expire_output(lua_State *lua_state,
3030
lua_pop(lua_state, 1); // "filename"
3131

3232
// optional "schema" and "table" fields
33-
auto const *schema =
34-
luaX_get_table_string(lua_state, "schema", -1, "The expire output", "");
33+
auto const *schema = luaX_get_table_string(lua_state, "schema", -1,
34+
"The expire output", "public");
3535
check_identifier(schema, "schema field");
3636
auto const *table =
3737
luaX_get_table_string(lua_state, "table", -2, "The expire output", "");

src/gen/gen-base.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ gen_base_t::gen_base_t(pg_conn_t *connection, params_t *params)
2020
assert(connection);
2121
assert(params);
2222

23+
params->check_identifier_with_default("schema", "public");
2324
auto const schema = params->get_identifier("schema");
24-
if (schema.empty()) {
25-
params->set("schema", "public");
26-
}
2725

2826
if (params->has("src_table")) {
2927
auto const src_table = get_params().get_identifier("src_table");

src/gen/gen-tile-builtup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ gen_tile_builtup_t::gen_tile_builtup_t(pg_conn_t *connection, params_t *params)
3333
{
3434
check_src_dest_table_params_exist();
3535

36-
m_schema = get_params().get_identifier("schema");
3736
m_source_tables =
3837
osmium::split_string(get_params().get_string("src_tables"), ',');
3938

@@ -108,10 +107,11 @@ CREATE TABLE IF NOT EXISTS "{}" (
108107
m_image_buffer, m_margin);
109108

110109
int n = 0;
110+
auto const schema = get_params().get_string("schema");
111111
for (auto const &src_table : m_source_tables) {
112112
params_t tmp_params;
113113
tmp_params.set("N", std::to_string(n++));
114-
tmp_params.set("SRC", qualified_name(m_schema, src_table));
114+
tmp_params.set("SRC", qualified_name(schema, src_table));
115115

116116
dbexec(tmp_params, R"(
117117
PREPARE get_geoms_{N} (real, real, real, real) AS

src/gen/gen-tile-builtup.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ class gen_tile_builtup_t final : public gen_tile_t
3737

3838
std::vector<std::string> m_source_tables;
3939
std::string m_image_path;
40-
std::string m_schema;
41-
std::string m_dest_table;
4240
std::string m_image_table;
4341
double m_margin = 0.0;
4442
std::size_t m_image_extent = 2048;

src/gen/osm2pgsql-gen.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Main Options:
9090
-c|--create Run in create mode (default)
9191
-S|--style=FILE The Lua config file (required, same as for osm2pgsql)
9292
-j|--jobs=NUM Number of parallel jobs (default 1)
93+
--middle-schema=SCHEMA Database schema for middle tables
9394
9495
Help/Version Options:
9596
-h|--help Print this help text and stop
@@ -125,6 +126,7 @@ static std::array<option, 20> const long_options = {
125126
{"log-level", required_argument, nullptr, 'l'},
126127
{"style", required_argument, nullptr, 'S'},
127128
{"log-sql", no_argument, nullptr, 201},
129+
{"middle-schema", required_argument, nullptr, 202},
128130
{nullptr, 0, nullptr, 0}}};
129131

130132
struct tile_extent
@@ -583,6 +585,7 @@ int main(int argc, char *argv[])
583585
{
584586
try {
585587
database_options_t database_options;
588+
std::string schema{"public"};
586589
std::string log_level;
587590
std::string style;
588591
uint32_t jobs = 1;
@@ -594,7 +597,7 @@ int main(int argc, char *argv[])
594597
while (-1 != (c = getopt_long(argc, argv, short_options,
595598
long_options.data(), nullptr))) {
596599
switch (c) {
597-
case 'h':
600+
case 'h': // --help
598601
show_help();
599602
return 0;
600603
case 'a': // --append
@@ -622,19 +625,27 @@ int main(int argc, char *argv[])
622625
case 'P': // --port
623626
database_options.port = optarg;
624627
break;
625-
case 'l':
628+
case 'l': // --log-level
626629
log_level = optarg;
627630
break;
628-
case 'S':
631+
case 'S': // --style
629632
style = optarg;
630633
break;
631-
case 'V':
634+
case 'V': // --version
632635
log_info("osm2pgsql-gen version {}", get_osm2pgsql_version());
633636
canvas_t::info();
634637
return 0;
635-
case 201:
638+
case 201: // --log-sql
636639
get_logger().enable_sql();
637640
break;
641+
case 202: // --middle-schema
642+
schema = optarg;
643+
if (schema.empty()) {
644+
log_error("Schema must not be empty");
645+
return 2;
646+
}
647+
check_identifier(schema, "--middle-schema");
648+
break;
638649
default:
639650
log_error("Unknown argument");
640651
return 2;
@@ -696,7 +707,7 @@ int main(int argc, char *argv[])
696707
init_database_capabilities(db_connection);
697708
}
698709

699-
properties_t properties{conninfo, ""};
710+
properties_t properties{conninfo, schema};
700711
properties.load();
701712

702713
bool const updatable = properties.get_bool("updatable", false);

src/middle-pgsql.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ static std::string build_sql(options_t const &options, std::string const &templ)
8787
: "USING INDEX TABLESPACE " +
8888
options.tblsslim_index};
8989

90-
std::string const schema = options.middle_dbschema.empty()
91-
? ""
92-
: ("\"" + options.middle_dbschema + "\".");
90+
std::string const schema = "\"" + options.middle_dbschema + "\".";
9391

9492
return fmt::format(
9593
fmt::runtime(templ), fmt::arg("prefix", options.prefix),

0 commit comments

Comments
 (0)