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
24 changes: 22 additions & 2 deletions includes/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,26 @@ function webseer_table_map(): array {
];
}

/**
* 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 webseer_create_definition(array $data): array {
if (isset($data['primary']) && is_array($data['primary'])) {
$data['primary'] = implode('`,`', $data['primary']);
}

return $data;
}

/**
* Creates all of this plugin's database tables through Cacti's tracked
* plugin table API. Called from plugin_webseer_install() during
Expand All @@ -267,7 +287,7 @@ function webseer_table_map(): array {
*/
function plugin_webseer_setup_table() {
foreach (webseer_table_map() as $table => $data) {
api_plugin_db_table_create('webseer', $table, $data);
api_plugin_db_table_create('webseer', $table, webseer_create_definition($data));
}
}

Expand Down Expand Up @@ -295,7 +315,7 @@ function webseer_upgrade_tables(): bool {
if (db_table_exists($table)) {
db_update_table($table, $data);
} else {
api_plugin_db_table_create('webseer', $table, $data);
api_plugin_db_table_create('webseer', $table, webseer_create_definition($data));
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Integration/PluginLifecycleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@
}
});

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 shared definitions keep it.
plugin_webseer_install();

$creates = array_filter($GLOBALS['__test_db_calls'], fn ($call) => $call['fn'] === 'api_plugin_db_table_create');

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

foreach ($creates as $call) {
// params = [$plugin, $data]
expect($call['params'][1]['primary'])->toBeString();
}

expect(webseer_servers_table_data()['primary'])->toBeArray();
});

it('drops every plugin table on uninstall', function () {
plugin_webseer_uninstall();

Expand Down
Loading