Merge branch 'ent-8324-desincronizacion-entornos-metaconsola-transacciones' into 'develop'

force db_process_sql

See merge request artica/pandorafms!4570
This commit is contained in:
Daniel Rodriguez 2021-12-09 17:45:43 +00:00
commit 76bcbd3c33
5 changed files with 79 additions and 119 deletions

View File

@ -8,7 +8,7 @@ ALTER TABLE `tlocal_component` ADD COLUMN `percentage_critical` tinyint(1) UNSIG
ALTER TABLE `tlocal_component` ADD COLUMN `percentage_warning` tinyint(1) UNSIGNED DEFAULT 0; ALTER TABLE `tlocal_component` ADD COLUMN `percentage_warning` tinyint(1) UNSIGNED DEFAULT 0;
ALTER TABLE `tpolicy_modules` ADD COLUMN `percentage_warning` tinyint(1) UNSIGNED DEFAULT 0; ALTER TABLE `tpolicy_modules` ADD COLUMN `percentage_warning` tinyint(1) UNSIGNED DEFAULT 0;
ALTER TABLE `tpolicy_modules` ADD COLUMN `percentage_critical` tinyint(1) UNSIGNED DEFAULT 0; ALTER TABLE `tpolicy_modules` ADD COLUMN `percentage_critical` tinyint(1) UNSIGNED DEFAULT 0;
ALTER TABLE `tsync_queue` ADD COLUMN `result` TEXT;
ALTER TABLE tagente_modulo MODIFY debug_content TEXT; ALTER TABLE tagente_modulo MODIFY debug_content TEXT;
CREATE TABLE IF NOT EXISTS `talert_calendar` ( CREATE TABLE IF NOT EXISTS `talert_calendar` (

View File

@ -4089,6 +4089,7 @@ CREATE TABLE IF NOT EXISTS `tsync_queue` (
`operation` text, `operation` text,
`table` text, `table` text,
`error` MEDIUMTEXT, `error` MEDIUMTEXT,
`result` TEXT,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

View File

@ -1244,57 +1244,6 @@ function mysql_db_get_all_row_by_steps_sql($new=true, &$result, $sql=null)
} }
/**
* Starts a database transaction.
*/
function mysql_db_process_sql_begin()
{
global $config;
if ($config['mysqli']) {
mysqli_query($config['dbconnection'], 'SET AUTOCOMMIT = 0');
mysqli_query($config['dbconnection'], 'START TRANSACTION');
} else {
mysql_query('SET AUTOCOMMIT = 0');
mysql_query('START TRANSACTION');
}
}
/**
* Commits a database transaction.
*/
function mysql_db_process_sql_commit()
{
global $config;
if ($config['mysqli']) {
mysqli_query($config['dbconnection'], 'COMMIT');
mysqli_query($config['dbconnection'], 'SET AUTOCOMMIT = 1');
} else {
mysql_query('COMMIT');
mysql_query('SET AUTOCOMMIT = 1');
}
}
/**
* Rollbacks a database transaction.
*/
function mysql_db_process_sql_rollback()
{
global $config;
if ($config['mysqli']) {
mysqli_query($config['dbconnection'], 'ROLLBACK ');
mysqli_query($config['dbconnection'], 'SET AUTOCOMMIT = 1');
} else {
mysql_query('ROLLBACK ');
mysql_query('SET AUTOCOMMIT = 1');
}
}
/** /**
* Get last error. * Get last error.
* *
@ -1390,7 +1339,7 @@ function mysql_db_process_file($path, $handle_error=true)
$query = ''; $query = '';
// Begin the transaction // Begin the transaction
mysql_db_process_sql_begin(); db_process_sql_begin();
foreach ($file_content as $sql_line) { foreach ($file_content as $sql_line) {
if (trim($sql_line) != '' && strpos($sql_line, '--') === false) { if (trim($sql_line) != '' && strpos($sql_line, '--') === false) {
@ -1405,7 +1354,7 @@ function mysql_db_process_file($path, $handle_error=true)
if (!$result = $query_result) { if (!$result = $query_result) {
// Error. Rollback the transaction // Error. Rollback the transaction
mysql_db_process_sql_rollback(); db_process_sql_rollback();
if ($config['mysqli']) { if ($config['mysqli']) {
$error_message = mysqli_error($config['dbconnection']); $error_message = mysqli_error($config['dbconnection']);
@ -1442,7 +1391,7 @@ function mysql_db_process_file($path, $handle_error=true)
} }
// No errors. Commit the transaction // No errors. Commit the transaction
mysql_db_process_sql_commit(); db_process_sql_commit();
return true; return true;
} else { } else {
return false; return false;
@ -1481,7 +1430,7 @@ function db_run_sql_file($location)
$mysqli->query($config['dbconnection'], 'START TRANSACTION'); $mysqli->query($config['dbconnection'], 'START TRANSACTION');
} else { } else {
// Run commands // Run commands
mysql_db_process_sql_begin(); db_process_sql_begin();
// Begin transaction // Begin transaction
} }
@ -1507,7 +1456,7 @@ function db_run_sql_file($location)
$mysqli->query($config['dbconnection'], 'COMMIT'); $mysqli->query($config['dbconnection'], 'COMMIT');
$mysqli->query($config['dbconnection'], 'SET AUTOCOMMIT = 1'); $mysqli->query($config['dbconnection'], 'SET AUTOCOMMIT = 1');
} else { } else {
mysql_db_process_sql_commit(); db_process_sql_commit();
// Save results // Save results
} }
@ -1517,7 +1466,7 @@ function db_run_sql_file($location)
$mysqli->query($config['dbconnection'], 'ROLLBACK '); $mysqli->query($config['dbconnection'], 'ROLLBACK ');
$mysqli->query($config['dbconnection'], 'SET AUTOCOMMIT = 1'); $mysqli->query($config['dbconnection'], 'SET AUTOCOMMIT = 1');
} else { } else {
mysql_db_process_sql_rollback(); db_process_sql_rollback();
// Undo results // Undo results
} }

View File

@ -1377,7 +1377,24 @@ function db_process_sql($sql, $rettype='affected_rows', $dbconnection='', $cache
break; break;
} }
if ($rc !== false) { db_sync($dbconnection, $sql, $rc);
return $rc;
}
/**
* Propagate to nodes.
*
* @param mixed $dbconnection Dbconnection.
* @param mixed $sql Sql.
* @param mixed $rc Rc.
*
* @return void
*/
function db_sync($dbconnection, $sql, $rc)
{
global $config;
if (enterprise_hook('is_metaconsole') === true if (enterprise_hook('is_metaconsole') === true
&& isset($config['centralized_management']) === true && isset($config['centralized_management']) === true
&& (bool) $config['centralized_management'] === true && (bool) $config['centralized_management'] === true
@ -1388,7 +1405,7 @@ function db_process_sql($sql, $rettype='affected_rows', $dbconnection='', $cache
// Synchronize changes to nodes if needed. // Synchronize changes to nodes if needed.
$sync = new Synchronizer(); $sync = new Synchronizer();
if ($sync !== null) { if ($sync !== null) {
if ($sync->queue($sql) === false) { if ($sync->queue($sql, $rc) === false) {
// Launch events per failed query. // Launch events per failed query.
$errors = $sync->getLatestErrors(); $errors = $sync->getLatestErrors();
if ($errors !== null) { if ($errors !== null) {
@ -1407,9 +1424,6 @@ function db_process_sql($sql, $rettype='affected_rows', $dbconnection='', $cache
error_log($errors); error_log($errors);
} }
} }
}
return $rc;
} }
@ -1808,19 +1822,19 @@ function db_process_sql_delete($table, $where, $where_join='AND')
function db_process_sql_begin() function db_process_sql_begin()
{ {
global $config; global $config;
$null = null;
switch ($config['dbtype']) { switch ($config['dbtype']) {
case 'mysql':
return mysql_db_process_sql_begin();
break;
case 'postgresql': case 'postgresql':
return postgresql_db_process_sql_begin(); return postgresql_db_process_sql_begin();
break;
case 'oracle': case 'oracle':
return oracle_db_process_sql_begin(); return oracle_db_process_sql_begin();
default:
case 'mysql':
db_process_sql('SET AUTOCOMMIT = 0', 'affected_rows', '', false, $null, false);
db_process_sql('START TRANSACTION', 'affected_rows', '', false, $null, false);
break; break;
} }
} }
@ -1832,19 +1846,19 @@ function db_process_sql_begin()
function db_process_sql_commit() function db_process_sql_commit()
{ {
global $config; global $config;
$null = null;
switch ($config['dbtype']) { switch ($config['dbtype']) {
case 'mysql':
return mysql_db_process_sql_commit();
break;
case 'postgresql': case 'postgresql':
return postgresql_db_process_sql_commit(); return postgresql_db_process_sql_commit();
break;
case 'oracle': case 'oracle':
return oracle_db_process_sql_commit(); return oracle_db_process_sql_commit();
default:
case 'mysql':
db_process_sql('COMMIT', 'affected_rows', '', false, $null, false);
db_process_sql('SET AUTOCOMMIT = 1', 'affected_rows', '', false, $null, false);
break; break;
} }
} }
@ -1856,19 +1870,19 @@ function db_process_sql_commit()
function db_process_sql_rollback() function db_process_sql_rollback()
{ {
global $config; global $config;
$null = null;
switch ($config['dbtype']) { switch ($config['dbtype']) {
case 'mysql':
return mysql_db_process_sql_rollback();
break;
case 'postgresql': case 'postgresql':
return postgresql_db_process_sql_rollback(); return postgresql_db_process_sql_rollback();
break;
case 'oracle': case 'oracle':
return oracle_db_process_sql_rollback(); return oracle_db_process_sql_rollback();
default:
case 'mysql':
db_process_sql('ROLLBACK', 'affected_rows', '', false, $null, false);
db_process_sql('SET AUTOCOMMIT = 1', 'affected_rows', '', false, $null, false);
break; break;
} }
} }
@ -1889,6 +1903,7 @@ function db_print_database_debug()
echo '<div class="database_debug_title">'.__('Database debug').'</div>'; echo '<div class="database_debug_title">'.__('Database debug').'</div>';
$table = new stdClass();
$table->id = 'database_debug'; $table->id = 'database_debug';
$table->cellpadding = '0'; $table->cellpadding = '0';
$table->width = '95%'; $table->width = '95%';
@ -1946,18 +1961,15 @@ function db_get_last_error()
global $config; global $config;
switch ($config['dbtype']) { switch ($config['dbtype']) {
case 'mysql':
return mysql_db_get_last_error();
break;
case 'postgresql': case 'postgresql':
return postgresql_db_get_last_error(); return postgresql_db_get_last_error();
break;
case 'oracle': case 'oracle':
return oracle_db_get_last_error(); return oracle_db_get_last_error();
break; case 'mysql':
default:
return mysql_db_get_last_error();
} }
} }
@ -1975,18 +1987,15 @@ function db_get_type_field_table($table, $field)
global $config; global $config;
switch ($config['dbtype']) { switch ($config['dbtype']) {
case 'mysql':
return mysql_db_get_type_field_table($table, $field);
break;
case 'postgresql': case 'postgresql':
return postgresql_db_get_type_field_table($table, $field); return postgresql_db_get_type_field_table($table, $field);
break;
case 'oracle': case 'oracle':
return oracle_db_get_type_field_table($table, $field); return oracle_db_get_type_field_table($table, $field);
break; case 'mysql':
default:
return mysql_db_get_type_field_table($table, $field);
} }
} }

View File

@ -4069,6 +4069,7 @@ CREATE TABLE IF NOT EXISTS `tsync_queue` (
`operation` text, `operation` text,
`table` text, `table` text,
`error` MEDIUMTEXT, `error` MEDIUMTEXT,
`result` TEXT,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;