database compare now successfull at updating xentral oss 20.1 to OpenXE 1.6

This commit is contained in:
OpenXE 2022-12-01 20:57:13 +00:00
parent 5d1319b419
commit c443496462
3 changed files with 13077 additions and 11844 deletions

View File

@ -0,0 +1,6 @@
{
"host": "localhost",
"database": "openxe",
"user": "openxe",
"passwd": "openxe"
}

View File

@ -206,13 +206,13 @@ if ($argc > 1) {
echo("--------------- Comparing database '$schema@$host' vs. JSON '".$compare_def['database']."@".$compare_def['host']."' ---------------\n"); echo("--------------- Comparing database '$schema@$host' vs. JSON '".$compare_def['database']."@".$compare_def['host']."' ---------------\n");
$compare_differences = compare_table_array($compare_def,"in JSON",$db_def,"in DB",true); $compare_differences = compare_table_array($compare_def,"in JSON",$db_def,"in DB",true);
echo("Comparison found ".(empty($compare_differences)?0:count($compare_differences))." differences.\n"); echo((empty($compare_differences)?0:count($compare_differences))." differences.\n");
if ($verbose) { if ($verbose) {
foreach ($compare_differences as $compare_difference) { foreach ($compare_differences as $compare_difference) {
$comma = ""; $comma = "";
foreach ($compare_difference as $key => $value) { foreach ($compare_difference as $key => $value) {
echo($comma."$key => '$value'"); echo($comma."$key => [$value]");
$comma = ", "; $comma = ", ";
} }
echo("\n"); echo("\n");
@ -265,7 +265,14 @@ if ($argc > 1) {
if ($key['Key_name'] == 'PRIMARY') { if ($key['Key_name'] == 'PRIMARY') {
$keystring = "PRIMARY KEY "; $keystring = "PRIMARY KEY ";
} else { } else {
$keystring = $key['Index_type']." KEY `".$key['Key_name']."` ";
if(array_key_exists('Index_type', $key)) {
$index_type = $key['Index_type'];
} else {
$index_type = "";
}
$keystring = $index_type." KEY `".$key['Key_name']."` ";
} }
$sql .= $comma.$keystring."(`".implode("`,`",$key['columns'])."`) "; $sql .= $comma.$keystring."(`".implode("`,`",$key['columns'])."`) ";
} }
@ -292,7 +299,7 @@ if ($argc > 1) {
if ($column_key !== false) { if ($column_key !== false) {
$column = $table['columns'][$column_key]; $column = $table['columns'][$column_key];
$sql = "ALTER TABLE `$table_name` ADD COLUMN `".$column_name."` "; $sql = "ALTER TABLE `$table_name` ADD COLUMN `".$column_name."` ";
$sql .= column_sql_definition($table_name, $column); $sql .= column_sql_definition($table_name, $column, array_column($replacers,1));
$sql .= ";"; $sql .= ";";
$upgrade_sql[] = $sql; $upgrade_sql[] = $sql;
} }
@ -334,11 +341,36 @@ if ($argc > 1) {
} }
// Modify Column in DB // Modify Column in DB
break; break;
case 'Table count': case "Key definition":
// Nothing to do $table_name = $compare_difference['table'];
break; $key_name = $compare_difference['key'];
case 'Table type': $table_key = array_search($table_name,array_column($compare_def['tables'],'name'));
echo("Upgrade type '".$compare_difference['type']."' on table '".$compare_difference['table']."' not supported.\n"); if ($table_key !== false) {
$table = $compare_def['tables'][$table_key];
$keys = $table['keys'];
$key_names = array_column($keys,'Key_name');
$key_key = array_search($key_name,$key_names);
if ($key_key !== false) {
$key = $table['keys'][$key_key];
$sql = "ALTER TABLE `$table_name` DROP KEY `".$key_name."`;";
$upgrade_sql[] = $sql;
$sql = "ALTER TABLE `$table_name` ADD KEY `".$key_name."` ";
$sql .= "(`".implode("`,`",$key['columns'])."`)";
$sql .= ";";
$upgrade_sql[] = $sql;
}
else {
echo("Error key_key while changing key '$key_name' in table '".$table['name']."'\n");
exit;
}
}
else {
echo("Error table_key while changing key '$key_name' in table '$table_name'.\n");
}
break; break;
case 'Key existance': case 'Key existance':
@ -361,16 +393,19 @@ if ($argc > 1) {
$upgrade_sql[] = $sql; $upgrade_sql[] = $sql;
} }
else { else {
echo("Error key_key while modifying key '$key_name' in table '".$table['name']."'\n"); echo("Error key_key while adding key '$key_name' in table '".$table['name']."'\n");
exit; exit;
} }
} }
else { else {
echo("Error table_key while adding key '$key_name' in table '$table_name'.\n"); echo("Error table_key while adding key '$key_name' in table '$table_name'.\n");
} }
// Modify Column in DB break;
case 'Table count':
// Nothing to do
break;
case 'Table type':
echo("Upgrade type '".$compare_difference['type']."' on table '".$compare_difference['table']."' not supported.\n");
break; break;
default: default:
echo("Upgrade type '".$compare_difference['type']."' not supported.\n"); echo("Upgrade type '".$compare_difference['type']."' not supported.\n");
@ -409,7 +444,7 @@ if ($argc > 1) {
foreach ($upgrade_sql as $sql) { foreach ($upgrade_sql as $sql) {
$counter++; $counter++;
echo("Upgrade step $counter of $number_of_statements... "); echo("\rUpgrade step $counter of $number_of_statements... ");
if ($verbose) { if ($verbose) {
echo("(".substr($sql,0,100)."...) "); echo("(".substr($sql,0,100)."...) ");
@ -422,14 +457,22 @@ if ($argc > 1) {
file_put_contents("./errors.txt",$error.$sql."\n",FILE_APPEND); file_put_contents("./errors.txt",$error.$sql."\n",FILE_APPEND);
$error_counter++; $error_counter++;
} else { } else {
echo("ok.\n"); echo("ok.\r");
} }
} }
echo("\n"); echo("--------------- Executing database upgrade for '$schema@$host' executed. ---------------\n");
echo("$error_counter errors.\n"); echo("$error_counter errors.\n");
echo("--------------- Executing database upgrade for '$schema@$host' done. ---------------\n"); echo("--------------- Executing database upgrade for '$schema@$host' done. ---------------\n");
echo("--------------- Checking database upgrade for '$schema@$host'... ---------------\n");
$db_def = load_tables_from_db($host, $schema, $user, $passwd, $replacers);
echo("--------------- Comparing database '$schema@$host' vs. JSON '".$compare_def['database']."@".$compare_def['host']."' ---------------\n");
$compare_differences = compare_table_array($compare_def,"in JSON",$db_def,"in DB",true);
echo((empty($compare_differences)?0:count($compare_differences))." differences.\n");
} }
} }
@ -483,7 +526,12 @@ function load_tables_from_db(string $host, string $schema, string $user, string
$columns = array(); $columns = array();
while ($column = mysqli_fetch_assoc($query_result)) { while ($column = mysqli_fetch_assoc($query_result)) {
// Do some harmonization // Do some harmonization
sql_replace_reserved_functions($column,$replacers);
if ($column['Default'] !== NULL) {
sql_replace_reserved_functions($column,$replacers);
$column['Default'] = mysql_put_text_type_in_quotes($column['Type'],$column['Default']);
}
$columns[] = $column; // Add column to list of columns $columns[] = $column; // Add column to list of columns
} }
$table['columns'] = $columns; $table['columns'] = $columns;
@ -565,6 +613,10 @@ function load_tables_from_json(string $path, string $tables_file_name) : array {
$db_def = json_decode($contents, true); $db_def = json_decode($contents, true);
if (!$db_def) {
return(array());
}
return($db_def); return($db_def);
} }
@ -626,7 +678,7 @@ function compare_table_array(array $nominal, string $nominal_name, array $actual
foreach ($column as $key => $value) { foreach ($column as $key => $value) {
if ($found_column[$key] != $value) { if ($found_column[$key] != $value) {
// if ($key != 'permissions') { if ($key != 'Key') { // Keys will be handled separately
$compare_difference = array(); $compare_difference = array();
$compare_difference['type'] = "Column definition"; $compare_difference['type'] = "Column definition";
$compare_difference['table'] = $database_table['name']; $compare_difference['table'] = $database_table['name'];
@ -635,7 +687,7 @@ function compare_table_array(array $nominal, string $nominal_name, array $actual
$compare_difference[$nominal_name] = $value; $compare_difference[$nominal_name] = $value;
$compare_difference[$actual_name] = $found_column[$key]; $compare_difference[$actual_name] = $found_column[$key];
$compare_differences[] = $compare_difference; $compare_differences[] = $compare_difference;
// } }
} }
} }
unset($value); unset($value);
@ -790,6 +842,17 @@ function sql_replace_reserved_functions(array &$column, array $replacers) {
$column['Extra'] = $result; $column['Extra'] = $result;
} }
// Is it a text type? -> Use quotes then
function mysql_put_text_type_in_quotes(string $checktype, string $value) : string {
$types = array('char','varchar','tinytext','text','mediumtext','longtext');
foreach($types as $type) {
if (stripos($checktype, $type) !== false) {
return("'".$value."'");
}
}
return($value);
}
function info() { function info() {
echo("OpenXE database compare\n"); echo("OpenXE database compare\n");

File diff suppressed because it is too large Load Diff