if ($index["Index_type"] == "FULLTEXT")
continue;
+ if ($index['Key_name'] != 'PRIMARY' && $index['Non_unique'] == '0' && !isset($indexdata[$index["Key_name"]])) {
+ $indexdata[$index["Key_name"]] = array('UNIQUE');
+ }
+
$column = $index["Column_name"];
// On utf8mb4 a varchar index can only have a length of 191
// To avoid the need to add this to every index definition we just ignore it here.
function update_structure($verbose, $action, $tables=null, $definition=null) {
global $a, $db;
+ if ($action)
+ set_config('system', 'maintenance', 1);
+
if (isset($a->config["system"]["db_charset"]))
$charset = $a->config["system"]["db_charset"];
else
if (is_null($definition))
$definition = db_definition($charset);
+ // Ensure index conversion to unique removes duplicates
+ $sql_config = "SET session old_alter_table=1;";
+ if ($verbose)
+ echo $sql_config."\n";
+ if ($action)
+ @$db->q($sql_config);
+
+ // MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements
+ if (version_compare($db->getdb()->server_info, '5.7.4') >= 0) {
+ $ignore = '';
+ }else {
+ $ignore = ' IGNORE';
+ }
// Compare it
foreach ($definition AS $name => $structure) {
$sql2=db_create_index($indexname, $fieldnames);
if ($sql2 != "") {
if ($sql3 == "")
- $sql3 = "ALTER TABLE `".$name."` ".$sql2;
+ $sql3 = "ALTER" . $ignore . " TABLE `".$name."` ".$sql2;
else
$sql3 .= ", ".$sql2;
}
}
}
+ if ($action)
+ set_config('system', 'maintenance', 0);
+
return $errors;
}
killme();
}
+ if ($fieldnames[0] == "UNIQUE") {
+ array_shift($fieldnames);
+ $method .= ' UNIQUE';
+ }
+
$names = "";
foreach ($fieldnames AS $fieldname) {
if ($names != "")
}
if ($indexname == "PRIMARY") {
- //return sprintf("%s PRIMARY KEY(`%s`)", $method, implode("`,`", $fieldnames));
return sprintf("%s PRIMARY KEY(%s)", $method, $names);
}
),
"indexes" => array(
"PRIMARY" => array("id"),
- "cat_k" => array("cat(30)","k(30)"),
+ "cat_k" => array("UNIQUE", "cat(30)","k(30)"),
)
);
$database["contact"] = array(
),
"indexes" => array(
"PRIMARY" => array("id"),
- "uid_cat_k" => array("uid","cat(30)","k(30)"),
+ "uid_cat_k" => array("UNIQUE", "uid","cat(30)","k(30)"),
)
);
$database["photo"] = array(
if ($argc==2) {
switch ($argv[1]) {
+ case "dryrun":
+ update_structure(true, false);
+ return;
case "update":
update_structure(true, true);
set_config('system','build',DB_UPDATE_VERSION);
// print help
echo $argv[0]." <command>\n";
echo "\n";
- echo "commands:\n";
+ echo "Commands:\n";
+ echo "dryrun show database update schema queries without running them\n";
echo "update update database schema\n";
echo "dumpsql dump database schema\n";
return;