Skip to content

Commit 66a8002

Browse files
authored
Merge pull request #1815 from joto/allow-tables-with-only-id-columns
Allow tables in flex output that only have ids columns
2 parents 54ea689 + 1a58b58 commit 66a8002

File tree

2 files changed

+124
-3
lines changed

2 files changed

+124
-3
lines changed

src/output-flex.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,8 @@ void output_flex_t::setup_flex_table_columns(flex_table_t *table)
11271127
lua_getfield(lua_state(), -1, "columns");
11281128
if (lua_type(lua_state(), -1) != LUA_TTABLE) {
11291129
throw std::runtime_error{
1130-
"No columns defined for table '{}'."_format(table->name())};
1130+
"No 'columns' field (or not an array) in table '{}'."_format(
1131+
table->name())};
11311132
}
11321133

11331134
std::size_t num_columns = 0;
@@ -1175,7 +1176,7 @@ void output_flex_t::setup_flex_table_columns(flex_table_t *table)
11751176
++num_columns;
11761177
}
11771178

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

1192-
luaL_checktype(lua_state(), 1, LUA_TTABLE);
1193+
if (lua_type(lua_state(), 1) != LUA_TTABLE) {
1194+
throw std::runtime_error{
1195+
"Argument #1 to 'define_table' must be a table."};
1196+
}
11931197

11941198
auto &new_table = create_flex_table();
11951199
setup_id_columns(&new_table);
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
Feature: Table definitions in Lua file
2+
3+
Scenario: Table definition needs a table parameter
4+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
5+
And the lua style
6+
"""
7+
local t = osm2pgsql.define_table()
8+
"""
9+
Then running osm2pgsql flex fails
10+
And the error output contains
11+
"""
12+
Argument #1 to 'define_table' must be a table.
13+
"""
14+
15+
Scenario: Table definition needs a name
16+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
17+
And the lua style
18+
"""
19+
local t = osm2pgsql.define_table({})
20+
"""
21+
Then running osm2pgsql flex fails
22+
And the error output contains
23+
"""
24+
The table must contain a 'name' string field.
25+
"""
26+
27+
Scenario: Name in table definition has to be a string
28+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
29+
And the lua style
30+
"""
31+
local t = osm2pgsql.define_table({
32+
name = false
33+
})
34+
"""
35+
Then running osm2pgsql flex fails
36+
And the error output contains
37+
"""
38+
The table must contain a 'name' string field.
39+
"""
40+
41+
Scenario: Table definition needs a column list
42+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
43+
And the lua style
44+
"""
45+
local t = osm2pgsql.define_table({
46+
name = 'foo'
47+
})
48+
"""
49+
Then running osm2pgsql flex fails
50+
And the error output contains
51+
"""
52+
No 'columns' field (or not an array) in table 'foo'.
53+
"""
54+
55+
Scenario: The columns field must contain a table
56+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
57+
And the lua style
58+
"""
59+
local t = osm2pgsql.define_table({
60+
name = 'foo',
61+
columns = 123
62+
})
63+
"""
64+
Then running osm2pgsql flex fails
65+
And the error output contains
66+
"""
67+
No 'columns' field (or not an array) in table 'foo'.
68+
"""
69+
70+
Scenario: Table with empty columns list is not okay if there are no ids
71+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
72+
And the lua style
73+
"""
74+
local t = osm2pgsql.define_table({
75+
name = 'foo',
76+
columns = {}
77+
})
78+
"""
79+
Then running osm2pgsql flex fails
80+
And the error output contains
81+
"""
82+
No columns defined for table 'foo'.
83+
"""
84+
85+
Scenario: Table with empty columns list is okay if there is an id column
86+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
87+
And the lua style
88+
"""
89+
local t = osm2pgsql.define_table({
90+
name = 'foo',
91+
ids = { type = 'node', id_column = 'node_id' },
92+
columns = {}
93+
})
94+
95+
function osm2pgsql.process_node(object)
96+
t:insert({})
97+
end
98+
"""
99+
When running osm2pgsql flex
100+
Then table foo has 1562 rows
101+
102+
Scenario: Can not create two tables with the same name
103+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
104+
And the lua style
105+
"""
106+
local t1 = osm2pgsql.define_node_table('foo', {
107+
{ column = 'bar' }
108+
})
109+
local t2 = osm2pgsql.define_node_table('foo', {
110+
{ column = 'baz' }
111+
})
112+
"""
113+
Then running osm2pgsql flex fails
114+
And the error output contains
115+
"""
116+
Table with name 'foo' already exists.
117+
"""

0 commit comments

Comments
 (0)