From 7a5114b97b2d3d413e5b165571f7fc2b8ef8ff7d Mon Sep 17 00:00:00 2001 From: TheWitness <1439914+TheWitness@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:37:32 +0000 Subject: [PATCH 1/3] Partition 802.1x history and include it in rebuilds Apply the existing scan-date partition lifecycle to 802.1x history while retaining the port retention setting and supporting interrupted upgrades. Bump the plugin version to 5.1. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- CHANGELOG.md | 1 + INFO | 2 +- README.md | 17 ++++---- cli/upgrade_partitioning.php | 16 ++++---- includes/database.php | 9 +++++ lib/mactrack_activity.php | 4 +- lib/mactrack_functions.php | 10 +++-- lib/mactrack_partition_ops.php | 13 +++--- lib/mactrack_partition_rebuild.php | 16 ++++---- lib/mactrack_partitions.php | 2 +- locales/po/cacti.pot | 26 +++++++----- mactrack_utilities.php | 9 ++++- mactrack_view_dot1x.php | 1 + setup.php | 2 +- tests/Unit/ArpDot1xRetentionTest.php | 2 +- tests/Unit/PartitionMaintainTest.php | 47 ++++++++++++++++++++++ tests/Unit/PartitionRebuildTest.php | 60 ++++++++++++++++++++-------- 17 files changed, 171 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e89c9de..59451c33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,7 @@ * issue: Mac Addresses are not displaying correctly * issue: Mactrack does not shutdown properly due to incorrect use of unregister_process * issue: Make the MacTrack tab script repoint only the MacTrack main tab rather than whichever main tab is selected +* issue: Partition 802.1x history on new installations and include it in partition maintenance and upgrades * issue: Pin the CI database image and shared test dependencies, disable persisted checkout credentials, and limit workflow permissions and runtime * issue: Reach PHPStan level 8 with zero errors across all first-party PHP and add native function return type declarations, using no baselines or inline suppressions * issue: Relocate the per-theme override stylesheets from themes// to css// alongside the base css/mactrack.css diff --git a/INFO b/INFO index e65d9efd..d65cfee6 100644 --- a/INFO +++ b/INFO @@ -21,7 +21,7 @@ [info] name = mactrack -version = 5.0 +version = 5.1 longname = Device Tracking author = The Cacti Group email = diff --git a/README.md b/README.md index 50dc069b..3f5fbc6c 100644 --- a/README.md +++ b/README.md @@ -53,10 +53,11 @@ Add Mactrack Site - Console -> Mactrack -> Sites Add/import devices - Console -> Mactrack -> Devices -> Add device or Management -> Choose host -> Import into Mactrack Database Create new or enable prepared Device Types - Console -> Mactrack -> Device Types -## Port and IP history partitioning +## Port, IP and 802.1x history partitioning MacTrack keeps the MAC address history in `mac_track_ports` and the IP address -history in `mac_track_ips`. Both tables are partitioned by scan date (MySQL 8.0+ +history in `mac_track_ips`, and 802.1x events in `mac_track_dot1x`. These tables +are partitioned by scan date (MySQL 8.0+ or MariaDB with the partition plugin), so retention drops whole expired partitions instead of deleting millions of rows, and a search for one scan reads one partition. A server without partitioning support keeps unpartitioned tables, @@ -64,7 +65,7 @@ which MacTrack fully supports. ### Setting it up -**New installs** create both tables weekly partitioned, starting Monday at +**New installs** create all three tables weekly partitioned, starting Monday at 00:00 UTC, and the poller's daily maintenance keeps the next two days of partitions created ahead of collection. Nothing needs to be done. @@ -76,8 +77,8 @@ the size, then run the rebuild once: Scan* (maintained by the poller; the rebuild script cannot build these two), or *Disabled* for unpartitioned tables. It sets the size of the partitions created from now on and never rewrites the ones that exist. -2. Set the retention (**Data Retention** for MAC addresses and **Data Retention - IPs**). Partitions older than the retention are dropped by the daily +2. Set the retention (**Data Retention** for MAC addresses and 802.1x events, + and **Data Retention IPs** for IP addresses). Partitions older than the retention are dropped by the daily maintenance, so with weekly partitions up to one extra week of history can stay. 3. Rebuild the existing history to match, either with **MacTrack Utilities > @@ -94,7 +95,7 @@ Run it as the user that runs the Cacti poller, off hours (see below). ### Checking and changing the scheme -`cli/upgrade_partitioning.php` checks both tables against the scheme and rebuilds +`cli/upgrade_partitioning.php` checks all three tables against the scheme and rebuilds only the ones that have drifted: - Without `--partition` the scheme is the **Port and IP Address Partition Size** setting. This @@ -109,7 +110,7 @@ A table is reported as drifted, and rebuilt, when it is not partitioned, uses th earlier daily layout, has partitions MacTrack does not recognize, mixes sizes, keeps rows outside every partition, or is not the selected size. A table that already follows the scheme is left alone, and the script says so. **View -Partition Status** in the Utilities shows the layout of both tables, the last +Partition Status** in the Utilities shows the layout of all three tables, the last rebuild and the last maintenance. ### What a rebuild does @@ -135,7 +136,7 @@ process registry for 24 hours, so two runs cannot overlap). - Scheduled scans and the daily database maintenance are paused. If the maintenance window passes meanwhile, the poller runs the maintenance as soon as the rebuild is over. -- MAC address and IP address searching shows a maintenance-mode message, and its +- MAC address, IP address and 802.1x searching shows a maintenance-mode message, and its results are incomplete until the rebuild has finished. - Changes to the history are refused with a message until the rebuild has finished, including after an interruption: removing devices and sites, diff --git a/cli/upgrade_partitioning.php b/cli/upgrade_partitioning.php index abc37e35..54460e28 100644 --- a/cli/upgrade_partitioning.php +++ b/cli/upgrade_partitioning.php @@ -25,9 +25,9 @@ /* * cli/upgrade_partitioning.php * - * Checks how mac_track_ports (MAC addresses) and mac_track_ips (IP - * addresses) are partitioned against the selected scheme and, when they have - * drifted from it or the scheme is being changed, rebuilds them one after the + * Checks how mac_track_ports (MAC addresses), mac_track_ips (IP + * addresses) and mac_track_dot1x are partitioned against the selected scheme + * and, when they have drifted from it or the scheme is changing, rebuilds them one after the * other: * * 1. the table is renamed to _rebuild; @@ -200,7 +200,7 @@ function display_help(): void { mactrack_upart_out(''); mactrack_upart_out('usage: upgrade_partitioning.php [--partition=daily|weekly|disabled] [--debug] [--version] [--help]'); mactrack_upart_out(''); - mactrack_upart_out('Checks how the MAC address (mac_track_ports) and IP address (mac_track_ips)'); + mactrack_upart_out('Checks the MAC address (mac_track_ports), IP address (mac_track_ips) and 802.1x (mac_track_dot1x)'); mactrack_upart_out('tables are partitioned. Without --partition, the Port and IP Address Partition Size setting'); mactrack_upart_out('is the scheme; a table that already follows it is left alone and reported.'); mactrack_upart_out('A table that has drifted from the scheme, or is not partitioned as asked, is'); @@ -330,7 +330,7 @@ function mactrack_upart_upgrade_active(): bool { } /** - * Checks both tables against the scheme and rebuilds the ones that need it. + * Checks the history tables against the scheme and rebuilds the ones that need it. * * @param string $scheme day, week or disabled. * @param bool $explicit True when --partition chose it, so it is saved as @@ -436,7 +436,7 @@ function mactrack_upart_assess_all(string $scheme): array { * follows the scheme, rebuild or resume when it must be rebuilt, and missing * when the table does not exist. * - * @param string $table mac_track_ports or mac_track_ips. + * @param string $table History table. * @param string $scheme day, week or disabled. * * @return array{action: string, reason: string} @@ -515,7 +515,7 @@ function mactrack_upart_rebuild_all(array $tables, string $scheme, int $pause): set_config_option('mt_partition_converting', (string) $started); - mactrack_upart_out('WARNING: Scanning is paused and MAC and IP address searching is in maintenance mode until this finishes.'); + mactrack_upart_out('WARNING: Scanning is paused and MAC, IP and 802.1x searching is in maintenance mode until this finishes.'); mactrack_upart_out('WARNING: Allow disk space for a second copy of each table and its indexes; this can take hours.'); $total = ['ok' => true, 'reason' => '', 'rows' => 0, 'partitions' => 0, 'granularity' => $scheme]; @@ -629,7 +629,7 @@ function mactrack_upart_plan(array $epochs, string $scheme, int $retention_days, * when it already has the scheme, and the copy restarts from the newest scan * date it holds. * - * @param string $table mac_track_ports or mac_track_ips. + * @param string $table History table. * @param string $scheme day, week or disabled. * @param callable|null $progress Called as ($table, $done, $total, $scan_date, $rows) after each scan date. * @param int $pause Seconds between DDL retries; the rename waits twice as long. diff --git a/includes/database.php b/includes/database.php index a1db6d72..622e6b89 100644 --- a/includes/database.php +++ b/includes/database.php @@ -1600,8 +1600,17 @@ function mactrack_setup_database(bool $notify_seed_failure = false): bool { $data['keys'][] = ['name' => 'scan_date', 'columns' => 'scan_date']; $data['type'] = 'InnoDB'; $data['comment'] = ''; + $dot1x_existed = db_table_exists('mac_track_dot1x'); api_plugin_db_table_create('mactrack', 'mac_track_dot1x', $data); + if (!$dot1x_existed) { + include_once(__DIR__ . '/../lib/mactrack_partitions.php'); + include_once(__DIR__ . '/../lib/mactrack_partition_ops.php'); + include_once(__DIR__ . '/../lib/mactrack_partition_rebuild.php'); + + mactrack_partition_fresh_install(null, 'mac_track_dot1x'); + } + $data = []; $data['columns'][] = ['name' => 'device_id', 'unsigned' => true, 'type' => 'int(10)', 'NULL' => false, 'default' => '0']; $data['columns'][] = ['name' => 'ifIndex', 'unsigned' => true, 'type' => 'int(10)', 'NULL' => false]; diff --git a/lib/mactrack_activity.php b/lib/mactrack_activity.php index 3fba8c9d..5b906291 100644 --- a/lib/mactrack_activity.php +++ b/lib/mactrack_activity.php @@ -266,7 +266,7 @@ function mactrack_activity_state(?int $now = null): array { if ($converting !== '' || mactrack_partition_rebuild_pending() !== []) { return array_merge($state, [ 'state' => 'converting', - 'text' => __('Repartitioning the port and IP tables', 'mactrack'), + 'text' => __('Repartitioning the port, IP and 802.1x tables', 'mactrack'), 'since' => $converting === '' ? null : (int) $converting, ]); } @@ -326,7 +326,7 @@ function mactrack_activity_deferred(int $now): ?array { $labels = [ 'upgrade' => __('A database upgrade is running', 'mactrack'), - 'partition_rebuild' => __('The port and IP table partitioning has not finished', 'mactrack'), + 'partition_rebuild' => __('The port, IP and 802.1x table partitioning has not finished', 'mactrack'), ]; $reason = $labels[$note['reason'] ?? ''] ?? __('The aggregated ports rebuild has not finished', 'mactrack'); diff --git a/lib/mactrack_functions.php b/lib/mactrack_functions.php index 4be6388f..cb81fcf0 100644 --- a/lib/mactrack_functions.php +++ b/lib/mactrack_functions.php @@ -3414,12 +3414,16 @@ function mactrack_db_maint_run(array &$steps, ?int $now = null): void { // daily DELETE frees. // The ARP translations follow the IP retention and the 802.1x sessions - // the port retention; nothing else ever removed them. + // the port retention. db_execute_prepared('DELETE FROM mac_track_arp WHERE scan_date < CURDATE() - INTERVAL ? DAY', [$ip_retention]); - if ($days > 0) { + if (mactrack_partition_state('mac_track_dot1x')['layout']['state'] === 'range') { + $result = mactrack_partition_maintain((int) $days, $now, 'mac_track_dot1x'); + + mactrack_debug('Dropped ' . count($result['dropped']) . ' and created ' . count($result['created']) . ' 802.1x partitions' . ($result['reason'] !== '' ? " ({$result['reason']})" : '') . '.'); + } elseif ($days > 0) { mactrack_delete_batched('DELETE FROM mac_track_dot1x WHERE scan_date < ? ORDER BY scan_date', @@ -6075,7 +6079,7 @@ function mactrack_utilities_post_actions(): array { 'mactrack_refresh_oui_database' => __('Are you sure you want to download and import the IEEE Vendor MAC/OUI database now?', 'mactrack'), 'mactrack_utilities_rebuild_scan_dates' => __('Are you sure you want to rebuild the scan date list from the stored Port to MAC to IP results now?', 'mactrack'), 'mactrack_utilities_optimize_ports' => __('Rebuild and analyze the partition containing the latest port observation, which is the current week with weekly partitions. An unpartitioned table is optimized in full. Scheduled scans are skipped while this runs. Continue?', 'mactrack'), - 'mactrack_utilities_convert_partitions' => __('Run this maintenance off hours. The rebuild copies the full history of the port (MAC address) and IP address tables one after the other and can take hours. Scheduled scans are skipped, database maintenance waits, and MAC and IP address searching shows a maintenance message until it finishes. Allow free disk space for a second copy of each table and its indexes. Continue?', 'mactrack'), + 'mactrack_utilities_convert_partitions' => __('Run this maintenance off hours. The rebuild copies the full history of the port (MAC address), IP address and 802.1x tables one after the other and can take hours. Scheduled scans are skipped, database maintenance waits, and MAC, IP and 802.1x searching shows a maintenance message until it finishes. Allow free disk space for a second copy of each table and its indexes. Continue?', 'mactrack'), ]; } diff --git a/lib/mactrack_partition_ops.php b/lib/mactrack_partition_ops.php index de72df13..42b4e772 100644 --- a/lib/mactrack_partition_ops.php +++ b/lib/mactrack_partition_ops.php @@ -635,13 +635,13 @@ function mactrack_partition_load_scan(array $plan): int|false { * daily partitions, or, for per-scan partitions, daily slots named * aYYYYMMDD that each scan carves its partition from. Nothing is created * while dMaxValue holds rows: the next scan's recovery split moves those - * first. The IP table has no scan to move them, so maintenance splits them - * into a recovery partition itself. Other layouts are left to their own + * first. The IP and 802.1x tables have no scan exchange to move them, so + * maintenance splits them into a recovery partition itself. Other layouts are left to their own * maintenance. * * @param int $days Retention in days; below 1 drops nothing. * @param int|null $now Current time, for tests. - * @param string $table mac_track_ports or mac_track_ips. + * @param string $table History table. * * @return array{dropped: string[], created: string[], reason: string} */ @@ -663,7 +663,8 @@ function mactrack_partition_maintain(int $days, ?int $now = null, string $table } } - mactrack_partition_status([($table === 'mac_track_ports' ? 'maintenance' : 'maintenance_ips') => $result + ['time' => time()]]); + $status_key = ['mac_track_ports' => 'maintenance', 'mac_track_ips' => 'maintenance_ips', 'mac_track_dot1x' => 'maintenance_dot1x'][$table]; + mactrack_partition_status([$status_key => $result + ['time' => time()]]); return $result; } @@ -675,7 +676,7 @@ function mactrack_partition_maintain(int $days, ?int $now = null, string $table * @param array{mariadb: bool, version: string} $server From mactrack_partition_server(). * @param int $days Retention in days. * @param int $now Current time. - * @param string $table mac_track_ports or mac_track_ips. + * @param string $table History table. * * @return array{dropped: string[], created: string[], reason: string} */ @@ -715,7 +716,7 @@ function mactrack_partition_maintain_locked(array $partitions, array $server, in // Per-scan partitions cannot be created before their scan_date is // known, so daily slots are created ahead and each scan carves its // partition off the front of the empty slot that covers it. The IP - // table only has daily and weekly partitions. + // and 802.1x tables only have daily and weekly partitions. $ahead = $size === 'scan' || ($size === 'hour' && !$ports) ? 'day' : $size; if (!$ports) { diff --git a/lib/mactrack_partition_rebuild.php b/lib/mactrack_partition_rebuild.php index eda789ac..54a55d45 100644 --- a/lib/mactrack_partition_rebuild.php +++ b/lib/mactrack_partition_rebuild.php @@ -23,8 +23,8 @@ */ /* - * Shared support for the scan-date partitioning of mac_track_ports and - * mac_track_ips. The rebuild itself lives in cli/upgrade_partitioning.php: + * Shared support for the scan-date partitioning of the port, IP and 802.1x + * history tables. The rebuild itself lives in cli/upgrade_partitioning.php: * it renames each table to
_rebuild, creates the table again with the * chosen layout and copies the history back one scan date at a time. These * helpers tell the poller, maintenance and the views whether such a rebuild @@ -71,7 +71,7 @@ function mactrack_partition_available(array $server): bool { * operator. * * @param int|null $now Current time, for tests. - * @param string $table mac_track_ports or mac_track_ips. + * @param string $table History table. * * @return bool True when the table was partitioned; false leaves it * unpartitioned, which MacTrack fully supports. @@ -154,7 +154,7 @@ function mactrack_partition_history_end(): void { * @return string */ function mactrack_partition_history_message(): string { - return __('The port and IP tables are being repartitioned, or a rebuild did not finish, so their history cannot be changed now. Try again when cli/upgrade_partitioning.php has finished.', 'mactrack'); + return __('The port, IP and 802.1x tables are being repartitioned, or a rebuild did not finish, so their history cannot be changed now. Try again when cli/upgrade_partitioning.php has finished.', 'mactrack'); } /** @@ -208,7 +208,7 @@ function mactrack_partition_current_scheme(array $layout): ?string { /** * Names the table a rebuild moves a table's original rows to. * - * @param string $table mac_track_ports or mac_track_ips. + * @param string $table History table. * * @return string */ @@ -291,9 +291,9 @@ function mactrack_partition_rebuild_notice(): void { } if ($state === 'running') { - $message = __('MacTrack is in maintenance mode for IP address and MAC address searching while its port and IP tables are repartitioned. Results are incomplete until the partitioning has finished.', 'mactrack'); + $message = __('MacTrack is in maintenance mode for IP address, MAC address and 802.1x searching while its history tables are repartitioned. Results are incomplete until the partitioning has finished.', 'mactrack'); } else { - $message = __('MacTrack is in maintenance mode for IP address and MAC address searching because the partitioning of its port and IP tables did not finish. Results are incomplete. Run cli/upgrade_partitioning.php again to resume it.', 'mactrack'); + $message = __('MacTrack is in maintenance mode for IP address, MAC address and 802.1x searching because the partitioning of its history tables did not finish. Results are incomplete. Run cli/upgrade_partitioning.php again to resume it.', 'mactrack'); } html_start_box(__('Maintenance Mode', 'mactrack'), '100%', '', '3', 'center', ''); @@ -335,7 +335,7 @@ function mactrack_partition_rebuild_note_maintenance(?int $now = null): void { * Returns how many days of history a table keeps. The retention settings * hold a number of days; older releases stored names such as 2weeks. * - * @param string $table mac_track_ports or mac_track_ips. + * @param string $table History table. * * @return int Days; 0 keeps everything. */ diff --git a/lib/mactrack_partitions.php b/lib/mactrack_partitions.php index 1fdb82b2..15aede65 100644 --- a/lib/mactrack_partitions.php +++ b/lib/mactrack_partitions.php @@ -309,7 +309,7 @@ function mactrack_partition_layout(array $rows): array { /** * Tables MacTrack partitions by scan date. */ -const MACTRACK_PARTITION_TABLES = ['mac_track_ports', 'mac_track_ips']; +const MACTRACK_PARTITION_TABLES = ['mac_track_ports', 'mac_track_ips', 'mac_track_dot1x']; /** * Throws unless a table is one MacTrack partitions by scan date. Table names diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index 69cef355..380c087f 100644 --- a/locales/po/cacti.pot +++ b/locales/po/cacti.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Cacti\n" "Report-Msgid-Bugs-To: developers@cacti.net\n" -"POT-Creation-Date: 2026-11-17 14:15-0500\n" +"POT-Creation-Date: 2026-11-18 18:36+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: Cacti Developers \n" "Language-Team: Cacti Developers \n" @@ -72,7 +72,7 @@ msgid "About %s rows left to copy (estimate)" msgstr "" #: lib/mactrack_activity.php -msgid "Repartitioning the port and IP tables" +msgid "Repartitioning the port, IP and 802.1x tables" msgstr "" #: lib/mactrack_activity.php @@ -95,7 +95,7 @@ msgid "A database upgrade is running" msgstr "" #: lib/mactrack_activity.php -msgid "The port and IP table partitioning has not finished" +msgid "The port, IP and 802.1x table partitioning has not finished" msgstr "" #: lib/mactrack_activity.php @@ -478,7 +478,7 @@ msgid "Rebuild and analyze the partition containing the latest port observation, msgstr "" #: lib/mactrack_functions.php -msgid "Run this maintenance off hours. The rebuild copies the full history of the port (MAC address) and IP address tables one after the other and can take hours. Scheduled scans are skipped, database maintenance waits, and MAC and IP address searching shows a maintenance message until it finishes. Allow free disk space for a second copy of each table and its indexes. Continue?" +msgid "Run this maintenance off hours. The rebuild copies the full history of the port (MAC address), IP address and 802.1x tables one after the other and can take hours. Scheduled scans are skipped, database maintenance waits, and MAC, IP and 802.1x searching shows a maintenance message until it finishes. Allow free disk space for a second copy of each table and its indexes. Continue?" msgstr "" #: lib/mactrack_functions.php mactrack_utilities.php @@ -518,15 +518,15 @@ msgid "Configure Port and IP Address Partitioning" msgstr "" #: lib/mactrack_partition_rebuild.php -msgid "The port and IP tables are being repartitioned, or a rebuild did not finish, so their history cannot be changed now. Try again when cli/upgrade_partitioning.php has finished." +msgid "The port, IP and 802.1x tables are being repartitioned, or a rebuild did not finish, so their history cannot be changed now. Try again when cli/upgrade_partitioning.php has finished." msgstr "" #: lib/mactrack_partition_rebuild.php -msgid "MacTrack is in maintenance mode for IP address and MAC address searching while its port and IP tables are repartitioned. Results are incomplete until the partitioning has finished." +msgid "MacTrack is in maintenance mode for IP address, MAC address and 802.1x searching while its history tables are repartitioned. Results are incomplete until the partitioning has finished." msgstr "" #: lib/mactrack_partition_rebuild.php -msgid "MacTrack is in maintenance mode for IP address and MAC address searching because the partitioning of its port and IP tables did not finish. Results are incomplete. Run cli/upgrade_partitioning.php again to resume it." +msgid "MacTrack is in maintenance mode for IP address, MAC address and 802.1x searching because the partitioning of its history tables did not finish. Results are incomplete. Run cli/upgrade_partitioning.php again to resume it." msgstr "" #: lib/mactrack_partition_rebuild.php @@ -1761,6 +1761,14 @@ msgstr "" msgid "IP Table Partitions" msgstr "" +#: mactrack_utilities.php +msgid "802.1x Table Layout" +msgstr "" + +#: mactrack_utilities.php +msgid "802.1x Table Partitions" +msgstr "" + #: mactrack_utilities.php msgid "Select daily, weekly or disabled partitioning before starting the rebuild." msgstr "" @@ -1834,7 +1842,7 @@ msgid "Rebuilds and analyzes only the partition containing the latest port obser msgstr "" #: mactrack_utilities.php -msgid "Choose daily or weekly partitions, or disable partitioning, for the port (MAC address) and IP address tables. The rebuild copies the full history in the background; scheduled scans are skipped and MAC and IP address searching shows a maintenance message meanwhile." +msgid "Choose daily or weekly partitions, or disable partitioning, for the port (MAC address), IP address and 802.1x tables. The rebuild copies the full history in the background; scheduled scans are skipped and MAC, IP and 802.1x searching shows a maintenance message meanwhile." msgstr "" #: mactrack_utilities.php @@ -2633,7 +2641,7 @@ msgid "How long should discovered IPs details be retained in the database." msgstr "" #: setup.php -msgid "Size of the partitions of the port and IP tables, or Disabled for unpartitioned tables. Changing it affects new partitions only; to rebuild the existing history to match, use Configure Port and IP Address Partitioning in MacTrack Utilities or run cli/upgrade_partitioning.php." +msgid "Size of the partitions of the port, IP and 802.1x tables, or Disabled for unpartitioned tables. Changing it affects new partitions only; to rebuild the existing history to match, use Configure Port and IP Address Partitioning in MacTrack Utilities or run cli/upgrade_partitioning.php." msgstr "" #: setup.php diff --git a/mactrack_utilities.php b/mactrack_utilities.php index e51f7b14..b9b934f4 100644 --- a/mactrack_utilities.php +++ b/mactrack_utilities.php @@ -815,6 +815,13 @@ function mactrack_utilities_partition_status(): void { $row(__('IP Table Partitions', 'mactrack'), (string) count($ips['partitions'])); } + $dot1x = mactrack_partition_state('mac_track_dot1x')['layout']; + $row(__('802.1x Table Layout', 'mactrack'), trim($layouts[$dot1x['state']] . ' ' . $dot1x['reason'])); + + if ($dot1x['state'] === 'range') { + $row(__('802.1x Table Partitions', 'mactrack'), (string) count($dot1x['partitions'])); + } + html_end_box(); } @@ -1086,7 +1093,7 @@ function mactrack_utilities(): void { diff --git a/mactrack_view_dot1x.php b/mactrack_view_dot1x.php index 60184e34..b2e08054 100644 --- a/mactrack_view_dot1x.php +++ b/mactrack_view_dot1x.php @@ -410,6 +410,7 @@ function mactrack_view_dot1x(): void { global $item_rows; mactrack_tabs(); + mactrack_partition_rebuild_notice(); html_start_box($title, '100%', '', '3', 'center', ''); mactrack_dot1x_filter(); diff --git a/setup.php b/setup.php index 19f0bb68..3880c234 100644 --- a/setup.php +++ b/setup.php @@ -900,7 +900,7 @@ function mactrack_config_settings() { ], 'mt_partition_granularity' => [ 'friendly_name' => __('Port and IP Address Partition Size', 'mactrack'), - 'description' => __('Size of the partitions of the port and IP tables, or Disabled for unpartitioned tables. Changing it affects new partitions only; to rebuild the existing history to match, use Configure Port and IP Address Partitioning in MacTrack Utilities or run cli/upgrade_partitioning.php.', 'mactrack'), + 'description' => __('Size of the partitions of the port, IP and 802.1x tables, or Disabled for unpartitioned tables. Changing it affects new partitions only; to rebuild the existing history to match, use Configure Port and IP Address Partitioning in MacTrack Utilities or run cli/upgrade_partitioning.php.', 'mactrack'), 'method' => 'drop_array', 'default' => 'week', 'array' => [ diff --git a/tests/Unit/ArpDot1xRetentionTest.php b/tests/Unit/ArpDot1xRetentionTest.php index 0a2ba401..ad265282 100644 --- a/tests/Unit/ArpDot1xRetentionTest.php +++ b/tests/Unit/ArpDot1xRetentionTest.php @@ -30,7 +30,7 @@ $src = preg_replace('/\s+/', ' ', file_get_contents(dirname(__DIR__, 2) . '/lib/mactrack_functions.php')); expect($src)->toContain("db_execute_prepared('DELETE FROM mac_track_arp WHERE scan_date < CURDATE() - INTERVAL ? DAY', [\$ip_retention]);"); - expect($src)->toContain("if (\$days > 0) { mactrack_delete_batched('DELETE FROM mac_track_dot1x WHERE scan_date < ? ORDER BY scan_date', [\$retention_date]); }"); + expect($src)->toContain("elseif (\$days > 0) { mactrack_delete_batched('DELETE FROM mac_track_dot1x WHERE scan_date < ? ORDER BY scan_date', [\$retention_date]); }"); }); it('reads the newest ARP translation for a MAC', function () { diff --git a/tests/Unit/PartitionMaintainTest.php b/tests/Unit/PartitionMaintainTest.php index 0ceb68f6..d8076a9a 100644 --- a/tests/Unit/PartitionMaintainTest.php +++ b/tests/Unit/PartitionMaintainTest.php @@ -423,6 +423,53 @@ function mt_pm_status(): array { expect(json_decode($GLOBALS['__test_config_options']['mt_partition_status'], true)['maintenance_ips']['dropped'])->toBe(['d20261101']); }); +it('drops and recovers 802.1x partitions without pruning port scan dates', function () { + mt_pm_db([ + ['d20261101', '1793491201'], + ['d20261112', '1794528000'], + ['dMaxValue', 'MAXVALUE'], + ], ['stranded' => '1794600000']); + + $r = mactrack_partition_maintain(7, MT_PM_NOW, 'mac_track_dot1x'); + + expect($r['dropped'])->toBe(['d20261101']); + expect($r['created'][0])->toBe('r' . gmdate('YmdHis', 1794600000)); + expect(mt_pm_ddl()[0])->toBe(mactrack_partition_drop_sql(['d20261101'], 'mac_track_dot1x')); + expect(mt_pm_prunes())->toBe([]); + expect(json_decode($GLOBALS['__test_config_options']['mt_partition_status'], true)['maintenance_dot1x']['dropped'])->toBe(['d20261101']); +}); + +it('maintains partitioned 802.1x history even when retention is unlimited', function () { + set_config_option('mt_data_retention', '0'); + set_config_option('mt_data_retention_ip', '3'); + mt_pm_db([ + ['d20261112', '1794528000'], + ['dMaxValue', 'MAXVALUE'], + ]); + + perform_mactrack_db_maint(); + + $sql = array_column($GLOBALS['__test_db_calls'], 'sql'); + expect(array_filter($sql, fn ($s) => strpos($s, 'DELETE FROM mac_track_dot1x') !== false))->toBe([]); + expect(array_filter($sql, fn ($s) => strpos($s, 'ALTER TABLE `mac_track_dot1x`') !== false))->not->toBe([]); +}); + +it('uses port retention for unpartitioned 802.1x history', function (int $days) { + set_config_option('mt_data_retention', (string) $days); + set_config_option('mt_data_retention_ip', '3'); + mt_pm_db([]); + + $steps = []; + mactrack_db_maint_run($steps, MT_PM_NOW); + + $deletes = array_values(array_filter($GLOBALS['__test_db_calls'], fn ($c) => strpos($c['sql'], 'DELETE FROM mac_track_dot1x') !== false)); + expect($deletes)->toHaveCount($days > 0 ? 1 : 0); + + if ($days > 0) { + expect($deletes[0]['params'])->toBe([date('Y-m-d H:i:s', MT_PM_NOW - $days * 86400)]); + } +})->with([0, 7]); + it('creates daily IP partitions ahead even when the port size is per scan', function () { mt_pm_db([ ['d20261112', '1794528000'], diff --git a/tests/Unit/PartitionRebuildTest.php b/tests/Unit/PartitionRebuildTest.php index 3bafa9cd..bc6cbae8 100644 --- a/tests/Unit/PartitionRebuildTest.php +++ b/tests/Unit/PartitionRebuildTest.php @@ -22,6 +22,7 @@ */ beforeAll(function () { + require_once dirname(__DIR__) . '/Support/load_function.php'; mactrack_test_load(dirname(__DIR__, 2) . '/lib/mactrack_functions.php'); mactrack_test_load(dirname(__DIR__, 2) . '/includes/database.php'); mactrack_test_load(dirname(__DIR__, 2) . '/setup.php'); @@ -126,6 +127,7 @@ function mt_ur_db(array $opts = []): void { mactrack_test_queue_return_for('db_fetch_cell_prepared', '_rebuild`', $old); mactrack_test_queue_return_for('db_fetch_cell_prepared', 'COUNT(*) FROM `mac_track_ports`', $opts['new'] ?? '2'); mactrack_test_queue_return_for('db_fetch_cell_prepared', 'COUNT(*) FROM `mac_track_ips`', $opts['new'] ?? '2'); + mactrack_test_queue_return_for('db_fetch_cell_prepared', 'COUNT(*) FROM `mac_track_dot1x`', $opts['new'] ?? '2'); mactrack_test_queue_return_for('db_fetch_cell_prepared', 'GET_LOCK(?, 0)', $opts['scan_lock'] ?? '1'); mactrack_test_queue_return_for('db_fetch_cell_prepared', 'GET_LOCK(?, 10)', $opts['partition_lock'] ?? '1'); mactrack_test_queue_return_for('db_fetch_assoc_prepared', 'SELECT DISTINCT scan_date', $opts['scans'] ?? MT_UR_SCANS); @@ -371,14 +373,14 @@ function mt_ur_capture(callable $fn): array { expect(mactrack_upart_assess('mac_track_ports', 'disabled')['action'])->toBe('ok'); }); - it('assesses both tables and says what it found', function () { + it('assesses all history tables and says what it found', function () { mt_ur_db(['layout' => 'none']); - mt_ur_exists([0, 1, 0, 1]); + mt_ur_exists([0, 1, 0, 1, 0, 1]); [$assessed, $out] = mt_ur_capture(fn () => mactrack_upart_assess_all('week')); - expect(array_keys($assessed))->toBe(['mac_track_ports', 'mac_track_ips']); - expect($out)->toContain('WARNING: mac_track_ports: not partitioned', 'WARNING: mac_track_ips: not partitioned'); + expect(array_keys($assessed))->toBe(['mac_track_ports', 'mac_track_ips', 'mac_track_dot1x']); + expect($out)->toContain('WARNING: mac_track_ports: not partitioned', 'WARNING: mac_track_ips: not partitioned', 'WARNING: mac_track_dot1x: not partitioned'); }); }); @@ -434,6 +436,19 @@ function mt_ur_capture(callable $fn): array { expect($GLOBALS['__test_config_options'])->not->toHaveKey('mt_partition_pending'); }); + it('rebuilds 802.1x history without changing port exchange state', function () { + mt_ur_db(); + mt_ur_exists([0, 1, 0]); + + $r = mactrack_upart_rebuild('mac_track_dot1x', 'day', null, 0, MT_UR_NOW); + + expect($r['ok'])->toBeTrue(); + expect(mt_ur_sql()[0])->toBe('RENAME TABLE `mac_track_dot1x` TO `mac_track_dot1x_rebuild`'); + expect(mt_ur_find('PARTITION BY RANGE')[0])->toContain('ALTER TABLE `mac_track_dot1x`', 'PARTITION `d20261112`'); + expect(mt_ur_find('INSERT INTO `mac_track_dot1x`'))->toHaveCount(2); + expect($GLOBALS['__test_config_options'])->not->toHaveKey('mt_partition_pending'); + }); + it('removes the partitioning the original had before partitioning again', function () { mt_ur_db(['src_partitioned' => true]); mt_ur_exists([0, 1, 0]); @@ -650,11 +665,10 @@ function mt_ur_capture(callable $fn): array { describe('run', function () { /** - * Existence answers for a full run over two unpartitioned tables: the - * aggregate upgrade table, both assessments, then both rebuilds. + * Existence answers for a full run over three unpartitioned tables. */ function mt_ur_full_run_exists(): void { - mt_ur_exists([0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0]); + mt_ur_exists([0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0]); } it('refuses to run without partitioning support unless the tables are to be unpartitioned', function () { @@ -758,7 +772,7 @@ function mt_ur_full_run_exists(): void { expect($GLOBALS['__test_config_options']['mt_partition_converting'] ?? '')->toBe(''); }); - it('rebuilds the port table and then the IP table, with scanning paused', function () { + it('rebuilds all history tables in order with scanning paused', function () { mt_ur_db(); mt_ur_full_run_exists(); @@ -767,22 +781,25 @@ function mt_ur_full_run_exists(): void { $sql = mt_ur_sql(); $ports = array_search('RENAME TABLE `mac_track_ports` TO `mac_track_ports_rebuild`', $sql, true); $ips = array_search('RENAME TABLE `mac_track_ips` TO `mac_track_ips_rebuild`', $sql, true); + $dot1x = array_search('RENAME TABLE `mac_track_dot1x` TO `mac_track_dot1x_rebuild`', $sql, true); $drops = array_keys(array_filter($sql, fn ($s) => strpos($s, 'DROP TABLE') === 0)); expect($rc)->toBe(0); expect($ports)->not->toBeFalse(); expect($ips)->toBeGreaterThan($ports); + expect($dot1x)->toBeGreaterThan($ips); expect($sql[$drops[0]])->toBe('DROP TABLE `mac_track_ports_rebuild`'); expect($drops[0])->toBeLessThan($ips); expect($sql[$drops[1]])->toBe('DROP TABLE `mac_track_ips_rebuild`'); - expect($out)->toContain('Scanning is paused', 'maintenance mode', 'Rebuilding mac_track_ports (weekly)', 'Rebuilding mac_track_ips (weekly)', 'Partitioning is complete'); + expect($sql[$drops[2]])->toBe('DROP TABLE `mac_track_dot1x_rebuild`'); + expect($out)->toContain('Scanning is paused', 'maintenance mode', 'Rebuilding mac_track_ports (weekly)', 'Rebuilding mac_track_ips (weekly)', 'Rebuilding mac_track_dot1x (weekly)', 'Partitioning is complete'); expect($GLOBALS['__test_config_options']['mt_partition_granularity'])->toBe('week'); expect($GLOBALS['__test_config_options']['mt_partition_converting'])->toBe(''); $status = json_decode($GLOBALS['__test_config_options']['mt_partition_status'], true)['conversion']; expect($status['ok'])->toBeTrue(); - expect($status['rows'])->toBe(4); + expect($status['rows'])->toBe(6); expect($status['reason'])->toBe(''); $locks = array_column($GLOBALS['__test_db_calls'], 'sql'); @@ -813,7 +830,7 @@ function mt_ur_full_run_exists(): void { it('stops at the first table that fails and leaves it to resume', function () { mt_ur_db(); - mt_ur_exists([0, 0, 1, 0, 1, 0, 0, 1, 0]); + mt_ur_full_run_exists(); mactrack_test_queue_return_for('db_execute_prepared', 'INSERT INTO `mac_track_ports`', false); [$rc, $out] = mt_ur_capture(fn () => mactrack_upart_run('week', false, 0)); @@ -842,7 +859,7 @@ function mt_ur_full_run_exists(): void { it('resumes an unfinished rebuild without another rename', function () { mt_ur_db(['layout' => 'weekly']); - mt_ur_exists([0, 1, 0, 1, 0, 1, 1, 0, 1, 0]); + mt_ur_exists([0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0]); [$rc, $out] = mt_ur_capture(fn () => mactrack_upart_run('week', false, 0)); @@ -865,6 +882,12 @@ function mt_ur_full_run_exists(): void { expect(mactrack_partition_rebuild_pending())->toBe(['mac_track_ports']); }); + it('detects an interrupted 802.1x rebuild', function () { + mt_ur_exists([0, 0, 1]); + + expect(mactrack_partition_rebuild_pending())->toBe(['mac_track_dot1x']); + }); + it('asks the database instead of Cacti\'s remembered answer about tables', function () { mt_ur_exists([0]); @@ -917,12 +940,12 @@ function mt_ur_full_run_exists(): void { mt_ur_exists([0, 1]); [, $interrupted] = mt_ur_capture('mactrack_partition_rebuild_notice'); - expect($interrupted)->toContain('maintenance mode for IP address and MAC address searching', 'did not finish', 'cli/upgrade_partitioning.php'); + expect($interrupted)->toContain('maintenance mode for IP address, MAC address and 802.1x searching', 'did not finish', 'cli/upgrade_partitioning.php'); mactrack_test_queue_return_for('db_fetch_row_prepared', 'FROM processes', ['pid' => (string) getmypid(), 'started' => '99000', 'now' => '100000', 'timeout' => '86400']); [, $running] = mt_ur_capture('mactrack_partition_rebuild_notice'); - expect($running)->toContain('maintenance mode for IP address and MAC address searching', 'until the partitioning has finished'); + expect($running)->toContain('maintenance mode for IP address, MAC address and 802.1x searching', 'until the partitioning has finished'); }); it('shows the notice on the IP and MAC address views', function (string $file, string $function) { @@ -935,6 +958,7 @@ function mt_ur_full_run_exists(): void { ['mactrack_view_arp.php', 'mactrack_view_ips'], ['mactrack_view_macs.php', 'mactrack_view_macs'], ['mactrack_view_macs.php', 'mactrack_view_aggregated_macs'], + ['mactrack_view_dot1x.php', 'mactrack_view_dot1x'], ]); it('reads the retention of each table in days', function (string $setting, string $table, string $value, int $days) { @@ -948,6 +972,8 @@ function mt_ur_full_run_exists(): void { ['mt_data_retention', 'mac_track_ports', '2weeks', 14], ['mt_data_retention', 'mac_track_ports', 'forever', 2], ['mt_data_retention_ip', 'mac_track_ips', '30', 30], + ['mt_data_retention', 'mac_track_dot1x', '14', 14], + ['mt_data_retention', 'mac_track_dot1x', '0', 0], ]); it('pauses maintenance and the poller while a rebuild is unfinished', function () { @@ -1086,7 +1112,7 @@ function mt_ur_full_run_exists(): void { expect(mactrack_partition_fresh_install(MT_UR_NOW, $table))->toBeTrue(); expect(mt_ur_sql()[0])->toContain('ALTER TABLE `' . $table . '` PARTITION BY RANGE (UNIX_TIMESTAMP(scan_date))', 'PARTITION `w20261109` VALUES LESS THAN (1794787200)', 'PARTITION `w20261116` VALUES LESS THAN (1795392000)'); expect($GLOBALS['__test_config_options']['mt_partition_granularity'])->toBe('week'); - })->with(['mac_track_ports', 'mac_track_ips']); + })->with(['mac_track_ports', 'mac_track_ips', 'mac_track_dot1x']); it('only partitions the tables MacTrack partitions', function () { expect(fn () => mactrack_partition_fresh_install(MT_UR_NOW, 'mac_track_devices'))->toThrow(InvalidArgumentException::class); @@ -1152,10 +1178,10 @@ function mt_ur_full_run_exists(): void { expect($alter)->toContain('ALTER TABLE `' . $created . '` PARTITION BY RANGE (UNIX_TIMESTAMP(scan_date)) (PARTITION `w'); - foreach (array_diff(['mac_track_ports', 'mac_track_ips'], [$created]) as $existing) { + foreach (array_diff(['mac_track_ports', 'mac_track_ips', 'mac_track_dot1x'], [$created]) as $existing) { expect($alter)->not->toContain('`' . $existing . '`'); } - })->with(['mac_track_ports', 'mac_track_ips']); + })->with(['mac_track_ports', 'mac_track_ips', 'mac_track_dot1x']); it('partitions only the tables setup has just created', function () { $src = file_get_contents(dirname(__DIR__, 2) . '/includes/database.php'); From 86e23870b04e94b42815a1449cdd08bee3967229 Mon Sep 17 00:00:00 2001 From: TheWitness <1439914+TheWitness@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:41:25 +0000 Subject: [PATCH 2/3] Share fresh history partition setup across tables Remove the repeated install-time partition initialization and preserve the rule that existing history tables are never repartitioned by setup. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- includes/database.php | 46 +++++++++++------------------ tests/Unit/PartitionRebuildTest.php | 9 +++--- 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/includes/database.php b/includes/database.php index 622e6b89..1236eb1c 100644 --- a/includes/database.php +++ b/includes/database.php @@ -1448,6 +1448,19 @@ function mactrack_retry_default_site(): bool { * false when seeding failed. */ function mactrack_setup_database(bool $notify_seed_failure = false): bool { + // Setup also runs without lib/mactrack_functions.php. + include_once(__DIR__ . '/../lib/mactrack_partitions.php'); + include_once(__DIR__ . '/../lib/mactrack_partition_ops.php'); + include_once(__DIR__ . '/../lib/mactrack_partition_rebuild.php'); + + $new_history_tables = []; + + foreach (MACTRACK_PARTITION_TABLES as $table) { + if (!db_table_exists($table)) { + $new_history_tables[] = $table; + } + } + $data = []; $data['columns'][] = ['name' => 'row_id', 'unsigned' => true, 'type' => 'bigint(20)', 'NULL' => false, 'auto_increment' => true]; $data['columns'][] = ['name' => 'site_id', 'unsigned' => true, 'type' => 'int(10)', 'NULL' => false, 'default' => '0']; @@ -1600,17 +1613,8 @@ function mactrack_setup_database(bool $notify_seed_failure = false): bool { $data['keys'][] = ['name' => 'scan_date', 'columns' => 'scan_date']; $data['type'] = 'InnoDB'; $data['comment'] = ''; - $dot1x_existed = db_table_exists('mac_track_dot1x'); api_plugin_db_table_create('mactrack', 'mac_track_dot1x', $data); - if (!$dot1x_existed) { - include_once(__DIR__ . '/../lib/mactrack_partitions.php'); - include_once(__DIR__ . '/../lib/mactrack_partition_ops.php'); - include_once(__DIR__ . '/../lib/mactrack_partition_rebuild.php'); - - mactrack_partition_fresh_install(null, 'mac_track_dot1x'); - } - $data = []; $data['columns'][] = ['name' => 'device_id', 'unsigned' => true, 'type' => 'int(10)', 'NULL' => false, 'default' => '0']; $data['columns'][] = ['name' => 'ifIndex', 'unsigned' => true, 'type' => 'int(10)', 'NULL' => false]; @@ -1739,18 +1743,8 @@ function mactrack_setup_database(bool $notify_seed_failure = false): bool { $data['keys'][] = ['name' => 'scan_date', 'columns' => 'scan_date']; $data['type'] = 'InnoDB'; $data['comment'] = ''; - $ips_existed = db_table_exists('mac_track_ips'); api_plugin_db_table_create('mactrack', 'mac_track_ips', $data); - if (!$ips_existed) { - // Setup loads this file without lib/mactrack_functions.php. - include_once(__DIR__ . '/../lib/mactrack_partitions.php'); - include_once(__DIR__ . '/../lib/mactrack_partition_ops.php'); - include_once(__DIR__ . '/../lib/mactrack_partition_rebuild.php'); - - mactrack_partition_fresh_install(null, 'mac_track_ips'); - } - $data = []; $data['columns'][] = ['name' => 'mac_address', 'type' => 'varchar(20)', 'NULL' => false, 'default' => '']; $data['columns'][] = ['name' => 'mac_id', 'unsigned' => true, 'type' => 'int(10)', 'NULL' => false, 'auto_increment' => true]; @@ -1823,18 +1817,8 @@ function mactrack_setup_database(bool $notify_seed_failure = false): bool { $data['keys'][] = ['name' => 'authorized', 'columns' => 'authorized']; $data['type'] = 'InnoDB'; $data['comment'] = 'Database for Tracking Device MACs'; - $ports_existed = db_table_exists('mac_track_ports'); api_plugin_db_table_create('mactrack', 'mac_track_ports', $data); - if (!$ports_existed) { - // Setup loads this file without lib/mactrack_functions.php. - include_once(__DIR__ . '/../lib/mactrack_partitions.php'); - include_once(__DIR__ . '/../lib/mactrack_partition_ops.php'); - include_once(__DIR__ . '/../lib/mactrack_partition_rebuild.php'); - - mactrack_partition_fresh_install(); - } - $data = []; $data['columns'][] = ['name' => 'device_id', 'type' => 'int(11)', 'NULL' => false, 'default' => '0']; $data['columns'][] = ['name' => 'process_id', 'unsigned' => true, 'type' => 'int(10)', 'NULL' => true]; @@ -2140,6 +2124,10 @@ function mactrack_setup_database(bool $notify_seed_failure = false): bool { VALUES ('92xx Switch-default','Cisco','1','*CAT9K_LITE_IOSXE*','','get_IOS_dot1dTpFdbEntry_ports','get_standard_arp_table','0','',0,0,'on')"); } + foreach ($new_history_tables as $table) { + mactrack_partition_fresh_install(null, $table); + } + // Seed only after the complete schema and built-in device types exist. return mactrack_ensure_default_site($notify_seed_failure, $notify_seed_failure); } diff --git a/tests/Unit/PartitionRebuildTest.php b/tests/Unit/PartitionRebuildTest.php index bc6cbae8..f9f50333 100644 --- a/tests/Unit/PartitionRebuildTest.php +++ b/tests/Unit/PartitionRebuildTest.php @@ -1166,7 +1166,7 @@ function mt_ur_full_run_exists(): void { expect($GLOBALS['__test_config_options']['mt_partition_granularity'])->toBe('week'); }); - it('partitions the port and IP tables when setup creates them', function (string $created) { + it('partitions each history table only when setup creates it', function (string $created) { mactrack_test_load(dirname(__DIR__, 2) . '/includes/database.php'); mactrack_test_queue_return_for('db_table_exists', $created, false); mactrack_test_queue_return_for('db_fetch_cell_prepared', 'VERSION()', '10.5.29-MariaDB'); @@ -1183,11 +1183,10 @@ function mt_ur_full_run_exists(): void { } })->with(['mac_track_ports', 'mac_track_ips', 'mac_track_dot1x']); - it('partitions only the tables setup has just created', function () { - $src = file_get_contents(dirname(__DIR__, 2) . '/includes/database.php'); + it('leaves existing history tables unpartitioned during setup', function () { + mactrack_setup_database(); - expect($src)->toContain("\$ports_existed = db_table_exists('mac_track_ports');\n\tapi_plugin_db_table_create('mactrack', 'mac_track_ports', \$data);\n\n\tif (!\$ports_existed) {"); - expect($src)->toContain("\$ips_existed = db_table_exists('mac_track_ips');\n\tapi_plugin_db_table_create('mactrack', 'mac_track_ips', \$data);\n\n\tif (!\$ips_existed) {"); + expect(mt_ur_find('PARTITION BY RANGE'))->toBe([]); }); }); From da73cf7fb02b6029844fadc2761e498a1d17e44f Mon Sep 17 00:00:00 2001 From: TheWitness <1439914+TheWitness@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:43:37 +0000 Subject: [PATCH 3/3] Parameterize shared IP and 802.1x retention tests Cover both history tables with finite and unlimited port retention without duplicating maintenance scenarios. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- tests/Unit/PartitionMaintainTest.php | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/tests/Unit/PartitionMaintainTest.php b/tests/Unit/PartitionMaintainTest.php index d8076a9a..4742956c 100644 --- a/tests/Unit/PartitionMaintainTest.php +++ b/tests/Unit/PartitionMaintainTest.php @@ -439,21 +439,6 @@ function mt_pm_status(): array { expect(json_decode($GLOBALS['__test_config_options']['mt_partition_status'], true)['maintenance_dot1x']['dropped'])->toBe(['d20261101']); }); -it('maintains partitioned 802.1x history even when retention is unlimited', function () { - set_config_option('mt_data_retention', '0'); - set_config_option('mt_data_retention_ip', '3'); - mt_pm_db([ - ['d20261112', '1794528000'], - ['dMaxValue', 'MAXVALUE'], - ]); - - perform_mactrack_db_maint(); - - $sql = array_column($GLOBALS['__test_db_calls'], 'sql'); - expect(array_filter($sql, fn ($s) => strpos($s, 'DELETE FROM mac_track_dot1x') !== false))->toBe([]); - expect(array_filter($sql, fn ($s) => strpos($s, 'ALTER TABLE `mac_track_dot1x`') !== false))->not->toBe([]); -}); - it('uses port retention for unpartitioned 802.1x history', function (int $days) { set_config_option('mt_data_retention', (string) $days); set_config_option('mt_data_retention_ip', '3'); @@ -528,8 +513,8 @@ function mt_pm_status(): array { expect(fn () => mactrack_partition_maintain(7, MT_PM_NOW, 'mac_track_devices'))->toThrow(InvalidArgumentException::class); }); -it('maintains the IP table by partition when it uses the scan-date layout', function () { - set_config_option('mt_data_retention', '7'); +it('maintains scan-date history by partition including unlimited port retention', function (string $table, int $days) { + set_config_option('mt_data_retention', (string) $days); set_config_option('mt_data_retention_ip', '3'); mt_pm_db([ ['d20261101', '1793491201'], @@ -540,9 +525,9 @@ function mt_pm_status(): array { perform_mactrack_db_maint(); $sql = array_column($GLOBALS['__test_db_calls'], 'sql'); - expect(array_filter($sql, fn ($s) => strpos($s, 'DELETE FROM mac_track_ips') !== false))->toBe([]); - expect(array_filter($sql, fn ($s) => strpos($s, 'ALTER TABLE `mac_track_ips`') !== false))->not->toBe([]); -}); + expect(array_filter($sql, fn ($s) => strpos($s, 'DELETE FROM ' . $table) !== false))->toBe([]); + expect(array_filter($sql, fn ($s) => strpos($s, 'ALTER TABLE `' . $table . '`') !== false))->not->toBe([]); +})->with(['mac_track_ips', 'mac_track_dot1x'])->with([0, 7]); it('forgets a maintenance window missed during a rebuild once the maintenance runs', function () { set_config_option('mt_data_retention', '7');
- +