upgrade new function drop_keys

This commit is contained in:
OpenXE 2024-03-26 10:55:53 +00:00
parent 37ed5ba246
commit c3d60a48fd
3 changed files with 33 additions and 11 deletions

View File

@ -119,6 +119,12 @@ if (php_sapi_name() == "cli") {
$strict_db = false;
}
if (in_array('-drop_keys', $argv)) {
$drop_keys = true;
} else {
$drop_keys = false;
}
if (in_array('-do', $argv)) {
if (!$check_git && !$check_db) {
$do_git = true;
@ -143,7 +149,8 @@ if (php_sapi_name() == "cli") {
do_db: $do_db,
force: $force,
connection: $connection,
origin: $origin);
origin: $origin,
drop_keys: $drop_keys);
} else {
info();
}
@ -155,7 +162,7 @@ if (php_sapi_name() == "cli") {
}
// -------------------------------- END
function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do_git, bool $export_db, bool $check_db, bool $strict_db, bool $do_db, bool $force, bool $connection, bool $origin) {
function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do_git, bool $export_db, bool $check_db, bool $strict_db, bool $do_db, bool $force, bool $connection, bool $origin, bool $drop_keys) {
$mainfolder = dirname($directory);
$datafolder = $directory."/data";
@ -395,7 +402,7 @@ function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do
echo_out("--------------- Calculating database upgrade for '$schema@$host'... ---------------\n");
$upgrade_sql = array();
$result = mustal_calculate_db_upgrade($compare_def, $db_def, $upgrade_sql, $mustal_replacers, $strict_db);
$result = mustal_calculate_db_upgrade($compare_def, $db_def, $upgrade_sql, $mustal_replacers, $strict_db, $drop_keys);
if (!empty($result)) {
abort(count($result)." errors.\n");
@ -411,7 +418,7 @@ function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do
foreach($upgrade_sql as $statement) {
echo_out($statement."\n");
}
}
}
echo_out(count($upgrade_sql)." upgrade statements\n");

View File

@ -21,7 +21,7 @@ function mustal_compare_table_array(array $nominal, string $nominal_name, array
Compare two database structures
Returns a structured array containing information on all the differences.
function mustal_calculate_db_upgrade(array $compare_def, array $db_def, array &$upgrade_sql, bool $strict) : int
function mustal_calculate_db_upgrade(array $compare_def, array $db_def, array &$upgrade_sql, array $replacers, bool $strict, bool $drop_keys) : array
Generate the SQL needed to upgrade the database to match the definition, based on a comparison.
Data structure in Array and JSON
@ -542,11 +542,22 @@ function mustal_implode_with_quote(string $quote, string $delimiter, array $arra
// 11 Table type upgrade not supported
// 12 Upgrade type not supported
function mustal_calculate_db_upgrade(array $compare_def, array $db_def, array &$upgrade_sql, array $replacers, bool $strict) : array {
function mustal_calculate_db_upgrade(array $compare_def, array $db_def, array &$upgrade_sql, array $replacers, bool $strict, bool $drop_keys) : array {
$result = array();
$upgrade_sql = array();
if ($drop_keys) {
foreach ($db_def['tables'] as $table_id => $table) {
foreach ($table['keys'] as $key_id => $key) {
if ($key['Key_name'] != 'PRIMARY') {
$upgrade_sql[] = "ALTER TABLE `".$table['name']. "` DROP KEY `".$key['Key_name']."`;";
unset($db_def['tables'][$table_id]['keys'][$key_id]);
}
}
}
}
$compare_differences = mustal_compare_table_array($compare_def,"in JSON",$db_def,"in DB",true,true);
foreach ($compare_differences as $compare_difference) {
@ -749,7 +760,7 @@ function mustal_calculate_db_upgrade(array $compare_def, array $db_def, array &$
}
}
$upgrade_sql = array_unique($upgrade_sql);
$upgrade_sql = array_unique($upgrade_sql);
if (count($upgrade_sql) > 0) {
array_unshift($upgrade_sql,"SET SQL_MODE='ALLOW_INVALID_DATES';");

View File

@ -57,7 +57,8 @@ class upgrade {
do_db: false,
force: $force,
connection: false,
origin: false
origin: false,
drop_keys: false
);
break;
case 'do_upgrade':
@ -72,7 +73,8 @@ class upgrade {
do_db: true,
force: $force,
connection: false,
origin: false
origin: false,
drop_keys: false
);
break;
case 'check_db':
@ -88,7 +90,8 @@ class upgrade {
do_db: false,
force: $force,
connection: false,
origin: false
origin: false,
drop_keys: false
);
break;
case 'do_db_upgrade':
@ -104,7 +107,8 @@ class upgrade {
do_db: true,
force: $force,
connection: false,
origin: false
origin: false,
drop_keys: false
);
break;
case 'refresh':