diff --git a/CHANGELOG.md b/CHANGELOG.md index fa1d1229..e96db578 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,55 +6,59 @@ --- develop --- -* docs: Refresh README release/source compatibility, installation guidance and source-checked behavior; add published release notes and 5.0 upgrade guidance -* bug: Keep log column resizing within the results area on both log tabs, preserve message ellipsis, and use an accessible Font Awesome export button +* issue#472: Purge Syslog Devices (Utilities) crashed with "Call to undefined function syslog_db_execute()" because the hook never connected to the Syslog database; it now connects first, reports connection failures, qualifies the Syslog database, and uses NOT EXISTS so a NULL host_id no longer prevents purging +* issue#473: Syslog Status: choosing a card in Add card now appends it (Cacti's select2 dropdowns report changes through jQuery, which the card controller's native listener missed), and the Refresh interval is saved per user through a callback and refreshes the cards in place instead of reloading the page +* issue#473: Bound every log query at the current time so empty future partitions are always pruned, whatever the filter's own date range +* issue#473: The partition rebuild CLI now refuses to start while Syslog processing is enabled and holds a database lock that syslog_process.php also takes (on a connection of its own, so nested locks cannot release it); copies stored and removed messages without seq into a replacement whose AUTO_INCREMENT starts above the original high-water mark; verifies each 50,000-row batch by checksum and checkpoints it for resumable retries; widens the incoming queue in place to keep its sequence identities; and refuses legacy DATETIME history before changing anything +* issue: Keep log column resizing within the results area on both log tabs, preserve message ellipsis, and use an accessible Font Awesome export button +* issue: Load grouped message details only on expansion, 50 at a time, preserving the displayed query's date range and filters instead of rendering every occurrence into hidden rows +* issue: Place Cancel before an active submit-type Save button in Save Search As, support Enter through a separate dialog form and the existing callback, and show a Font Awesome refresh glyph that spins only while results callbacks are running +* issue: Remove message-row and lightbulb hover tooltips, keep details in the click-open flyout, and fix the recommendation column at 20 pixels with a centered header +* issue: Remove recommendation callbacks on message hover, show a theme-aware lightbulb before messages with matching recommendations, and include the CSRF token in the details flyout's fallback POST +* issue: Trim leading and trailing parentheses and square brackets from incoming program names before reference lookup and rule processing +* feature#473: Add an Hourly or Daily (default) Partitioning Level under the renamed Data Retention and Partitioning settings, the install advisor (replacing the unused Partitions per Day option) and silent install options. A changed level takes effect only when cli/upgrade_partitioning.php (or its new --partition=hourly|daily option) rebuilds the tables; until then maintenance follows the existing layout and Settings and Status show the change as pending. Hourly tables keep at least two days of partitions ahead so dMaxValue reorganizations stay instant +* feature#473: Refuse to save Syslog settings, install, or rebuild when retention, pre-create window and partitioning level need more partitions than the database server supports (8192, or 1024 before MariaDB 10.0.4 and MySQL 5.7), or when Hourly is combined with Indefinite retention +* feature: Add a data-preserving partition rebuild CLI with optional --convert-ms, verified 50,000-row batch copies, interrupted-copy recovery, and database-size, extra-disk-space and several-hour maintenance warnings * feature: Standardize both schema and partition CLI help with Cacti-style version banners and options, separate maintenance warnings, and replace the unreleased millisecond converter with --convert-ms -* feature: Add a data-preserving partition rebuild CLI with optional --convert-ms, verified 10,000-row copies, interrupted-copy recovery, and database-size, extra-disk-space and several-hour maintenance warnings -* bug: Trim leading and trailing parentheses and square brackets from incoming program names before reference lookup and rule processing -* bug: Load grouped message details only on expansion, 50 at a time, preserving the displayed query's date range and filters instead of rendering every occurrence into hidden rows -* bug: Place Cancel before an active submit-type Save button in Save Search As, support Enter through a separate dialog form and the existing callback, and show a Font Awesome refresh glyph that spins only while results callbacks are running -* bug: Remove message-row and lightbulb hover tooltips, keep details in the click-open flyout, and fix the recommendation column at 20 pixels with a centered header -* bug: Remove recommendation callbacks on message hover, show a theme-aware lightbulb before messages with matching recommendations, and include the CSRF token in the details flyout's fallback POST --- 5.0 --- -* bug: Exercise the grouped-message detail query directly in regression tests without dynamic code evaluation or interpolated fixture table names, and cover unavailable or failing gzip operations -* bug: Cache populated Syslog realm ID lookups per display name for the current request while continuing to check the current user's permissions on each call -* bug: Align the saved-search Default label beneath Name and use Cacti's toggle switch, restore full-message hover tooltips alongside the details flyout, and retain the active search and date range when fetching grouped message details -* bug: Re-importing a full Recommendations export rejected the 860 bundled records whose identifiers contain characters such as `|`, `#` or brackets (an over-strict identifier check). Imports now accept any identifier without spaces, write each rejected record to the Cacti log with its position, identifier and reason plus a one-line summary, and warn about identifiers that can never match a message -* bug: Keep a leading `%` as a literal part of a Cisco recommendation identifier; it must match the message word as written instead of being silently added or removed -* feature: The Recommendations Import button, filters and import submission now run as callbacks instead of full page loads, like the other Syslog Settings pages; the list explains how recommendations are matched -* bug: The Alert and Removal rule filter panel no longer shows the Dark theme's tiled jQuery UI background in Dark and Deepness, and Deepness gets its own Syslog surface colors instead of Dark's (Deepness reuses Dark's jQuery UI styles, which the Syslog views sample) +* issue: Align the saved-search Default label beneath Name and use Cacti's toggle switch, restore full-message hover tooltips alongside the details flyout, and retain the active search and date range when fetching grouped message details +* issue: Cache populated Syslog realm ID lookups per display name for the current request while continuing to check the current user's permissions on each call +* issue: Creating an Alert or Removal rule with the SQL Expression match type failed in the browser with "An invalid form control is not focusable" because the hidden Filter Builder's required fields still took part in form validation; the hidden builder is now disabled +* issue: Exercise the grouped-message detail query directly in regression tests without dynamic code evaluation or interpolated fixture table names, and cover unavailable or failing gzip operations +* issue: Keep a leading `%` as a literal part of a Cisco recommendation identifier; it must match the message word as written instead of being silently added or removed +* issue: Re-importing a full Recommendations export rejected the 860 bundled records whose identifiers contain characters such as `|`, `#` or brackets (an over-strict identifier check). Imports now accept any identifier without spaces, write each rejected record to the Cacti log with its position, identifier and reason plus a one-line summary, and warn about identifiers that can never match a message +* issue: SQL Expression report emails showed an empty Host column and logged PHP "Undefined array key" warnings; the report query now resolves host and program names +* issue: The Alert and Removal rule filter panel no longer shows the Dark theme's tiled jQuery UI background in Dark and Deepness, and Deepness gets its own Syslog surface colors instead of Dark's (Deepness reuses Dark's jQuery UI styles, which the Syslog views sample) +* issue: The rule Test preview dialogs on the Alert, Removal and Report Rules pages are no longer modal, so the rule can be edited while the preview stays open +* issue: Theme fixes: in Midwinter the rule Filter Builder text and buttons inherited the theme's mid-grey text at about 3.5:1 contrast and now use Midwinter's own palette (including its light color mode); in Paper Plane, whose buttons are light text on a dark fill, the Syslog search builder's add buttons were white on white and now fall back to the body text color; the rule Filter Builder no longer overflows the form column on the rule edit pages * feature: Report Rules gain the Filter Builder (previously listed but never shown) and a Test button that previews matches against the most recent stored messages; Filter Builder reports are compiled to bound SQL and processed like the other match types -* bug: Creating an Alert or Removal rule with the SQL Expression match type failed in the browser with "An invalid form control is not focusable" because the hidden Filter Builder's required fields still took part in form validation; the hidden builder is now disabled -* bug: The rule Test preview dialogs on the Alert, Removal and Report Rules pages are no longer modal, so the rule can be edited while the preview stays open +* feature: The Recommendations Import button, filters and import submission now run as callbacks instead of full page loads, like the other Syslog Settings pages; the list explains how recommendations are matched * feature: The Report Rules Message Match String is a resizable text area, taller for SQL Expressions -* bug: SQL Expression report emails showed an empty Host column and logged PHP "Undefined array key" warnings; the report query now resolves host and program names -* bug: Theme fixes: in Midwinter the rule Filter Builder text and buttons inherited the theme's mid-grey text at about 3.5:1 contrast and now use Midwinter's own palette (including its light color mode); in Paper Plane, whose buttons are light text on a dark fill, the Syslog search builder's add buttons were white on white and now fall back to the body text color; the rule Filter Builder no longer overflows the form column on the rule edit pages -* docs: State plainly in the README that replacing the plugin files does not upgrade the plugin. Upgrades never start automatically during ordinary page or poller requests; start one with `cli/syslog_schema.php --upgrade` (recommended, runs outside the web server) or by disabling and enabling the plugin to open the Upgrade Advisor (runs inside that web request). Until then Plugin Management shows the old version and new pages return Permission Denied; the version shown there, not `--audit`, confirms that an upgrade completed -* feature: Add Recommendations (issue #46). A vendor-neutral `syslog_recommendations` table maps a message identifier (a Cisco `%FACILITY-SEV-MNEMONIC`, a Juniper `RPD_OSPF_NBRDOWN`, and so on) to an explanation and a recommended action. Hovering over a row in the Syslog tab now asks the server for a matching recommendation and shows its explanation and recommended action in the tooltip. Console > Syslog Settings > Recommendations (Syslog Administration realm) adds, edits and deletes records and imports and exports them as JSON, using field names drawn from Sigma and the Elastic Common Schema so catalogues from other suppliers can be converted. docs/cisco.json.gz, converted from the Cisco Error Message Database (CEMDB) shipped with php-syslog-ng, is loaded once on install or upgrade without overwriting existing records +* feature#46: Add Recommendations. A vendor-neutral `syslog_recommendations` table maps a message identifier (a Cisco `%FACILITY-SEV-MNEMONIC`, a Juniper `RPD_OSPF_NBRDOWN`, and so on) to an explanation and a recommended action. Hovering over a row in the Syslog tab now asks the server for a matching recommendation and shows its explanation and recommended action in the tooltip. Console > Syslog Settings > Recommendations (Syslog Administration realm) adds, edits and deletes records and imports and exports them as JSON, using field names drawn from Sigma and the Elastic Common Schema so catalogues from other suppliers can be converted. docs/cisco.json.gz, converted from the Cisco Error Message Database (CEMDB) shipped with php-syslog-ng, is loaded once on install or upgrade without overwriting existing records * feature: Add an optional Juniper EX Series recommendation catalogue in docs/juniper.json.gz, convert the Cisco seed to docs/cisco.json.gz, accept .json and .json.gz recommendation imports, and export gzip-compressed JSON by default while retaining one-time Cisco seeding * issue#458: Initialize and validate the active tab before saving view defaults, avoiding a TypeError that disables Syslog and stops processing * issue#459: Explain that SQL Expression rule previews search only queued incoming messages, so an empty preview is not mistaken for an alert-processing failure * issue#459: Legacy "Facility is" removal and alert rules compared the facility name (for example `local7`) with the numeric `facility_id` column of the incoming table, which MySQL evaluates as facility 0 (kern). Removal rules therefore discarded or transferred kern messages instead of their own facility, and facility alerts never fired. The name is now resolved through the facility reference table, as the rule preview, rule editor and stored-table removal already did. `syslog_process.php --debug` now names each removal rule that matched and how many messages it took, and notes host-level alerts skipped for having no matches, so a removal rule that takes messages before an alert sees them is visible -* bug: Filter Builder conditions such as `Host != router01` or `Program != sshd` on Report Rules and retroactive removal against stored messages selected only the excluded host or program, because mapping names to ids dropped the negation -* bug: Testing a Filter Builder report after visiting the Alert or Removal Rules page could send the previous page's conditions; Test now uses the builder that belongs to the form being tested -* feature: Clicking a System Logs or Alert Logs row opens a Message details flyout on the right, in the style of the active theme. It requests the record from the server (`syslog.php?action=message_details`, which returns JSON for stored, removed and alert log records) and shows its details, the full message, the matching recommendations and the rule and filter actions. The flyout survives the page's auto refresh, and Copy link produces a `details=` address that reopens the record -* bug: The System Logs and Alert Logs tables always showed a horizontal scroll bar: the column-resize handle of the last header cell sits 5px outside the table and overflowed the results area. In Midwinter the results area also collapsed to zero width beside the theme's floated tab bar, pushing the whole table off the page +* issue#461: Fit message previews to the available column width instead of cutting them to 100 characters +* issue: Clicking a host or program on the log view, or Filter by device or program in the message details, reloaded the whole page, and clicking the same host again added a duplicate condition. Filters now apply as a callback into the page, values already limiting the search are marked and are not added twice, and the search, clear and refresh actions also reload as callbacks (exports still download through a hidden frame) +* issue: Dragging a column edge on the log view did nothing and then sorted the column. The message column claimed all spare width under the automatic table layout, which ignored the dragged widths, and releasing the drag over the header text counted as a sort click. The log tables now use the fixed layout so resized widths hold, the message column absorbs the change instead of the neighbouring column, and the click that ends a resize no longer sorts +* issue: Filter Builder conditions such as `Host != router01` or `Program != sshd` on Report Rules and retroactive removal against stored messages selected only the excluded host or program, because mapping names to ids dropped the negation +* issue: Opening one of the log view's menus (Manage, View options) or dropdowns left any others open, so several could be open at once; opening one now closes the rest, and clicking elsewhere on the page closes them all +* issue: Testing a Filter Builder report after visiting the Alert or Removal Rules page could send the previous page's conditions; Test now uses the builder that belongs to the form being tested +* issue: The grouped Count column now shows thousands separators for the current language +* issue: The log view's action and menu controls (Search, Clear, Refresh, Export, Save and the Manage menu) are now real ` +
${card('latest_run', 'Latest run')}${card('workers', 'Workers')}
+ + + +`; + +(async () => { + const browser = await chromium.launch({headless: true, executablePath: process.env.CHROMIUM_PATH, args: ['--no-sandbox']}); + const page = await browser.newPage(); + const errors = []; + const posts = []; + let navigations = 0; + let rendered = 0; + + page.on('pageerror', error => errors.push(error.message)); + page.on('framenavigated', frame => { if (frame === page.mainFrame()) navigations++; }); + + const routes = { + '/jquery.js': path.join(cacti, 'include/js/jquery.js'), + '/select2.js': path.join(cacti, 'include/js/select2.js'), + '/select2.css': path.join(cacti, 'include/themes/cacti/select2.css'), + '/cards.js': path.join(root, 'js/cards.js'), + '/syslog_dashboard.js': path.join(root, 'js/syslog_dashboard.js') + }; + + await page.route('https://fixture/**', route => { + const url = new URL(route.request().url()); + + if (url.pathname === '/') { + return route.fulfill({body: page_html, contentType: 'text/html; charset=utf-8'}); + } + + if (url.pathname === '/syslog.php' && route.request().method() === 'POST') { + const data = Object.fromEntries(new URLSearchParams(route.request().postData() || '')); + assert.equal(data.__csrf_magic, 'fixture', 'Status callbacks carry the CSRF token'); + posts.push(data); + + if (data.action === 'status_card') { + rendered++; + + return route.fulfill({contentType: 'application/json', body: JSON.stringify({card: data.card, html: card(data.card, 'Rendered ' + data.card + ' ' + rendered)})}); + } + + return route.fulfill({contentType: 'application/json', body: JSON.stringify({ok: true})}); + } + + return routes[url.pathname] ? route.fulfill({path: routes[url.pathname], contentType: 'text/javascript'}) : route.fulfill({status: 404, body: ''}); + }); + + await page.goto('https://fixture/'); + await page.waitForTimeout(200); + assert.deepEqual(errors, []); + assert.equal(await page.locator('#syslog_status_add').evaluate(select => select.classList.contains('select2-hidden-accessible')), true, 'The fixture uses select2 like Cacti'); + const loaded = navigations; + + // Adding a card through the select2 widget appends it and saves the layout. + await page.locator('#syslog_status_add + .select2 .select2-selection').click(); + await page.locator('.select2-results__option', {hasText: 'Storage'}).click(); + await page.waitForFunction(() => document.querySelectorAll('#syslog_status_grid .ccCard').length === 3); + assert.deepEqual(await page.locator('#syslog_status_grid .ccCard').evaluateAll(cards => cards.map(item => item.dataset.card)), ['latest_run', 'workers', 'storage'], 'The added card goes to the end'); + await page.waitForTimeout(100); + const layout = posts.filter(post => post.action === 'status_layout').pop(); + assert.ok(layout, 'Adding a card saves the layout'); + assert.deepEqual(JSON.parse(layout.layout).order, ['latest_run', 'workers', 'storage']); + assert.equal(await page.locator('#syslog_status_add').inputValue(), '', 'The add control resets'); + assert.equal(await page.locator('#syslog_status_add option[value="storage"]').count(), 0, 'An added card leaves the catalogue'); + + // Changing the interval saves it through a callback and refreshes cards in place. + posts.length = 0; + await page.locator('#syslog_status_refresh + .select2 .select2-selection').click(); + await page.locator('.select2-results__option', {hasText: '1 Second'}).click(); + await page.waitForFunction(() => document.querySelectorAll('#syslog_status_grid .ccCard .ccCardBody').length === 3 + && [...document.querySelectorAll('#syslog_status_grid .ccCardBody')].every(body => body.textContent.startsWith('Rendered')), null, {timeout: 5000}); + assert.deepEqual(posts.find(post => post.action === 'status_refresh'), {action: 'status_refresh', tab: 'status', refresh: '1', __csrf_magic: 'fixture'}); + assert.deepEqual(posts.filter(post => post.action === 'status_card').map(post => post.card).sort((a, b) => a.localeCompare(b)), ['latest_run', 'storage', 'workers']); + + // Never stops the timer; the manual refresh still uses callbacks. + await page.locator('#syslog_status_refresh + .select2 .select2-selection').click(); + await page.locator('.select2-results__option', {hasText: 'Never'}).click(); + await page.waitForTimeout(1500); + posts.length = 0; + await page.waitForTimeout(1500); + assert.equal(posts.filter(post => post.action === 'status_card').length, 0, 'Never stops automatic refreshes'); + await page.locator('#syslog_status_refresh_now').click(); + await page.waitForFunction(() => document.querySelectorAll('#syslog_status_refresh_now .fa-spin').length === 0); + await page.waitForTimeout(100); + assert.equal(posts.filter(post => post.action === 'status_card').length, 3, 'Refresh now re-renders every card'); + + assert.equal(navigations, loaded, 'No status interaction reloads the page'); + assert.deepEqual(errors, []); + await browser.close(); + console.log('Status card add and callback refresh passed (browser fixture)'); +})().catch(error => { + console.error(error); + process.exit(1); +}); diff --git a/tests/regression/cli_schema_upgrade_test.php b/tests/regression/cli_schema_upgrade_test.php index 78f25aad..a2f692fb 100644 --- a/tests/regression/cli_schema_upgrade_test.php +++ b/tests/regression/cli_schema_upgrade_test.php @@ -21,7 +21,7 @@ function check($condition, $message) { $syslogdb_default = 'syslog'; function syslog_connect() { return true; } function cacti_sizeof($value) { return count($value); } -function read_config_option($name) { return ''; } +function read_config_option($name) { return $name === 'syslog_retention' ? '0' : ''; } // The precision helpers live above syslog_setup_table_new() in schema.php and // are not part of the evaluated slice, so stub them. Repair runs assert plain // second-precision table construction. @@ -36,6 +36,8 @@ function set_config_option($name, $value, $force = false) { $options_saved[$name] = $value; } function syslog_install_storage_engine($engine, $options) { return $engine; } +function syslog_partition_settings_error(...$args) { return ''; } +function syslog_partition_server_limit(...$args) { return 8192; } function syslog_db_fetch_row($sql) { return []; } function syslog_db_execute($sql) { global $queries; @@ -62,6 +64,12 @@ function syslog_ensure_message_digest_columns() {} function syslog_db_add_column($table, $column, $log = true) { return true; } function syslog_db_fetch_cell_prepared($sql, $params = [], $col_name = '', $log = true) { return '50'; } function syslog_status_set($name, $value) { return true; } +// Repair of a missing table runs under the processing lock. +$lock_calls = []; +$lock_free = true; +function syslog_processing_lock_acquire() { $GLOBALS['lock_calls'][] = 'acquire'; return $GLOBALS['lock_free']; } +function syslog_processing_lock_release() { $GLOBALS['lock_calls'][] = 'release'; } +function cacti_log(...$args) {} function db_update_table($table, $definition, $create, $upgrade, $connection) { return true; } // syslog_setup_table_new() routes table creation through this wrapper; forward // to the stubbed db_update_table() so the real builder runs without a database. @@ -83,6 +91,14 @@ function syslog_db_table_exists($table, $log = true) { } check(!isset($options_saved['syslog_rows']), 'Repair reset an existing preference'); check(substr_count(implode("\n", $queries), 'INSERT INTO') === 2, 'Lookup tables must only be seeded when missing'); + +// The stubbed builder never records `syslog` as created, so it stays missing. +$lock_free = false; +check(!syslog_ensure_table_structures() && $lock_calls === ['acquire'], 'Repair must not recreate a missing table while another session holds the processing lock'); +$lock_free = true; +$lock_calls = []; +syslog_ensure_table_structures(); +check($lock_calls === ['acquire', 'release'], 'Repair of a missing table must run under the processing lock'); $config['poller_id'] = 2; $all_tables_exist = true; check(syslog_ensure_table_structures(), 'Remote schema repair failed'); diff --git a/tests/regression/partition_rebuild_database_test.php b/tests/regression/partition_rebuild_database_test.php index d115a2cc..3c984dea 100644 --- a/tests/regression/partition_rebuild_database_test.php +++ b/tests/regression/partition_rebuild_database_test.php @@ -23,6 +23,7 @@ $copy_attempts = 0; $fail_copy_at = 0; $fail_prefix = ''; +$corrupt_after_copy = ''; $batch_sizes = []; $checks = 0; @@ -35,7 +36,8 @@ function rebuild_check(bool $ok, string $message): void { } function syslog_connect() { return true; } -function read_config_option($name) { return $GLOBALS['options'][$name] ?? ''; } +function __($text, ...$args) { array_pop($args); return $args ? vsprintf($text, $args) : $text; } +function read_config_option($name, $force = false) { return $GLOBALS['options'][$name] ?? ''; } function set_config_option($name, $value) { $GLOBALS['options'][$name] = $value; } function cacti_log(...$args) {} function syslog_db_execute_prepared($sql, $params = [], ...$args) { @@ -54,6 +56,11 @@ function syslog_db_execute_prepared($sql, $params = [], ...$args) { if ($copy) { $GLOBALS['batch_sizes'][] = $query->rowCount(); + + if ($GLOBALS['corrupt_after_copy'] !== '') { + $GLOBALS['pdo']->exec($GLOBALS['corrupt_after_copy']); + $GLOBALS['corrupt_after_copy'] = ''; + } } return $ok; @@ -65,11 +72,12 @@ function syslog_db_fetch_assoc_prepared($sql, $params = [], ...$args) { return $query->fetchAll(); } -function syslog_db_fetch_row($sql) { - $rows = syslog_db_fetch_assoc_prepared($sql); +function syslog_db_fetch_row_prepared($sql, $params = [], ...$args) { + $rows = syslog_db_fetch_assoc_prepared($sql, $params); return $rows[0] ?? []; } +function syslog_db_fetch_row($sql) { return syslog_db_fetch_row_prepared($sql); } function syslog_db_fetch_cell_prepared($sql, $params = [], ...$args) { $query = $GLOBALS['pdo']->prepare($sql); $query->execute($params); @@ -82,8 +90,25 @@ function syslog_db_table_exists($table, ...$args) { WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?', [$GLOBALS['syslogdb_default'], $table]); } function syslog_partition_ahead_days() { return (int) read_config_option('syslog_partition_ahead_days'); } +// No Cacti process table here, so no transfer workers are registered. +function db_fetch_assoc($sql, ...$args) { return []; } require dirname(__DIR__, 2) . '/includes/schema.php'; + +// The partitioning-level helpers come from the real functions.php source. +$helpers = file_get_contents(dirname(__DIR__, 2) . '/includes/functions.php'); +$helper_code = ''; + +foreach (['syslog_partition_periods', 'syslog_partition_requested_granularity', 'syslog_partition_ahead_days_for', + 'syslog_partition_server_limit', 'syslog_partition_projected_count', 'syslog_partition_settings_error', + 'syslog_processing_lock_name'] as $helper) { + if (preg_match('/^function ' . $helper . '\(.*?^\}/ms', $helpers, $match) !== 1) { + throw new RuntimeException("Cannot find $helper"); + } + + $helper_code .= $match[0] . "\n"; +} + $source = file_get_contents(dirname(__DIR__, 2) . '/cli/upgrade_partitioning.php'); $offset = strpos($source, "\nfunction syslog_upart_run("); @@ -94,17 +119,22 @@ function syslog_partition_ahead_days() { return (int) read_config_option('syslog $fixture = tempnam(sys_get_temp_dir(), 'syslog-rebuild-'); try { - file_put_contents($fixture, "exec("SET SESSION time_zone = '+00:00'"); $pdo->exec("SET SESSION sql_mode = 'STRICT_TRANS_TABLES,NO_AUTO_VALUE_ON_ZERO'"); $pdo->exec("CREATE TABLE syslog ( - seq BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, logtime DATETIME NOT NULL, + seq BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, logtime TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00', message TEXT COLLATE utf8mb4_general_ci NOT NULL, nullable_value VARCHAR(40), replication_source_event_id BIGINT UNSIGNED, PRIMARY KEY(seq,logtime), KEY logtime(logtime) - ) ENGINE=InnoDB PARTITION BY RANGE(TO_DAYS(logtime)) + ) ENGINE=InnoDB PARTITION BY RANGE(UNIX_TIMESTAMP(logtime)) (PARTITION legacy VALUES LESS THAN MAXVALUE)"); $pdo->exec("INSERT INTO syslog (seq,logtime,message,replication_source_event_id) SELECT seq,TIMESTAMPADD(SECOND,seq,UTC_DATE()),CONCAT('Event ',seq),seq FROM seq_1_to_10005"); @@ -129,55 +159,101 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In (0,'0000-00-00 00:00:00','zero',NULL,0), (10000,TIMESTAMPADD(SECOND,10001,UTC_DATE()),'repeated seq','',10000), (10006,UTC_DATE() - INTERVAL 400 DAY,'outside retention',NULL,10006), - (18446744073709551600,UTC_DATE(),UNHEX('410042'),NULL,18446744073709551600)"); - $pdo->exec('ALTER TABLE syslog AUTO_INCREMENT=18446744073709551610'); + (20000,UTC_DATE(),UNHEX('410042'),NULL,20000)"); + $pdo->exec('ALTER TABLE syslog AUTO_INCREMENT=1000000'); $total = syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog'); + $original_order = $pdo->query('SELECT message FROM syslog ORDER BY seq, logtime')->fetchAll(PDO::FETCH_COLUMN); $fail_copy_at = 2; - rebuild_check(!rebuild_table('syslog'), 'The injected second-batch failure must stop the rebuild'); + rebuild_check(!rebuild_table('syslog', false), 'The injected second-batch failure must stop the rebuild'); rebuild_check(syslog_db_table_exists('syslog_rebuild'), 'A failed copy must retain the original'); - rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === 10000, 'The first copy is exactly 10000 rows'); + rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === 10000, 'The first verified copy is exactly 10000 rows'); + rebuild_check((int) syslog_db_fetch_cell('SELECT copied FROM syslog_rebuild_progress WHERE table_name=\'syslog\'') === 10000, 'The checkpoint records the verified batch'); rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_rebuild') === (int) $total, 'The original is intact'); $fail_copy_at = 0; - $pdo->exec("UPDATE syslog SET message='event 1' WHERE seq=1"); - rebuild_check(!rebuild_table('syslog'), 'Case-only differences must fail binary value verification'); + $pdo->exec("INSERT INTO syslog (logtime,message) VALUES (UTC_DATE(),'unverified partial batch')"); + $corrupt_after_copy = 'UPDATE syslog SET message=LOWER(message) ORDER BY seq DESC LIMIT 1'; + rebuild_check(!rebuild_table('syslog', false), 'Case-only differences must fail binary batch verification'); rebuild_check(syslog_db_table_exists('syslog_rebuild'), 'A value mismatch must retain the original even with matching counts'); - $pdo->exec('UPDATE syslog AS copied JOIN syslog_rebuild AS source - ON copied.seq=source.seq AND copied.logtime=source.logtime SET copied.message=source.message'); - rebuild_check(rebuild_table('syslog'), 'A repaired copy resumes successfully'); + rebuild_check((int) syslog_db_fetch_cell('SELECT copied FROM syslog_rebuild_progress WHERE table_name=\'syslog\'') === 10000, 'A mismatched batch is never checkpointed'); + rebuild_check(rebuild_table('syslog', false), 'A retry discards unverified rows and resumes after the checkpoint'); rebuild_check(!syslog_db_table_exists('syslog_rebuild'), 'Verified originals are removed automatically'); - rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === (int) $total, 'Every row survives retries'); - rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog WHERE seq=10000') === 2, 'The composite cursor preserves repeated sequence numbers across batches'); - rebuild_check(syslog_db_fetch_cell('SELECT HEX(message) FROM syslog WHERE seq=18446744073709551600') === '410042', 'Binary message contents survive'); - rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog WHERE seq=0') === 1, 'Zero sequence values must not be regenerated'); - rebuild_check((string) syslog_db_fetch_cell("SELECT AUTO_INCREMENT FROM information_schema.TABLES - WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='syslog'") === '18446744073709551610', 'Unsigned high-water marks must not overflow PHP integers'); + rebuild_check(!syslog_db_table_exists('syslog_rebuild_progress'), 'The checkpoint table is removed after the last rebuild'); + rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === (int) $total, 'Every row survives retries exactly once'); + rebuild_check((int) syslog_db_fetch_cell("SELECT COUNT(*) FROM syslog WHERE message IN ('unverified partial batch')") === 0, 'Unverified replacement rows are discarded'); + rebuild_check($pdo->query('SELECT message FROM syslog ORDER BY seq')->fetchAll(PDO::FETCH_COLUMN) === $original_order, 'Renumbered rows keep their original order, including repeated sequence values'); + rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(DISTINCT seq) FROM syslog') === (int) $total, 'Repeated original sequence values receive distinct new values'); + rebuild_check((int) syslog_db_fetch_cell('SELECT MIN(seq) FROM syslog') >= 1000000, 'Copied rows never reuse a sequence value at or below the original high-water mark'); + rebuild_check(syslog_db_fetch_cell("SELECT HEX(message) FROM syslog WHERE replication_source_event_id=20000") === '410042', 'Binary message contents survive'); + rebuild_check(syslog_db_fetch_cell("SELECT logtime FROM syslog WHERE message='zero'") === '0000-00-00 00:00:00', 'Zero timestamps survive'); + rebuild_check((int) syslog_db_fetch_cell("SELECT replication_source_event_id FROM syslog WHERE message='Event 5'") === 5, 'Non-sequence columns are copied unchanged'); + + $pdo->exec('CREATE TABLE syslog_removed (seq BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, logtime TIMESTAMP NOT NULL, message TEXT) ENGINE=InnoDB'); + $pdo->exec("INSERT INTO syslog_removed (logtime,message) SELECT UTC_DATE(),'near the limit' FROM seq_1_to_10"); + $pdo->exec('ALTER TABLE syslog_removed AUTO_INCREMENT=18446744073709551610'); + rebuild_check(!rebuild_table('syslog_removed', false), 'Too few remaining sequence values must stop the rebuild'); + rebuild_check(!syslog_db_table_exists('syslog_removed_rebuild') && (int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_removed') === 10, 'The sequence headroom check runs before any change'); + $pdo->exec('DROP TABLE syslog_removed'); $pdo->exec('CREATE TABLE syslog_removed (seq BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, logtime TIMESTAMP NOT NULL, message TEXT) ENGINE=Aria'); rebuild_check(rebuild_table('syslog_removed', false, 'Aria'), 'An empty legacy table can be repartitioned'); rebuild_check(syslog_detect_logtime_precision('syslog_removed') === 'sec', 'A plain rebuild must preserve second precision'); rebuild_check(syslog_db_fetch_cell("SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='syslog_removed'") === 'Aria', 'Configured Aria engine is supported'); - $pdo->exec('ALTER TABLE syslog_removed REMOVE PARTITIONING'); - $pdo->exec('ALTER TABLE syslog_removed MODIFY logtime DATETIME NOT NULL'); - $pdo->exec("SET SESSION time_zone = '+05:30'"); - $pdo->exec("INSERT INTO syslog_removed VALUES(8,CONCAT(UTC_DATE(),' 12:00:00'),'removed')"); + $pdo->exec("INSERT INTO syslog_removed (logtime,message) VALUES (CONCAT(UTC_DATE(),' 12:00:00'),'removed')"); $removed_epoch = syslog_db_fetch_cell('SELECT UNIX_TIMESTAMP(logtime) FROM syslog_removed'); $fail_prefix = "CREATE TABLE `$syslogdb_default`.`syslog_removed`"; rebuild_check(!rebuild_table('syslog_removed', false, 'Aria'), 'A create failure after rename must be recoverable'); rebuild_check(!syslog_db_table_exists('syslog_removed') && syslog_db_table_exists('syslog_removed_rebuild'), 'The renamed original survives a create failure'); + $fail_prefix = "ALTER TABLE `$syslogdb_default`.`syslog_removed` AUTO_INCREMENT"; + rebuild_check(!rebuild_table('syslog_removed', false, 'Aria'), 'A setup failure before the checkpoint must be recoverable'); + rebuild_check(syslog_db_table_exists('syslog_removed') && (int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_removed') === 0, 'An interrupted setup leaves an empty replacement'); $fail_prefix = ''; - rebuild_check(rebuild_table('syslog_removed', false, 'Aria'), 'A missing replacement can be recreated on retry'); + rebuild_check(rebuild_table('syslog_removed', false, 'Aria'), 'An empty replacement without a checkpoint is recreated on retry'); rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_removed') === 1, 'Aria data survives recovery'); - rebuild_check(syslog_db_fetch_cell('SELECT UNIX_TIMESTAMP(logtime) FROM syslog_removed') === $removed_epoch, 'Legacy DATETIME history preserves its original non-UTC interpretation'); + rebuild_check(syslog_db_fetch_cell('SELECT UNIX_TIMESTAMP(logtime) FROM syslog_removed') === $removed_epoch, 'TIMESTAMP history keeps its UTC instant'); + + $pdo->exec('ALTER TABLE syslog_removed REMOVE PARTITIONING'); + $pdo->exec('ALTER TABLE syslog_removed MODIFY logtime DATETIME NOT NULL'); + rebuild_check(!rebuild_table('syslog_removed', false, 'Aria'), 'Preserving a legacy DATETIME table is refused'); + rebuild_check(!syslog_db_table_exists('syslog_removed_rebuild'), 'The DATETIME refusal happens before any change'); + $options['syslog_enabled'] = ''; + ob_start(); + rebuild_check(!syslog_upart_run(true, false), 'The command refuses legacy DATETIME history before any change'); + ob_end_clean(); + rebuild_check(syslog_db_fetch_cell("SELECT DATA_TYPE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() + AND TABLE_NAME='syslog_removed' AND COLUMN_NAME='logtime'") === 'datetime' && !syslog_db_table_exists('syslog_removed_rebuild'), 'A refused command leaves DATETIME tables untouched'); + rebuild_check(is_free_processing_lock(), 'The processing lock is released after a refusal'); + ob_start(); + rebuild_check(syslog_upart_rebuild('syslog_removed', false, false, 'InnoDB', 2, 2), 'Explicit discard mode replaces a legacy DATETIME table'); + ob_end_clean(); + rebuild_check(syslog_detect_logtime_precision('syslog_removed') === 'sec' && syslog_db_fetch_cell("SELECT DATA_TYPE FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='syslog_removed' AND COLUMN_NAME='logtime'") === 'timestamp', 'A discarded DATETIME table is recreated as TIMESTAMP'); + + set_config_option('syslog_enabled', 'on'); + ob_start(); + rebuild_check(!syslog_upart_run(true, false), 'The command refuses to run while Syslog processing is enabled'); + ob_end_clean(); + rebuild_check(is_free_processing_lock(), 'Enabled processing is refused before taking the processing lock'); + set_config_option('syslog_enabled', ''); + // A second connection stands in for a running syslog_process.php. + $holder = new PDO($dsn, 'root', getenv('SYSLOG_TEST_MYSQL_PASSWORD') ?: '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); + $holder->prepare('SELECT GET_LOCK(?, 0)')->execute([syslog_processing_lock_name()]); + ob_start(); + rebuild_check(!syslog_upart_run(true, false), 'The command refuses to run while a processing run holds the processing lock'); + ob_end_clean(); + rebuild_check(!syslog_db_table_exists('syslog_rebuild') && !syslog_db_table_exists('syslog_removed_rebuild'), 'Refusals leave the message tables untouched'); + $holder = null; $pdo->exec('CREATE TABLE syslog_incoming (seq BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, logtime TIMESTAMP NOT NULL, status INT, message TEXT) ENGINE=InnoDB'); $pdo->exec("INSERT INTO syslog_incoming VALUES(30,UTC_DATE(),1,'queued'),(40,UTC_DATE(),0,'new')"); $pdo->exec("INSERT INTO syslog_incoming SELECT seq+100,UTC_DATE(),0,CONCAT('queued ',seq) FROM seq_1_to_10001"); $incoming_count = syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_incoming'); + $incoming_seq = syslog_db_fetch_cell('SELECT SUM(seq) FROM syslog_incoming'); $pdo->exec("SET SESSION time_zone = '+05:30'"); - $pdo->exec('CREATE TABLE syslog_replication_output (seq BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, logtime DATETIME NOT NULL, message TEXT) ENGINE=InnoDB'); + $pdo->exec('CREATE TABLE syslog_replication_output (seq BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, logtime TIMESTAMP NOT NULL, message TEXT) ENGINE=InnoDB'); $pdo->exec("INSERT INTO syslog_replication_output VALUES(4,CONCAT(UTC_DATE(),' 12:00:00'),'undelivered')"); $outbox_epoch = syslog_db_fetch_cell('SELECT UNIX_TIMESTAMP(logtime) FROM syslog_replication_output'); + $stored_epochs = syslog_db_fetch_cell("SELECT SUM(UNIX_TIMESTAMP(logtime)) FROM syslog WHERE logtime > '1970-01-01'"); $options['syslog_install_logtime_precision'] = 'sec'; $fail_prefix = "ALTER TABLE `$syslogdb_default`.`syslog_replication_output`"; ob_start(); @@ -187,17 +263,19 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In && str_contains($output, 'SEVERAL HOURS'), 'Size, extra-space and duration warnings are mandatory with --confirm'); rebuild_check($options['syslog_install_logtime_precision'] === 'sec', 'Partial conversion must not update the precision setting'); rebuild_check(syslog_db_fetch_cell('SELECT @@SESSION.time_zone') === '+05:30', 'The original non-UTC session zone is restored after failure'); - rebuild_check((int) syslog_db_fetch_cell("SELECT IS_FREE_LOCK('" . hash('sha256', $syslogdb_default . '.syslog_upgrade_partitioning') . "')") === 1, 'The named lock is released after failure'); + rebuild_check(is_free_processing_lock(), 'The processing lock is released after failure'); $fail_prefix = ''; ob_start(); rebuild_check(syslog_upart_run(true, true), 'The complete pipeline can finish after the outbox failure is fixed'); ob_end_clean(); rebuild_check($options['syslog_install_logtime_precision'] === 'ms', 'Only complete conversion records millisecond precision'); - rebuild_check(syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_incoming') === $incoming_count, 'All queued incoming messages survive multiple sequence-only batches'); + rebuild_check(syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_incoming') === $incoming_count + && syslog_db_fetch_cell('SELECT SUM(seq) FROM syslog_incoming') === $incoming_seq, 'Queued incoming messages keep their sequence identities'); rebuild_check((int) syslog_db_fetch_cell('SELECT status FROM syslog_incoming WHERE seq=30') === 1, 'Incoming processing state survives'); rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_replication_output') === 1, 'Undelivered replication messages survive'); - rebuild_check((string) syslog_db_fetch_cell('SELECT UNIX_TIMESTAMP(logtime) FROM syslog_replication_output') === $outbox_epoch . '.000', 'A legacy DATETIME outbox uses its original session time zone'); - rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === (int) $total, 'Previously converted history survives subsequent pipeline retries'); + rebuild_check((string) syslog_db_fetch_cell('SELECT UNIX_TIMESTAMP(logtime) FROM syslog_replication_output') === $outbox_epoch . '.000', 'The outbox keeps its UTC instant'); + rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === (int) $total, 'Converted history keeps every row'); + rebuild_check(syslog_db_fetch_cell("SELECT SUM(UNIX_TIMESTAMP(logtime)) FROM syslog WHERE logtime > '1970-01-01'") === $stored_epochs . '.000', 'Converted history keeps every UTC instant'); rebuild_check((int) syslog_db_fetch_cell("SELECT COUNT(*) FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME IN ('syslog','syslog_removed') AND REPLACE(PARTITION_EXPRESSION,'`','')='unix_timestamp(logtime)'") > 2, 'Stored and removed tables use epoch partitions'); @@ -207,7 +285,31 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_removed') === 0, 'Only explicit discard mode empties a rebuilt table'); rebuild_check(!syslog_db_table_exists('syslog_removed_rebuild'), 'Explicit discard removes the original after verification'); rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_replication_output') === 1, 'Discard mode does not delete the outbox'); - rebuild_check(max($batch_sizes) <= 10000, 'No stored or incoming INSERT SELECT may copy more than 10000 rows'); + rebuild_check(max($batch_sizes) <= 50000, 'No stored INSERT SELECT may copy more than the 50000-row default batch'); + + ob_start(); + rebuild_check(syslog_upart_rebuild('syslog', false, true, 'InnoDB', 2, 2, 'hour', 8192), 'A preserving rebuild converts daily partitions to hourly'); + ob_end_clean(); + rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === (int) $total, 'Every row survives the hourly rebuild'); + rebuild_check(syslog_upart_layout_ok('syslog', 'InnoDB', 'hour') && !syslog_upart_layout_ok('syslog', 'InnoDB', 'day'), 'The replacement has contiguous UTC hourly partitions'); + $now = (int) syslog_db_fetch_cell('SELECT UNIX_TIMESTAMP()'); + $future = syslog_db_fetch_assoc_prepared('SELECT PARTITION_NAME FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = \'syslog\' AND PARTITION_DESCRIPTION <> \'MAXVALUE\' AND CAST(PARTITION_DESCRIPTION AS UNSIGNED) > ?', [$now + 7200]); + $future = array_column($future, 'PARTITION_NAME'); + rebuild_check(count($future) >= 46, 'Hourly partitions exist at least two days ahead'); + $plan = syslog_db_fetch_assoc_prepared('EXPLAIN PARTITIONS SELECT COUNT(*) FROM syslog WHERE logtime >= FROM_UNIXTIME(?) AND logtime < FROM_UNIXTIME(?)', + [$now - 86400, $now + 1]); + $scanned = explode(',', (string) ($plan[0]['partitions'] ?? '')); + rebuild_check($scanned !== [''] && !array_intersect($scanned, array_merge($future, ['dMaxValue'])), 'The automatic upper bound prunes every empty future partition'); + ob_start(); + rebuild_check(syslog_upart_rebuild('syslog', false, true, 'InnoDB', 2, 2, 'day', 8192), 'A preserving rebuild converts hourly partitions back to daily'); + ob_end_clean(); + rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === (int) $total && syslog_upart_layout_ok('syslog', 'InnoDB', 'day'), 'Every row survives the daily rebuild'); + rebuild_check(syslog_upart_partition_sql(400, 2, null, null, null, 'hour', 8192) === null, 'Hourly plans above the server partition limit are refused'); + ob_start(); + rebuild_check(syslog_upart_run(true, false, false), 'The discard command runs when no table needs a column-type check'); + ob_end_clean(); + rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === 0 && is_free_processing_lock(), 'The discard command empties the rebuilt tables and releases the lock'); print "$checks partition rebuild database checks passed.\n"; } finally { diff --git a/tests/regression/review_fixes_database_test.php b/tests/regression/review_fixes_database_test.php index 83f9c092..2b9b3216 100644 --- a/tests/regression/review_fixes_database_test.php +++ b/tests/regression/review_fixes_database_test.php @@ -157,6 +157,54 @@ $result = syslog_process_alerts($max); check($result['syslog_alarms'] === 1, 'A host-level rule that uses an asterisk must fire'); + + // ---- Hourly partitioning: install, then poller maintenance --------------- + $options['syslog_partition_ahead_days'] = '1'; + $options['syslog_partition_recover_limit'] = '31'; + $pdo->exec('DROP TABLE syslog_removed'); + syslog_setup_table_new(['upgrade_type' => 'truncate', 'engine' => 'InnoDB', 'days' => 2, 'db_type' => 'part', 'partitioning' => 'hour'], false); + + $hourly = static fn() => $pdo->query("SELECT PARTITION_NAME FROM information_schema.PARTITIONS + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'syslog' ORDER BY PARTITION_ORDINAL_POSITION")->fetchAll(PDO::FETCH_COLUMN); + $names = $hourly(); + $now = (int) $pdo->query('SELECT UNIX_TIMESTAMP()')->fetchColumn(); + + check($options['syslog_partition_granularity'] === 'hour', 'An hourly install records the partitioning level'); + check(end($names) === 'dMaxValue' && preg_grep('/^h\d{10}$/', $names) === array_slice($names, 0, -1), 'An hourly install creates only hourly partitions before dMaxValue'); + check(in_array(syslog_partition_name('hour', $now + 2 * 86400), $names, true), 'An hourly install keeps at least two days ahead, even with a 1-day window'); + check(syslog_partition_report_state('syslog') && syslog_partition_report_state('syslog_removed'), 'Hourly layouts pass the partition state check'); + check(syslog_partition_layout_granularity('syslog_removed') === 'hour', 'syslog_removed follows the hourly layout'); + + // Merge the last six future hours back into dMaxValue, then let maintenance + // heal the gap while Settings already asks for Daily: the layout must win. + $tail = array_slice($names, -7, 6); + $pdo->exec('ALTER TABLE syslog REORGANIZE PARTITION ' . implode(', ', $tail) . ', dMaxValue INTO (PARTITION dMaxValue VALUES LESS THAN MAXVALUE)'); + $options['syslog_partition_granularity'] = 'day'; + syslog_partition_manage(); + $healed = $hourly(); + + check(array_intersect($tail, $healed) === $tail && preg_grep('/^d\d{8}$/', $healed) === [], 'Maintenance heals the hourly gap and ignores the pending Daily setting'); + check(syslog_status_get()['partition_maintenance_outcome'] === 'success', 'Hourly maintenance completes'); + + // ---- Repair sizes a missing table by the current retention ------------- + // 365 days of hourly history would exceed 8,192 partitions; repair must use + // the 3-day retention now configured, not the install-time 365 days. + $options['syslog_install_days'] = '365'; + $options['syslog_retention'] = '3'; + $options['syslog_partition_granularity'] = 'hour'; + $pdo->exec('DROP TABLE syslog'); + syslog_setup_table_new([], true); + $names = $hourly(); + + check($names !== [] && count($names) < 24 * 8, 'Repair recreates a missing hourly table from the current retention'); + check(strcmp($names[0], syslog_partition_name('hour', $now - 4 * 86400)) >= 0, 'Repair keeps no history beyond the current retention'); + + // Hourly partitioning cannot keep Indefinite history: repair refuses before any DDL. + $options['syslog_retention'] = '0'; + $pdo->exec('DROP TABLE syslog'); + syslog_setup_table_new([], true); + + check(!syslog_db_table_exists('syslog', false), 'Repair creates no table whose partition plan is refused'); } catch (Throwable $error) { $failures[] = $error->getMessage() . ' (' . basename($error->getFile()) . ':' . $error->getLine() . ')'; } finally {