Skip to content
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
26 changes: 23 additions & 3 deletions includes/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ function gexport_graph_exports_tasks_table_data(): array {
return $data;
}

/**
* Returns a copy of a table definition with an array-valued 'primary'
* flattened to the backtick-joined scalar form api_plugin_db_table_create()
* expects. Down to this plugin's Cacti 1.2.29 compat floor that API
* interpolates $data['primary'] straight into PRIMARY KEY (`...`), so an
* array would render as PRIMARY KEY (`Array`) and fail the create. The array
* form is kept in the *_table_data() definitions for db_update_table().
*
* @param array<string, mixed> $data The table definition (array primary).
*
* @return array<string, mixed> The definition with a scalar primary key.
*/
function gexport_create_definition(array $data): array {
if (isset($data['primary']) && is_array($data['primary'])) {
$data['primary'] = implode('`,`', $data['primary']);
}

return $data;
}

/**
* Creates every table this plugin owns via the tracked plugin table API
* (idempotent, so it is safe to call on both install and upgrade). Called
Expand Down Expand Up @@ -141,7 +161,7 @@ function gexport_setup_table() {
* @return bool Always returns true.
*/
function gexport_create_table() {
api_plugin_db_table_create('gexport', 'graph_exports', gexport_graph_exports_table_data());
api_plugin_db_table_create('gexport', 'graph_exports', gexport_create_definition(gexport_graph_exports_table_data()));

return true;
}
Expand All @@ -154,7 +174,7 @@ function gexport_create_table() {
* @return bool Always returns true.
*/
function gexport_create_table_tasks() {
api_plugin_db_table_create('gexport', 'graph_exports_tasks', gexport_graph_exports_tasks_table_data());
api_plugin_db_table_create('gexport', 'graph_exports_tasks', gexport_create_definition(gexport_graph_exports_tasks_table_data()));

return true;
}
Expand Down Expand Up @@ -197,7 +217,7 @@ function gexport_upgrade_tables() {
if (db_table_exists($table)) {
db_update_table($table, $data);
} else {
api_plugin_db_table_create('gexport', $table, $data);
api_plugin_db_table_create('gexport', $table, gexport_create_definition($data));
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/GexportSetupTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,24 @@
expect($refreshed)->toContain('graph_exports')
->and($refreshed)->toContain('graph_exports_tasks');
});

it('hands api_plugin_db_table_create a scalar primary key while keeping the array form for db_update_table', function () {
// api_plugin_db_table_create() interpolates data['primary'] straight into
// PRIMARY KEY (`...`) down to the 1.2.29 compat floor, so an array would
// render as PRIMARY KEY (`Array`) and fail the create on a fresh install.
// db_update_table() needs the array form, so the canonical definitions keep it.
gexport_setup_table();

$creates = array_filter($GLOBALS['__test_db_calls'], function ($call) {
return $call['fn'] === 'api_plugin_db_table_create';
});

expect($creates)->not->toBeEmpty();

foreach ($creates as $call) {
expect($call['data']['primary'])->toBeString();
}

expect(gexport_graph_exports_table_data()['primary'])->toBeArray()
->and(gexport_graph_exports_tasks_table_data()['primary'])->toBeArray();
});
Loading