fix: execute replication CREATE TABLE SQL from SHOW CREATE output#267
fix: execute replication CREATE TABLE SQL from SHOW CREATE output#267somethingwithproof wants to merge 2 commits intoCacti:developfrom
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Fixes syslog rule replication bootstrap when the destination rules table is missing by executing the actual CREATE TABLE statement obtained from SHOW CREATE TABLE, and adds a regression test + CI coverage to prevent regressions.
Changes:
- Fix
syslog_replace_data()to execute$create_sql(fromSHOW CREATE TABLE) when creating a missing replicated rules table. - Add a standalone regression script validating the “missing table => CREATE + TRUNCATE + bulk upsert” path.
- Run regression scripts in CI when
tests/regressionexists, and add a changelog entry for issue #258.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
setup.php |
Fixes CREATE TABLE execution during replication bootstrap for missing rules tables. |
tests/regression/issue258_replication_create_sql_test.php |
Adds regression coverage for the missing-table bootstrap path. |
.github/workflows/plugin-ci-workflow.yml |
Runs regression scripts in CI when present. |
CHANGELOG.md |
Documents the fix for issue #258. |
| if (!syslog_db_table_exists($table)) { | ||
| syslog_db_execute($create); | ||
| syslog_db_execute($create_sql); | ||
| syslog_db_execute("TRUNCATE TABLE $table"); |
There was a problem hiding this comment.
$create_sql is only set when the SHOW CREATE TABLE result contains expected keys. If db_fetch_row() returns an empty/false value (e.g., permissions issue, table missing, or unexpected column label), this will call syslog_db_execute($create_sql) with an undefined variable and fail to create the destination table. Initialize $create_sql (e.g., to an empty string) and add a guard that logs/returns early when the CREATE statement cannot be derived before executing it.
| if (!function_exists('db_fetch_row')) { | ||
| function db_fetch_row($sql) { | ||
| return array('Create Table' => 'CREATE TABLE `syslog_alert` (`id` INT NOT NULL)'); | ||
| } | ||
| } | ||
|
|
||
| if (!function_exists('syslog_db_table_exists')) { | ||
| function syslog_db_table_exists($table) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| if (!function_exists('syslog_db_execute')) { | ||
| function syslog_db_execute($sql) { | ||
| $GLOBALS['syslog_replace_data_execute_calls'][] = $sql; | ||
|
|
||
| return true; | ||
| } |
There was a problem hiding this comment.
This regression script conditionally defines DB wrapper stubs using function_exists(). If someone runs it in an environment where Cacti/syslog functions are already loaded (so the stubs are skipped), the script will call the real syslog_replace_data() against a real DB and may create/modify syslog rule tables before failing its assertions. Consider adding an explicit safety check up front (e.g., abort when core functions like db_fetch_row/syslog_db_execute already exist) to ensure the test only runs in an isolated/stubbed context.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Addressed review feedback in the latest commit:
|
Summary
CREATE TABLEstatement text ($create_sql) when a replicated rules table is missingcreate_sqlbefore execution insyslog_replace_data()syslog_replace_data()table bootstrap pathtests/regression/*.phpexistValidation
Fixes #258