Database transactions maintenance (#7047)

* add database_maintenance function to database_transactions

* update database_maintenance function in database_transactions
New changes in the maintenance application required changes to any
existing functions already created

---------

Co-authored-by: Tim Fry <tim@fusionpbx.com>
This commit is contained in:
frytimo 2024-07-09 15:52:05 -03:00 committed by GitHub
parent d1594da57e
commit 8cfdb40acc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 83 additions and 1 deletions

View File

@ -151,5 +151,12 @@
$apps[$x]['db'][$y]['fields'][$z]['type']['sqlite'] = "text";
$apps[$x]['db'][$y]['fields'][$z]['type']['mysql'] = "char(36)";
$apps[$x]['db'][$y]['fields'][$z]['description']['en-us'] = "";
$y = 0;
$apps[$x]['default_settings'][$y]['default_setting_uuid'] = "5cb285ea-c94b-41a8-8f3d-80680278d1ae";
$apps[$x]['default_settings'][$y]['default_setting_category'] = "database_transactions";
$apps[$x]['default_settings'][$y]['default_setting_subcategory'] = "database_retention_days";
$apps[$x]['default_settings'][$y]['default_setting_name'] = "numeric";
$apps[$x]['default_settings'][$y]['default_setting_value'] = "30";
$apps[$x]['default_settings'][$y]['default_setting_enabled'] = "true";
$apps[$x]['default_settings'][$y]['default_setting_description'] = "Number of days to retain the transaction logs by the maintenance service in the database.";
?>

View File

@ -0,0 +1,75 @@
<?php
/*
* FusionPBX
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FusionPBX
*
* The Initial Developer of the Original Code is
* Mark J Crane <markjcrane@fusionpbx.com>
* Portions created by the Initial Developer are Copyright (C) 2008-2024
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Mark J Crane <markjcrane@fusionpbx.com>
* Tim Fry <tim@fusionpbx.com>
*/
/**
* Description of database_transactions
*
* @author Tim Fry <tim@fusionpbx.com>
*/
class database_transactions {
/**
* Removes old entries for in the database database_transactions table
* see {@link https://github.com/fusionpbx/fusionpbx-app-maintenance/} FusionPBX Maintenance App
* @param settings $settings Settings object
* @return void
*/
public static function database_maintenance(settings $settings): void {
//set table name for query
$table = 'database_transactions';
//get a database connection
$database = $settings->database();
//get a list of domains
$domains = maintenance::get_domains($database);
foreach ($domains as $domain_uuid => $domain_name) {
//get domain settings
$domain_settings = new settings(['database' => $database, 'domain_uuid' => $domain_uuid]);
//get the retention days for this domain
$retention_days = $domain_settings->get('database_transactions', 'database_retention_days', '');
//ensure we have a retention day
if (!empty($retention_days) && is_numeric($retention_days)) {
//clear out old records
$sql = "delete from v_{$table} WHERE transaction_date < NOW() - INTERVAL '{$retention_days} days'"
. " and domain_uuid = '{$domain_uuid}'";
$database->execute($sql);
if ($database->message['code'] === 200) {
//success
maintenance_service::log_write(self::class, "Successfully removed database_transaction entries from $domain_name", $domain_uuid);
} else {
//failure
maintenance_service::log_write(self::class, "Unable to remove records for domain $domain_name", $domain_uuid, maintenance_service::LOG_ERROR);
}
} else {
//report an invalid setting
maintenance_service::log_write(self::class, "Retention days not set or not a numeric value", $domain_uuid, maintenance_service::LOG_ERROR);
}
}
//ensure logs are saved
maintenance_service::log_flush();
}
}