Skip to content

Commit 43dd112

Browse files
committed
Add new command line parameter --schema that sets the default schema
This will be used as default for --middle-schema, --output-pgsql-schema, and for the different ways of setting a schema in the flex output and generalizer code. This removes the last places where the schema was hardcoded to "public" (except as a default for this command line parameter and in some legacy gazetteer code).
1 parent e340a4c commit 43dd112

14 files changed

+105
-46
lines changed

src/command-line-parser.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ struct option const long_options[] = {
8383
{"prefix", required_argument, nullptr, 'p'},
8484
{"proj", required_argument, nullptr, 'E'},
8585
{"reproject-area", no_argument, nullptr, 213},
86+
{"schema", required_argument, nullptr, 218},
8687
{"slim", no_argument, nullptr, 's'},
8788
{"style", required_argument, nullptr, 'S'},
8889
{"tablespace-index", required_argument, nullptr, 'i'},
@@ -140,6 +141,7 @@ Common options:\n\
140141
information in slim mode instead of in PostgreSQL.\n\
141142
This is a single large file (> 50GB). Only recommended\n\
142143
for full planet imports. Default is disabled.\n\
144+
--schema=SCHEMA Default schema (default: 'public').\n\
143145
\n\
144146
Database options:\n\
145147
-d|--database=DB The name of the PostgreSQL database to connect to or\n\
@@ -181,7 +183,7 @@ Middle options:\n\
181183
--cache-strategy=STRATEGY Deprecated. Not used any more.\n\
182184
-x|--extra-attributes Include attributes (user name, user id, changeset\n\
183185
id, timestamp and version) for each object in the database.\n\
184-
--middle-schema=SCHEMA Schema to use for middle tables (default: 'public').\n\
186+
--middle-schema=SCHEMA Schema to use for middle tables (default: setting of --schema).\n\
185187
--middle-way-node-index-id-shift=SHIFT Set ID shift for bucket index.\n\
186188
--middle-database-format=FORMAT Set middle db format (default: legacy).\n\
187189
--middle-with-nodes Store tagged nodes in db (new middle db format only).\n\
@@ -213,7 +215,7 @@ Pgsql output options:\n\
213215
-K|--keep-coastlines Keep coastline data rather than filtering it out.\n\
214216
Default: discard objects tagged natural=coastline.\n\
215217
--output-pgsql-schema=SCHEMA Schema to use for pgsql output tables\n\
216-
(default: 'public').\n\
218+
(default: setting of --schema).\n\
217219
--reproject-area Compute area column using web mercator coordinates.\n\
218220
\n\
219221
Expiry options:\n\
@@ -728,6 +730,13 @@ options_t parse_command_line(int argc, char *argv[])
728730
options.with_forward_dependencies =
729731
parse_with_forward_dependencies_param(optarg);
730732
break;
733+
case 218: // --schema
734+
options.dbschema = optarg;
735+
if (options.dbschema.empty()) {
736+
throw std::runtime_error{"Schema can not be empty."};
737+
}
738+
check_identifier(options.dbschema, "--schema parameter");
739+
break;
731740
case 300: // --middle-way-node-index-id-shift
732741
options.way_node_index_id_shift = atoi(optarg);
733742
break;
@@ -763,6 +772,14 @@ options_t parse_command_line(int argc, char *argv[])
763772
}
764773
} //end while
765774

775+
if (options.middle_dbschema.empty()) {
776+
options.middle_dbschema = options.dbschema;
777+
}
778+
779+
if (options.output_dbschema.empty()) {
780+
options.output_dbschema = options.dbschema;
781+
}
782+
766783
//they were looking for usage info
767784
if (print_help) {
768785
options.command = command_t::help;

src/expire-output.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class expire_output_t
8181
std::string m_filename;
8282

8383
/// The schema for output
84-
std::string m_schema{"public"};
84+
std::string m_schema;
8585

8686
/// The table (if any) for output
8787
std::string m_table;

src/flex-lua-expire-output.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <lua.hpp>
1919

2020
static expire_output_t &
21-
create_expire_output(lua_State *lua_state,
21+
create_expire_output(lua_State *lua_state, std::string const &default_schema,
2222
std::vector<expire_output_t> *expire_outputs)
2323
{
2424
auto &new_expire_output = expire_outputs->emplace_back();
@@ -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 = luaX_get_table_string(lua_state, "schema", -1,
34-
"The expire output", "public");
33+
auto const *schema = luaX_get_table_string(
34+
lua_state, "schema", -1, "The expire output", default_schema.c_str());
3535
check_identifier(schema, "schema field");
3636
auto const *table =
3737
luaX_get_table_string(lua_state, "table", -2, "The expire output", "");
@@ -72,14 +72,15 @@ create_expire_output(lua_State *lua_state,
7272
}
7373

7474
int setup_flex_expire_output(lua_State *lua_state,
75+
std::string const &default_schema,
7576
std::vector<expire_output_t> *expire_outputs)
7677
{
7778
if (lua_type(lua_state, 1) != LUA_TTABLE) {
7879
throw std::runtime_error{
7980
"Argument #1 to 'define_expire_output' must be a Lua table."};
8081
}
8182

82-
create_expire_output(lua_state, expire_outputs);
83+
create_expire_output(lua_state, default_schema, expire_outputs);
8384

8485
void *ptr = lua_newuserdata(lua_state, sizeof(std::size_t));
8586
auto *num = new (ptr) std::size_t{};

src/flex-lua-expire-output.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static char const *const osm2pgsql_expire_output_name =
2020
"osm2pgsql.ExpireOutput";
2121

2222
int setup_flex_expire_output(lua_State *lua_state,
23+
std::string const &default_schema,
2324
std::vector<expire_output_t> *expire_outputs);
2425

2526
#endif // OSM2PGSQL_FLEX_LUA_EXPIRE_OUTPUT_HPP

src/flex-lua-table.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static void check_tablespace(std::string const &tablespace)
2929
}
3030

3131
static flex_table_t &create_flex_table(lua_State *lua_state,
32+
std::string const &default_schema,
3233
std::vector<flex_table_t> *tables)
3334
{
3435
std::string const table_name =
@@ -40,7 +41,7 @@ static flex_table_t &create_flex_table(lua_State *lua_state,
4041
throw fmt_error("Table with name '{}' already exists.", table_name);
4142
}
4243

43-
auto &new_table = tables->emplace_back(table_name);
44+
auto &new_table = tables->emplace_back(default_schema, table_name);
4445

4546
lua_pop(lua_state, 1); // "name"
4647

@@ -411,14 +412,15 @@ static void setup_flex_table_indexes(lua_State *lua_state, flex_table_t *table,
411412

412413
int setup_flex_table(lua_State *lua_state, std::vector<flex_table_t> *tables,
413414
std::vector<expire_output_t> *expire_outputs,
414-
bool updatable, bool append_mode)
415+
std::string const &default_schema, bool updatable,
416+
bool append_mode)
415417
{
416418
if (lua_type(lua_state, 1) != LUA_TTABLE) {
417419
throw std::runtime_error{
418420
"Argument #1 to 'define_table' must be a table."};
419421
}
420422

421-
auto &new_table = create_flex_table(lua_state, tables);
423+
auto &new_table = create_flex_table(lua_state, default_schema, tables);
422424
setup_flex_table_id_columns(lua_state, &new_table);
423425
setup_flex_table_columns(lua_state, &new_table, expire_outputs,
424426
append_mode);

src/flex-lua-table.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* For a full list of authors see the git log.
1111
*/
1212

13+
#include <string>
1314
#include <vector>
1415

1516
class expire_output_t;
@@ -20,6 +21,7 @@ static char const *const osm2pgsql_table_name = "osm2pgsql.Table";
2021

2122
int setup_flex_table(lua_State *lua_state, std::vector<flex_table_t> *tables,
2223
std::vector<expire_output_t> *expire_outputs,
23-
bool updatable, bool append_mode);
24+
std::string const &default_schema, bool updatable,
25+
bool append_mode);
2426

2527
#endif // OSM2PGSQL_FLEX_LUA_TABLE_HPP

src/flex-table.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ class flex_table_t
5959
permanent
6060
};
6161

62-
explicit flex_table_t(std::string name) : m_name(std::move(name)) {}
62+
flex_table_t(std::string schema, std::string name)
63+
: m_schema(std::move(schema)), m_name(std::move(name))
64+
{
65+
}
6366

6467
std::string const &name() const noexcept { return m_name; }
6568

@@ -195,12 +198,12 @@ class flex_table_t
195198
bool has_columns_with_expire() const noexcept;
196199

197200
private:
201+
/// The schema this table is in
202+
std::string m_schema;
203+
198204
/// The name of the table
199205
std::string m_name;
200206

201-
/// The schema this table is in
202-
std::string m_schema{"public"};
203-
204207
/// The table space used for this table (empty for default tablespace)
205208
std::string m_data_tablespace;
206209

src/gen/gen-base.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ 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");
23+
params->check_identifier_with_default("schema", "");
2424
auto const schema = params->get_identifier("schema");
2525

2626
if (params->has("src_table")) {

src/gen/osm2pgsql-gen.cpp

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ Main Options:
9090
-c|--create Run in create mode (default)
9191
-S|--style=FILE The Lua config file (same as for osm2pgsql)
9292
-j|--jobs=NUM Number of parallel jobs (default 1)
93-
--middle-schema=SCHEMA Database schema for middle tables
93+
--middle-schema=SCHEMA Database schema for middle tables (default set with --schema)
94+
--schema=SCHEMA Default database schema (default: 'public')
9495
9596
Help/Version Options:
9697
-h|--help Print this help text and stop
@@ -127,6 +128,7 @@ static std::array<option, 20> const long_options = {
127128
{"style", required_argument, nullptr, 'S'},
128129
{"log-sql", no_argument, nullptr, 201},
129130
{"middle-schema", required_argument, nullptr, 202},
131+
{"schema", required_argument, nullptr, 203},
130132
{nullptr, 0, nullptr, 0}}};
131133

132134
struct tile_extent
@@ -178,9 +180,10 @@ static tile_extent get_extent_from_db(pg_conn_t const &db_connection,
178180
}
179181

180182
static tile_extent get_extent_from_db(pg_conn_t const &db_connection,
183+
std::string const &default_schema,
181184
params_t const &params, uint32_t zoom)
182185
{
183-
auto const schema = params.get_string("schema", "public");
186+
auto const schema = params.get_string("schema", default_schema);
184187
std::string table;
185188
if (params.has("src_table")) {
186189
table = params.get_string("src_table");
@@ -267,8 +270,9 @@ void run_tile_gen(std::string const &conninfo, gen_base_t *master_generalizer,
267270
class genproc_t
268271
{
269272
public:
270-
genproc_t(std::string const &filename, std::string conninfo, bool append,
271-
bool updatable, uint32_t jobs);
273+
genproc_t(std::string const &filename, std::string conninfo,
274+
std::string dbschema, bool append, bool updatable,
275+
uint32_t jobs);
272276

273277
int app_define_table()
274278
{
@@ -281,12 +285,13 @@ class genproc_t
281285
#endif
282286

283287
return setup_flex_table(m_lua_state.get(), &m_tables, &m_expire_outputs,
284-
true, m_append);
288+
m_dbschema, true, m_append);
285289
}
286290

287291
int app_define_expire_output()
288292
{
289-
return setup_flex_expire_output(m_lua_state.get(), &m_expire_outputs);
293+
return setup_flex_expire_output(m_lua_state.get(), m_dbschema,
294+
&m_expire_outputs);
290295
}
291296

292297
int app_run_gen()
@@ -308,6 +313,10 @@ class genproc_t
308313

309314
auto params = parse_params();
310315

316+
if (!params.has("schema")) {
317+
params.set("schema", m_dbschema);
318+
}
319+
311320
write_to_debug_log(params, "Params (config):");
312321

313322
log_debug("Connecting to database...");
@@ -440,7 +449,8 @@ class genproc_t
440449
log_debug("Truncating table '{}'...", table);
441450
db_connection.exec("TRUNCATE {}", table);
442451
} else {
443-
auto const extent = get_extent_from_db(db_connection, params, zoom);
452+
auto const extent =
453+
get_extent_from_db(db_connection, m_dbschema, params, zoom);
444454

445455
if (extent.valid) {
446456
auto const num_tiles = (extent.xmax - extent.xmin + 1) *
@@ -493,6 +503,7 @@ class genproc_t
493503
std::vector<expire_output_t> m_expire_outputs;
494504

495505
std::string m_conninfo;
506+
std::string m_dbschema;
496507
std::size_t m_gen_run = 0;
497508
uint32_t m_jobs;
498509
bool m_append;
@@ -505,9 +516,10 @@ TRAMPOLINE(app_run_gen, run_gen)
505516
TRAMPOLINE(app_run_sql, run_sql)
506517

507518
genproc_t::genproc_t(std::string const &filename, std::string conninfo,
508-
bool append, bool updatable, uint32_t jobs)
509-
: m_conninfo(std::move(conninfo)), m_jobs(jobs), m_append(append),
510-
m_updatable(updatable)
519+
std::string dbschema, bool append, bool updatable,
520+
uint32_t jobs)
521+
: m_conninfo(std::move(conninfo)), m_dbschema(std::move(dbschema)),
522+
m_jobs(jobs), m_append(append), m_updatable(updatable)
511523
{
512524
setup_lua_environment(lua_state(), filename, append);
513525

@@ -587,7 +599,8 @@ int main(int argc, char *argv[])
587599
{
588600
try {
589601
database_options_t database_options;
590-
std::string schema{"public"};
602+
std::string dbschema{"public"};
603+
std::string middle_dbschema{};
591604
std::string log_level;
592605
std::string style;
593606
uint32_t jobs = 1;
@@ -641,12 +654,20 @@ int main(int argc, char *argv[])
641654
get_logger().enable_sql();
642655
break;
643656
case 202: // --middle-schema
644-
schema = optarg;
645-
if (schema.empty()) {
657+
middle_dbschema = optarg;
658+
if (middle_dbschema.empty()) {
659+
log_error("Schema must not be empty");
660+
return 2;
661+
}
662+
check_identifier(middle_dbschema, "--middle-schema");
663+
break;
664+
case 203: // --schema
665+
dbschema = optarg;
666+
if (dbschema.empty()) {
646667
log_error("Schema must not be empty");
647668
return 2;
648669
}
649-
check_identifier(schema, "--middle-schema");
670+
check_identifier(dbschema, "--schema");
650671
break;
651672
default:
652673
log_error("Unknown argument");
@@ -674,6 +695,10 @@ int main(int argc, char *argv[])
674695
return 2;
675696
}
676697

698+
if (middle_dbschema.empty()) {
699+
middle_dbschema = dbschema;
700+
}
701+
677702
util::timer_t timer_overall;
678703

679704
log_info("osm2pgsql-gen version {}", get_osm2pgsql_version());
@@ -704,7 +729,7 @@ int main(int argc, char *argv[])
704729
init_database_capabilities(db_connection);
705730
}
706731

707-
properties_t properties{conninfo, schema};
732+
properties_t properties{conninfo, middle_dbschema};
708733
properties.load();
709734

710735
if (style.empty()) {
@@ -716,7 +741,7 @@ int main(int argc, char *argv[])
716741
}
717742

718743
bool const updatable = properties.get_bool("updatable", false);
719-
genproc_t gen{style, conninfo, append, updatable, jobs};
744+
genproc_t gen{style, conninfo, dbschema, append, updatable, jobs};
720745
gen.run();
721746

722747
osmium::MemoryUsage const mem;

src/options.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,14 @@ struct options_t
9898
/// Pg Tablespace to store slim tables (no default TABLESPACE)
9999
std::string tblsslim_data{};
100100

101+
/// Default Pg schema.
102+
std::string dbschema{"public"};
103+
101104
/// Pg schema to store middle tables in.
102-
std::string middle_dbschema{"public"};
105+
std::string middle_dbschema{};
103106

104107
/// Pg schema to store output tables in.
105-
std::string output_dbschema{"public"};
108+
std::string output_dbschema{};
106109

107110
std::string style{}; ///< style file to use
108111

0 commit comments

Comments
 (0)