Skip to content

Commit 4d04f50

Browse files
committed
Add option to create primary key on flex tables
This is basically just a small variant of the option to create unique indexes. But it documents the intention that the specified columns is supposed to be the primary key. Some programs (like pg_featureserv) use this information to handle the primary key specially.
1 parent a09787c commit 4d04f50

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/flex-lua-table.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ void parse_create_index(lua_State *lua_state, flex_table_t *table)
112112
table->set_always_build_id_index();
113113
} else if (create_index == "unique") {
114114
table->set_always_build_id_index();
115-
table->set_build_unique_id_index();
115+
table->set_build_unique_id_index(false);
116+
} else if (create_index == "primary_key") {
117+
table->set_always_build_id_index();
118+
table->set_build_unique_id_index(true);
116119
} else if (create_index != "auto") {
117120
throw fmt_error("Unknown value '{}' for 'create_index' field of ids",
118121
create_index);

src/flex-table.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ std::string flex_table_t::build_sql_column_list() const
223223

224224
std::string flex_table_t::build_sql_create_id_index() const
225225
{
226+
if (m_primary_key_index) {
227+
auto ts = tablespace_clause(index_tablespace());
228+
if (!ts.empty()) {
229+
ts = " USING INDEX" + ts;
230+
}
231+
return fmt::format("ALTER TABLE {} ADD PRIMARY KEY ({}){}", full_name(),
232+
id_column_names(), ts);
233+
}
234+
226235
return fmt::format("CREATE {}INDEX ON {} USING BTREE ({}) {}",
227236
m_build_unique_id_index ? "UNIQUE " : "", full_name(),
228237
id_column_names(),

src/flex-table.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,10 @@ class flex_table_t
194194
return m_always_build_id_index;
195195
}
196196

197-
void set_build_unique_id_index() noexcept
197+
void set_build_unique_id_index(bool as_primary_key) noexcept
198198
{
199199
m_build_unique_id_index = true;
200+
m_primary_key_index = as_primary_key;
200201
}
201202

202203
bool build_unique_id_index() const noexcept
@@ -265,6 +266,9 @@ class flex_table_t
265266
/// Build the index as a unique index.
266267
bool m_build_unique_id_index = false;
267268

269+
/// Index should be a primary key.
270+
bool m_primary_key_index = false;
271+
268272
}; // class flex_table_t
269273

270274
class table_connection_t

tests/bdd/flex/lua-table-definitions.feature

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,40 @@ Feature: Table definitions in Lua file
9999
When running osm2pgsql flex
100100
Then table foo has 1562 rows
101101

102+
Scenario: Unique index is okay
103+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
104+
And the lua style
105+
"""
106+
local t = osm2pgsql.define_table({
107+
name = 'foo',
108+
ids = { type = 'node', id_column = 'node_id', index = 'unique' },
109+
columns = {}
110+
})
111+
112+
function osm2pgsql.process_node(object)
113+
t:insert({})
114+
end
115+
"""
116+
When running osm2pgsql flex
117+
Then table foo has 1562 rows
118+
119+
Scenario: Primary key is okay
120+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
121+
And the lua style
122+
"""
123+
local t = osm2pgsql.define_table({
124+
name = 'foo',
125+
ids = { type = 'node', id_column = 'node_id', index = 'primary_key' },
126+
columns = {}
127+
})
128+
129+
function osm2pgsql.process_node(object)
130+
t:insert({})
131+
end
132+
"""
133+
When running osm2pgsql flex
134+
Then table foo has 1562 rows
135+
102136
Scenario: Can not create two tables with the same name
103137
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
104138
And the lua style

0 commit comments

Comments
 (0)