bugfix mustal use correct syntax for BTREE and HASH key types

This commit is contained in:
OpenXE 2023-08-03 14:43:52 +02:00
parent 6242024451
commit 623b041a5f
2 changed files with 24 additions and 15 deletions

View File

@ -365,6 +365,9 @@ function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do
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) {
if (is_array($value)) {
$value = implode(',',$value);
}
echo_out($comma."$key => [$value]"); echo_out($comma."$key => [$value]");
$comma = ", "; $comma = ", ";
} }

View File

@ -67,6 +67,7 @@ $mustal_replacers = [
['on update current_timestamp','on update current_timestamp()'] ['on update current_timestamp','on update current_timestamp()']
]; ];
// Load all db_def from a DB connection into a db_def array // Load all db_def from a DB connection into a db_def array
function mustal_load_tables_from_db(string $host, string $schema, string $user, string $passwd, array $replacers) : array { function mustal_load_tables_from_db(string $host, string $schema, string $user, string $passwd, array $replacers) : array {
@ -358,8 +359,6 @@ function mustal_compare_table_array(array $nominal, string $nominal_name, array
$compare_difference['table'] = $database_table['name']; $compare_difference['table'] = $database_table['name'];
$compare_difference['key'] = $sql_index['Key_name']; $compare_difference['key'] = $sql_index['Key_name'];
$compare_difference['property'] = $key; $compare_difference['property'] = $key;
/* $compare_difference[$nominal_name] = implode(',',$value);
$compare_difference[$actual_name] = implode(',',$found_sql_index[$key]);*/
$compare_difference[$nominal_name] = $value; $compare_difference[$nominal_name] = $value;
$compare_difference[$actual_name] = $found_sql_index[$key]; $compare_difference[$actual_name] = $found_sql_index[$key];
$compare_differences[] = $compare_difference; $compare_differences[] = $compare_difference;
@ -585,14 +584,7 @@ function mustal_calculate_db_upgrade(array $compare_def, array $db_def, array &$
if ($key['Key_name'] == 'PRIMARY') { if ($key['Key_name'] == 'PRIMARY') {
$keystring = "PRIMARY KEY "; $keystring = "PRIMARY KEY ";
} else { } else {
$keystring = mustal_key_type(" ".$key['Non_unique']." KEY `".$key['Key_name']."` ",$key['Index_type']);
// if(array_key_exists('Index_type', $key)) {
// $index_type = $key['Index_type'];
// } else {
$index_type = "";
// }
$keystring = $index_type." ".$key['Non_unique']." KEY `".$key['Key_name']."` ";
} }
$sql .= $comma.$keystring."(`".implode("`,`",$key['columns'])."`) "; $sql .= $comma.$keystring."(`".implode("`,`",$key['columns'])."`) ";
} }
@ -675,10 +667,7 @@ function mustal_calculate_db_upgrade(array $compare_def, array $db_def, array &$
if ($key_key !== false) { if ($key_key !== false) {
$key = $table['keys'][$key_key]; $key = $table['keys'][$key_key];
$sql = "ALTER TABLE `$table_name` ADD ".mustal_key_type(" ".$key['Non_unique']." KEY `".$key['Key_name']."` "."(`".implode("`,`",$key['columns'])."`)",$key['Index_type']).";";
$sql = "ALTER TABLE `$table_name` ADD ".$key['Non_unique']." KEY `".$key_name."` ";
$sql .= "(`".implode("`,`",$key['columns'])."`)";
$sql .= ";";
$upgrade_sql[] = $sql; $upgrade_sql[] = $sql;
} }
else { else {
@ -706,7 +695,7 @@ function mustal_calculate_db_upgrade(array $compare_def, array $db_def, array &$
$sql = "ALTER TABLE `$table_name` DROP KEY `".$key_name."`;"; $sql = "ALTER TABLE `$table_name` DROP KEY `".$key_name."`;";
$upgrade_sql[] = $sql; $upgrade_sql[] = $sql;
$sql = "ALTER TABLE `$table_name` ADD ".$key['Non_unique']." KEY `".$key_name."` "; $sql = "ALTER TABLE `$table_name` ADD ".mustal_key_type(" ".$key['Non_unique']." KEY `".$key['Key_name']."` ",$key['Index_type']);
$sql .= "(`".implode("`,`",$key['columns'])."`)"; $sql .= "(`".implode("`,`",$key['columns'])."`)";
$sql .= ";"; $sql .= ";";
$upgrade_sql[] = $sql; $upgrade_sql[] = $sql;
@ -781,3 +770,20 @@ function mustal_is_string_type(string $type) {
return(false); return(false);
} }
// create correct index type syntax
function mustal_key_type(string $key_definition_string, string $key_type) {
// Key types with using syntax
$mustal_key_types_using_mapping = [
'BTREE',
'HASH'
];
if (in_array($key_type,$mustal_key_types_using_mapping)) {
return ($key_definition_string." USING ".$key_type);
} else {
return ($key_type." ".$key_definition_string);
}
}