-
Notifications
You must be signed in to change notification settings - Fork 18
fix: execute replication CREATE TABLE SQL from SHOW CREATE output #267
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| <?php | ||
|
|
||
| $existing_functions = array( | ||
| 'db_fetch_row', | ||
| 'syslog_db_table_exists', | ||
| 'syslog_db_execute', | ||
| 'syslog_db_execute_prepared' | ||
| ); | ||
|
|
||
| foreach ($existing_functions as $existing_function) { | ||
| if (function_exists($existing_function)) { | ||
| fwrite(STDERR, "This test must run in isolated mode; function already exists: $existing_function\n"); | ||
| exit(1); | ||
| } | ||
| } | ||
|
|
||
| if (!function_exists('cacti_sizeof')) { | ||
| function cacti_sizeof($value) { | ||
| if (is_array($value) || $value instanceof Countable) { | ||
| return count($value); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| $GLOBALS['syslog_replace_data_execute_calls'] = array(); | ||
| $GLOBALS['syslog_replace_data_prepared_calls'] = array(); | ||
| $GLOBALS['issue258_logs'] = array(); | ||
| $GLOBALS['issue258_show_create'] = array('Create Table' => 'CREATE TABLE `syslog_alert` (`id` INT NOT NULL)'); | ||
|
|
||
| if (!function_exists('db_fetch_row')) { | ||
| function db_fetch_row($sql) { | ||
| return $GLOBALS['issue258_show_create']; | ||
| } | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
|
Comment on lines
+32
to
+49
|
||
| } | ||
|
|
||
| if (!function_exists('syslog_db_execute_prepared')) { | ||
| function syslog_db_execute_prepared($sql, $params) { | ||
| $GLOBALS['syslog_replace_data_prepared_calls'][] = array( | ||
| 'sql' => $sql, | ||
| 'params' => $params | ||
| ); | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
|
|
||
| if (!function_exists('cacti_log')) { | ||
| function cacti_log($message, $output = false, $facility = 'SYSTEM', $level = '') { | ||
| $GLOBALS['issue258_logs'][] = $message; | ||
| } | ||
| } | ||
|
|
||
| require_once dirname(__DIR__, 2) . '/setup.php'; | ||
|
|
||
| $data = array( | ||
| array( | ||
| 'id' => 1, | ||
| 'hash' => 'abc123', | ||
| 'name' => 'sample' | ||
| ) | ||
| ); | ||
|
|
||
| syslog_replace_data('syslog_alert', $data); | ||
|
|
||
| $executed = $GLOBALS['syslog_replace_data_execute_calls']; | ||
|
|
||
| if (cacti_sizeof($executed) < 2) { | ||
| fwrite(STDERR, "Expected CREATE + TRUNCATE calls to run.\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| if ($executed[0] !== 'CREATE TABLE `syslog_alert` (`id` INT NOT NULL)') { | ||
| fwrite(STDERR, "Expected CREATE TABLE SQL to be executed from create_sql.\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| if ($executed[1] !== 'TRUNCATE TABLE syslog_alert') { | ||
| fwrite(STDERR, "Expected TRUNCATE TABLE to run after CREATE TABLE.\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| $prepared = $GLOBALS['syslog_replace_data_prepared_calls']; | ||
|
|
||
| if (cacti_sizeof($prepared) !== 1) { | ||
| fwrite(STDERR, "Expected a single prepared INSERT statement execution.\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| $GLOBALS['syslog_replace_data_execute_calls'] = array(); | ||
| $GLOBALS['syslog_replace_data_prepared_calls'] = array(); | ||
| $GLOBALS['issue258_show_create'] = false; | ||
|
|
||
| syslog_replace_data('syslog_alert', $data); | ||
|
|
||
| if (cacti_sizeof($GLOBALS['syslog_replace_data_execute_calls']) !== 0) { | ||
| fwrite(STDERR, "Expected no execute calls when CREATE TABLE SQL is unavailable.\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| if (cacti_sizeof($GLOBALS['syslog_replace_data_prepared_calls']) !== 0) { | ||
| fwrite(STDERR, "Expected no prepared inserts when CREATE TABLE SQL is unavailable.\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| echo "issue258_replication_create_sql_test passed\n"; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$create_sqlis only set when theSHOW CREATE TABLEresult contains expected keys. Ifdb_fetch_row()returns an empty/false value (e.g., permissions issue, table missing, or unexpected column label), this will callsyslog_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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed --
$create_sqlis initialized to''before theSHOW CREATE TABLEfetch, and the guard at line 833 returns with a warning log when the value cannot be derived.