Fix Issue #2816 - Update dbstructure instead of update

- Add UNIQUE index support to `update_structure()`
- Move DB update from `update.php` to `dbstructure.php`
- Update DB_VERSION to 1210
This commit is contained in:
Hypolite Petovan 2016-10-01 23:29:30 -04:00
parent 3c85fb8c9a
commit 3f619726b1
3 changed files with 61 additions and 53 deletions

View file

@ -38,7 +38,7 @@ define ( 'FRIENDICA_PLATFORM', 'Friendica');
define ( 'FRIENDICA_CODENAME', 'Asparagus'); define ( 'FRIENDICA_CODENAME', 'Asparagus');
define ( 'FRIENDICA_VERSION', '3.5.1-dev' ); define ( 'FRIENDICA_VERSION', '3.5.1-dev' );
define ( 'DFRN_PROTOCOL_VERSION', '2.23' ); define ( 'DFRN_PROTOCOL_VERSION', '2.23' );
define ( 'DB_UPDATE_VERSION', 1203 ); define ( 'DB_UPDATE_VERSION', 1210 );
/** /**
* @brief Constant with a HTML line break. * @brief Constant with a HTML line break.

View file

@ -78,6 +78,10 @@ function table_structure($table) {
if ($index["Index_type"] == "FULLTEXT") if ($index["Index_type"] == "FULLTEXT")
continue; 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"]; $column = $index["Column_name"];
// On utf8mb4 a varchar index can only have a length of 191 // 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. // To avoid the need to add this to every index definition we just ignore it here.
@ -151,6 +155,12 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
if (is_null($definition)) if (is_null($definition))
$definition = db_definition($charset); $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);
// Compare it // Compare it
foreach ($definition AS $name => $structure) { foreach ($definition AS $name => $structure) {
@ -176,7 +186,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
if ($current_index_definition != $new_index_definition && substr($indexname, 0, 6) != 'local_') { if ($current_index_definition != $new_index_definition && substr($indexname, 0, 6) != 'local_') {
$sql2=db_drop_index($indexname); $sql2=db_drop_index($indexname);
if ($sql3 == "") if ($sql3 == "")
$sql3 = "ALTER TABLE `".$name."` ".$sql2; $sql3 = "ALTER IGNORE TABLE `".$name."` ".$sql2;
else else
$sql3 .= ", ".$sql2; $sql3 .= ", ".$sql2;
} }
@ -186,7 +196,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
if (!isset($database[$name]["fields"][$fieldname])) { if (!isset($database[$name]["fields"][$fieldname])) {
$sql2=db_add_table_field($fieldname, $parameters); $sql2=db_add_table_field($fieldname, $parameters);
if ($sql3 == "") if ($sql3 == "")
$sql3 = "ALTER TABLE `".$name."` ".$sql2; $sql3 = "ALTER IGNORE TABLE `".$name."` ".$sql2;
else else
$sql3 .= ", ".$sql2; $sql3 .= ", ".$sql2;
} else { } else {
@ -196,7 +206,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
if ($current_field_definition != $new_field_definition) { if ($current_field_definition != $new_field_definition) {
$sql2=db_modify_table_field($fieldname, $parameters); $sql2=db_modify_table_field($fieldname, $parameters);
if ($sql3 == "") if ($sql3 == "")
$sql3 = "ALTER TABLE `".$name."` ".$sql2; $sql3 = "ALTER IGNORE TABLE `".$name."` ".$sql2;
else else
$sql3 .= ", ".$sql2; $sql3 .= ", ".$sql2;
} }
@ -220,7 +230,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
$sql2=db_create_index($indexname, $fieldnames); $sql2=db_create_index($indexname, $fieldnames);
if ($sql2 != "") { if ($sql2 != "") {
if ($sql3 == "") if ($sql3 == "")
$sql3 = "ALTER TABLE `".$name."` ".$sql2; $sql3 = "ALTER IGNORE TABLE `".$name."` ".$sql2;
else else
$sql3 .= ", ".$sql2; $sql3 .= ", ".$sql2;
} }
@ -324,6 +334,11 @@ function db_create_index($indexname, $fieldnames, $method="ADD") {
killme(); killme();
} }
if ($fieldnames[0] == "UNIQUE") {
array_shift($fieldnames);
$method .= ' UNIQUE';
}
$names = ""; $names = "";
foreach ($fieldnames AS $fieldname) { foreach ($fieldnames AS $fieldname) {
if ($names != "") if ($names != "")
@ -451,7 +466,7 @@ function db_definition($charset) {
), ),
"indexes" => array( "indexes" => array(
"PRIMARY" => array("id"), "PRIMARY" => array("id"),
"cat_k" => array("cat(30)","k(30)"), "cat_k" => array("UNIQUE", "cat(30)","k(30)"),
) )
); );
$database["contact"] = array( $database["contact"] = array(
@ -1060,7 +1075,7 @@ function db_definition($charset) {
), ),
"indexes" => array( "indexes" => array(
"PRIMARY" => array("id"), "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( $database["photo"] = array(
@ -1484,6 +1499,9 @@ function dbstructure_run(&$argv, &$argc) {
if ($argc==2) { if ($argc==2) {
switch ($argv[1]) { switch ($argv[1]) {
case "dryrun":
update_structure(true, false);
return;
case "update": case "update":
update_structure(true, true); update_structure(true, true);
set_config('system','build',DB_UPDATE_VERSION); set_config('system','build',DB_UPDATE_VERSION);

View file

@ -1,6 +1,6 @@
<?php <?php
define('UPDATE_VERSION' , 1203); define('UPDATE_VERSION' , 1210);
/** /**
* *
@ -1728,13 +1728,3 @@ function update_1202() {
$r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)", $r = q("UPDATE `user` SET `account-type` = %d WHERE `page-flags` IN (%d, %d)",
dbesc(ACCOUNT_TYPE_COMMUNITY), dbesc(PAGE_COMMUNITY), dbesc(PAGE_PRVGROUP)); dbesc(ACCOUNT_TYPE_COMMUNITY), dbesc(PAGE_COMMUNITY), dbesc(PAGE_PRVGROUP));
} }
function update_1210() {
// Convert config indexes to unique, old_alter_table=1 removes duplicates on ALTER IGNORE
$r = q("SET session old_alter_table=1;");
$r = q("ALTER TABLE config DROP INDEX cat_k");
$r = q("ALTER IGNORE TABLE config ADD UNIQUE INDEX cat_k (cat, k)");
$r = q("ALTER TABLE pconfig DROP INDEX uid_cat_k");
$r = q("ALTER IGNORE TABLE pconfig ADD UNIQUE INDEX uid_cat_k (uid, cat, k)");
}