Skip to content

Commit

Permalink
Merge pull request #1815 from joto/allow-tables-with-only-id-columns
Browse files Browse the repository at this point in the history
Allow tables in flex output that only have ids columns
  • Loading branch information
lonvia authored Nov 10, 2022
2 parents 54ea689 + 1a58b58 commit 66a8002
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/output-flex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,8 @@ void output_flex_t::setup_flex_table_columns(flex_table_t *table)
lua_getfield(lua_state(), -1, "columns");
if (lua_type(lua_state(), -1) != LUA_TTABLE) {
throw std::runtime_error{
"No columns defined for table '{}'."_format(table->name())};
"No 'columns' field (or not an array) in table '{}'."_format(
table->name())};
}

std::size_t num_columns = 0;
Expand Down Expand Up @@ -1175,7 +1176,7 @@ void output_flex_t::setup_flex_table_columns(flex_table_t *table)
++num_columns;
}

if (num_columns == 0) {
if (num_columns == 0 && !table->has_id_column()) {
throw std::runtime_error{
"No columns defined for table '{}'."_format(table->name())};
}
Expand All @@ -1189,7 +1190,10 @@ int output_flex_t::app_define_table()
" main Lua code, not in any of the callbacks."};
}

luaL_checktype(lua_state(), 1, LUA_TTABLE);
if (lua_type(lua_state(), 1) != LUA_TTABLE) {
throw std::runtime_error{
"Argument #1 to 'define_table' must be a table."};
}

auto &new_table = create_flex_table();
setup_id_columns(&new_table);
Expand Down
117 changes: 117 additions & 0 deletions tests/bdd/flex/lua-table-definitions.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
Feature: Table definitions in Lua file

Scenario: Table definition needs a table parameter
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
And the lua style
"""
local t = osm2pgsql.define_table()
"""
Then running osm2pgsql flex fails
And the error output contains
"""
Argument #1 to 'define_table' must be a table.
"""

Scenario: Table definition needs a name
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
And the lua style
"""
local t = osm2pgsql.define_table({})
"""
Then running osm2pgsql flex fails
And the error output contains
"""
The table must contain a 'name' string field.
"""

Scenario: Name in table definition has to be a string
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
And the lua style
"""
local t = osm2pgsql.define_table({
name = false
})
"""
Then running osm2pgsql flex fails
And the error output contains
"""
The table must contain a 'name' string field.
"""

Scenario: Table definition needs a column list
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
And the lua style
"""
local t = osm2pgsql.define_table({
name = 'foo'
})
"""
Then running osm2pgsql flex fails
And the error output contains
"""
No 'columns' field (or not an array) in table 'foo'.
"""

Scenario: The columns field must contain a table
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
And the lua style
"""
local t = osm2pgsql.define_table({
name = 'foo',
columns = 123
})
"""
Then running osm2pgsql flex fails
And the error output contains
"""
No 'columns' field (or not an array) in table 'foo'.
"""

Scenario: Table with empty columns list is not okay if there are no ids
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
And the lua style
"""
local t = osm2pgsql.define_table({
name = 'foo',
columns = {}
})
"""
Then running osm2pgsql flex fails
And the error output contains
"""
No columns defined for table 'foo'.
"""

Scenario: Table with empty columns list is okay if there is an id column
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
And the lua style
"""
local t = osm2pgsql.define_table({
name = 'foo',
ids = { type = 'node', id_column = 'node_id' },
columns = {}
})
function osm2pgsql.process_node(object)
t:insert({})
end
"""
When running osm2pgsql flex
Then table foo has 1562 rows

Scenario: Can not create two tables with the same name
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
And the lua style
"""
local t1 = osm2pgsql.define_node_table('foo', {
{ column = 'bar' }
})
local t2 = osm2pgsql.define_node_table('foo', {
{ column = 'baz' }
})
"""
Then running osm2pgsql flex fails
And the error output contains
"""
Table with name 'foo' already exists.
"""

0 comments on commit 66a8002

Please sign in to comment.