diff --git a/CHANGELOG.md b/CHANGELOG.md index da7a6449..a2b9d1a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ --- develop --- +* issue#440: Avoid repeating successful schema operations and seed the default dashboard after schema setup, preventing duplicate table creation during installation and upgrade + * feature: Add an install-time timestamp precision choice for the logtime column (issue #325). New MariaDB installs default to millisecond TIMESTAMP(3), which preserves sub-second ordering and still RANGE partition-prunes; MySQL installs are fixed to second precision because MySQL cannot prune a fractional partitioned column, and an explicit millisecond request on MySQL is rejected. Upgrades never migrate the schema automatically; switch an existing MariaDB install with the destructive cli/updatedb_convert_ms.php utility during a maintenance window. README documents forwarder configuration (rsyslog, Cisco, Juniper, Arista, syslog-ng) for delivering sub-second timestamps * feature: Add a manifest.json that drives upgrade-time "housecleaning": when genuinely upgrading from an older version, syslog_prune_files() (run from the shared syslog_check_upgrade() path so every version-aware entry point - the plugin upgrade hook, the re-enable path and cli/syslog_schema.php - cleans up) removes files and directories that older layouts shipped but that have since moved or been deleted (for example the old top-level database.php/functions.php now living under includes/), so a stale module can no longer redeclare a function and fatal the plugin on load after an upgrade. User-data paths (config.php, config_local.php) and VCS metadata (including .git/.md descendants nested under a tombstoned directory) are never touched, tombstones reached through a symlinked parent or that resolve outside the plugin are refused, a fresh install or same-version repair leaves the dev-only tests/ tree intact, and a CI check (tests/bin/validate-manifest.php) keeps the manifest's expected-file list from drifting * bug: Stop partition recovery from wedging with MySQL error 1493 ('VALUES LESS THAN value must be strictly increasing') when an existing partition boundary already covers the day being created. Both syslog_partition_check() and syslog_partition_create() decided a daily partition was missing purely by name, so a table carried over from an older TO_DAYS schema, or left inconsistent by an interrupted 4.2-to-4.11 upgrade, would repeatedly emit a REORGANIZE of dMaxValue that MySQL rejected, leaving writes parked in the dMaxValue safety partition. Both now compare the target boundary against the highest existing concrete boundary, so already-covered days no longer count as missing, no longer consume the bounded per-run recovery budget, and are treated as a successful no-op, while genuine gaps are still created (issue #434) diff --git a/includes/schema.php b/includes/schema.php index 59cea297..f2d537c7 100644 --- a/includes/schema.php +++ b/includes/schema.php @@ -56,13 +56,33 @@ function syslog_cacti_db_array_primary_safe(): bool { * @return bool True on success, false on failure. */ function syslog_db_update_table($table, $data, $removecolumns = false, $log = true, $db_conn = false) { + global $syslog_cnn; + + static $applied = []; + + // Core caches missing tables even after CREATE. Do not repeat an identical + // successful schema operation in this request (connect and upgrade can overlap). + $connection = is_object($db_conn) ? $db_conn : $syslog_cnn; + $key = (is_object($connection) ? spl_object_hash($connection) : 'default') . ':' . $table; + $definition = [$connection, $data, $removecolumns]; + + if (isset($applied[$key]) && $applied[$key] === $definition && syslog_db_table_exists($table, false)) { + return true; + } + if (isset($data['primary']) && is_array($data['primary']) && !syslog_cacti_db_array_primary_safe() && !syslog_db_table_exists($table, false)) { $data['primary'] = implode('`,`', $data['primary']); } - return db_update_table($table, $data, $removecolumns, $log, $db_conn); + $result = db_update_table($table, $data, $removecolumns, $log, $db_conn); + + if ($result) { + $applied[$key] = $definition; + } + + return $result; } /** diff --git a/setup.php b/setup.php index 2909c064..9911ec03 100644 --- a/setup.php +++ b/setup.php @@ -1189,12 +1189,12 @@ function syslog_check_upgrade(bool $force = false): bool { return true; } -/** Create dashboard structures and seed the shared starter during install/upgrade. */ +/** Seed the shared starter after the install/upgrade caller creates its tables. */ function syslog_dashboard_seed_default(): void { global $syslogdb_default; - syslog_ensure_dashboard_tables(); - + // Both callers already ensured the schema. Repeating that work can reuse + // Cacti's cached missing-table result and try to create the tables twice. $lock = 'syslog_dashboard_' . md5($syslogdb_default); if (!syslog_db_fetch_cell_prepared('SELECT GET_LOCK(?, 5)', [$lock])) { diff --git a/tests/Unit/DashboardDefaultTest.php b/tests/Unit/DashboardDefaultTest.php index 0ce24b55..13770fac 100644 --- a/tests/Unit/DashboardDefaultTest.php +++ b/tests/Unit/DashboardDefaultTest.php @@ -4,8 +4,17 @@ syslog_load_plugin_source('setup.php'); syslog_load_plugin_source('includes/schema.php'); syslog_load_plugin_source('lib/syslog_dashboard.php'); - test_override('db_update_table', fn () => true); + $GLOBALS['syslog_cnn'] = new stdClass(); + $schema_calls = []; + test_override('syslog_db_table_exists', fn () => true); + test_override('db_update_table', function ($table) use (&$schema_calls) { + $schema_calls[] = $table; + return true; + }); $GLOBALS['syslogdb_default'] = 'syslog'; + // Install and upgrade create these before seeding. A second schema pass + // can hit core's cached negative existence result from the first pass. + syslog_ensure_dashboard_tables(); test_override('syslog_db_fetch_insert_id', fn () => 42); $existing = false; $calls = []; @@ -23,6 +32,7 @@ }); syslog_dashboard_seed_default(); + expect($schema_calls)->toBe(['syslog_dashboards', 'syslog_dashboard_panels']); expect($calls[0])->toBe('START TRANSACTION'); expect($calls[1][1][1])->toBe('default'); expect($calls[1][1][2])->toBe(''); @@ -44,10 +54,12 @@ $existing = true; syslog_dashboard_seed_default(); expect($calls)->toBe([]); + expect($schema_calls)->toHaveCount(2); $existing = false; $fail = true; syslog_dashboard_seed_default(); expect(end($calls))->toBe('ROLLBACK'); expect($calls)->not->toContain('COMMIT'); + expect($schema_calls)->toHaveCount(2); }); diff --git a/tests/Unit/SchemaPrimaryKeyCompatTest.php b/tests/Unit/SchemaPrimaryKeyCompatTest.php index c5adbc8f..2a7111ed 100644 --- a/tests/Unit/SchemaPrimaryKeyCompatTest.php +++ b/tests/Unit/SchemaPrimaryKeyCompatTest.php @@ -90,3 +90,31 @@ function schema_primary_capture(string $cacti_version, bool $table_exists): arra expect(syslog_db_update_table('syslog_dashboards_perm', $definition, false, true, new stdClass()))->toBeTrue() ->and($captured->primary)->toBe('dashboard_id`,`type`,`item_id'); }); + + +it('does not replay a successful definition but retries failures and changed definitions', function () { + syslog_load_plugin_source('includes/schema.php'); + $GLOBALS['config']['cacti_version'] = '1.2.32'; + $connection = new stdClass(); + $exists = false; + $succeed = false; + $calls = 0; + test_override('syslog_db_table_exists', function () use (&$exists) { return $exists; }); + test_override('db_update_table', function () use (&$calls, &$succeed, &$exists) { + $calls++; + $exists = $succeed; + return $succeed; + }); + $definition = ['columns' => [['name' => 'id', 'type' => 'int']], 'primary' => ['id']]; + expect(syslog_db_update_table('syslog_repeat_probe', $definition, false, true, $connection))->toBeFalse(); + $succeed = true; + expect(syslog_db_update_table('syslog_repeat_probe', $definition, false, true, $connection))->toBeTrue(); + expect(syslog_db_update_table('syslog_repeat_probe', $definition, false, true, $connection))->toBeTrue(); + expect($calls)->toBe(2); + $definition['columns'][] = ['name' => 'label', 'type' => 'text']; + expect(syslog_db_update_table('syslog_repeat_probe', $definition, false, true, $connection))->toBeTrue(); + expect($calls)->toBe(3); + $exists = false; + expect(syslog_db_update_table('syslog_repeat_probe', $definition, false, true, $connection))->toBeTrue(); + expect($calls)->toBe(4); +});