Skip to content

Fix fatal PRIMARY KEY (Array) on fresh install from array primary key - #56

Merged
TheWitness merged 3 commits into
mainfrom
fix/create-path-array-primary
Oct 15, 2026
Merged

TheWitness merged 3 commits into
mainfrom
fix/create-path-array-primary

Conversation

@TheWitness

Copy link
Copy Markdown
Member

Problem

On a fresh install, slowlog_setup_table_new() hands api_plugin_db_table_create() several table definitions whose primary is declared in the array form, e.g.:

$data['primary'] = ['logentry'];                                   // plugin_slowlog_details
$data['primary'] = ['logid', 'logentry', 'method'];                // details_methods
$data['primary'] = ['logid', 'scope', 'scope_key', 'metric'];      // stats

Down to this plugin''s compat = 1.2.29 floor, api_plugin_db_table_create() (lib/plugins.php) interpolates the primary key straight into the DDL string:

$sql .= ",\n PRIMARY KEY (`" . $data['primary'] . '`)';

With an array, PHP renders it as PRIMARY KEY (`Array`), so the CREATE TABLE fails and the table is never created on Cacti 1.2.29-1.2.31.

This is the fresh-install mirror of the upgrade-path fatal fixed in plugin_routerconfigs #191: api_plugin_db_table_create() requires the scalar primary form, while db_update_table() (used here for plugin_slowlog_details) requires the array form.

Fix

Add a slowlog_create_definition() helper that flattens an array primary to the backtick-joined scalar form, and wrap every api_plugin_db_table_create() call with it (a no-op for the tables that already declare a scalar primary). The slowlog_details_table_data() definition keeps the array form that db_update_table() still requires on the upgrade path. This mirrors the helpers already present in plugin_maint, plugin_apcupsd, plugin_exlist, plugin_lsflog and plugin_stream.

Validation

  • php -l includes/database.php - no syntax errors.
  • Added regression coverage asserting every create call receives a scalar primary while slowlog_details_table_data() keeps the array form.

@TheWitness
TheWitness requested review from bmfmancini and xmacan and a balanced review from Copilot October 15, 2026 13:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Two existing tests still expect array-valued create-call primary keys and fail after normalization.

1 open finding
What changed in this PR

Fixes fresh-install table creation by adapting primary-key definitions to Cacti’s create API while preserving the array form needed for upgrades.

Changes:

  • Normalizes primary keys at every table-create call.
  • Adds regression coverage for create and upgrade definition formats.
File Description
tests/​Unit/​SetupTableApiUsageTest.php Adds primary-key format assertions.
includes/​database.php Adds and applies the create-definition adapter.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread tests/Unit/SetupTableApiUsageTest.php
@TheWitness
TheWitness requested a review from xmacan October 15, 2026 13:37
xmacan
xmacan previously approved these changes Oct 15, 2026
xmacan
xmacan previously approved these changes Oct 15, 2026
@TheWitness
TheWitness merged commit 47f1e1c into main Oct 15, 2026
5 checks passed
@TheWitness
TheWitness deleted the fix/create-path-array-primary branch October 15, 2026 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants