Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to create primary key on flex tables #2289

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/flex-lua-table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ void parse_create_index(lua_State *lua_state, flex_table_t *table)
table->set_always_build_id_index();
} else if (create_index == "unique") {
table->set_always_build_id_index();
table->set_build_unique_id_index();
table->set_build_unique_id_index(false);
} else if (create_index == "primary_key") {
table->set_always_build_id_index();
table->set_build_unique_id_index(true);
} else if (create_index != "auto") {
throw fmt_error("Unknown value '{}' for 'create_index' field of ids",
create_index);
Expand Down
9 changes: 9 additions & 0 deletions src/flex-table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ std::string flex_table_t::build_sql_column_list() const

std::string flex_table_t::build_sql_create_id_index() const
{
if (m_primary_key_index) {
auto ts = tablespace_clause(index_tablespace());
if (!ts.empty()) {
ts = " USING INDEX" + ts;
}
return fmt::format("ALTER TABLE {} ADD PRIMARY KEY ({}){}", full_name(),
id_column_names(), ts);
}

return fmt::format("CREATE {}INDEX ON {} USING BTREE ({}) {}",
m_build_unique_id_index ? "UNIQUE " : "", full_name(),
id_column_names(),
Expand Down
6 changes: 5 additions & 1 deletion src/flex-table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,10 @@ class flex_table_t
return m_always_build_id_index;
}

void set_build_unique_id_index() noexcept
void set_build_unique_id_index(bool as_primary_key) noexcept
{
m_build_unique_id_index = true;
m_primary_key_index = as_primary_key;
}

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

/// Index should be a primary key.
bool m_primary_key_index = false;

}; // class flex_table_t

class table_connection_t
Expand Down
34 changes: 34 additions & 0 deletions tests/bdd/flex/lua-table-definitions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,40 @@ Feature: Table definitions in Lua file
When running osm2pgsql flex
Then table foo has 1562 rows

Scenario: Unique index is okay
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', index = 'unique' },
columns = {}
})

function osm2pgsql.process_node(object)
t:insert({})
end
"""
When running osm2pgsql flex
Then table foo has 1562 rows

Scenario: Primary key is okay
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', index = 'primary_key' },
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
Expand Down
Loading