*/
//try the logger
logger("CRITICAL: Database structure update failed: ".$retval);
- break;
}
}
function print_structure($database) {
+ echo "-- ------------------------------------------\n";
+ echo "-- ".FRIENDICA_PLATFORM." ".FRIENDICA_VERSION." (".FRIENDICA_CODENAME,")\n";
+ echo "-- DB_UPDATE_VERSION ".DB_UPDATE_VERSION."\n";
+ echo "-- ------------------------------------------\n\n\n";
foreach ($database AS $name => $structure) {
- echo "\t".'$database["'.$name."\"] = array(\n";
+ echo "--\n";
+ echo "-- TABLE $name\n";
+ echo "--\n";
+ db_create_table($name, $structure['fields'], true, false, $structure["indexes"]);
- echo "\t\t\t".'"fields" => array('."\n";
- foreach ($structure["fields"] AS $fieldname => $parameters) {
- echo "\t\t\t\t\t".'"'.$fieldname.'" => array(';
-
- $data = "";
- foreach ($parameters AS $name => $value) {
- if ($data != "")
- $data .= ", ";
- $data .= '"'.$name.'" => "'.$value.'"';
- }
-
- echo $data."),\n";
- }
- echo "\t\t\t\t\t),\n";
- echo "\t\t\t".'"indexes" => array('."\n";
- foreach ($structure["indexes"] AS $indexname => $fieldnames) {
- echo "\t\t\t\t\t".'"'.$indexname.'" => array("'.implode($fieldnames, '","').'"'."),\n";
- }
- echo "\t\t\t\t\t)\n";
- echo "\t\t\t);\n";
+ echo "\n";
}
}
-function update_structure($verbose, $action) {
+function update_structure($verbose, $action, $tables=null, $definition=null) {
global $a, $db;
$errors = false;
// Get the current structure
$database = array();
- $tables = q("show tables");
+ if (is_null($tables))
+ $tables = q("show tables");
foreach ($tables AS $table) {
$table = current($table);
}
// Get the definition
- $definition = db_definition();
+ if (is_null($definition))
+ $definition = db_definition();
+
// Compare it
foreach ($definition AS $name => $structure) {
+ $is_new_table = False;
$sql3="";
if (!isset($database[$name])) {
- $r = db_create_table($name, $structure["fields"], $verbose, $action);
- if(false === $r)
+ $r = db_create_table($name, $structure["fields"], $verbose, $action, $structure['indexes']);
+ if(false === $r) {
$errors .= t('Errors encountered creating database tables.').$name.EOL;
+ }
+ $is_new_table = True;
} else {
// Drop the index if it isn't present in the definition
- foreach ($database[$name]["indexes"] AS $indexname => $fieldnames)
- if (!isset($structure["indexes"][$indexname])) {
+ // or the definition differ from current status
+ // and index name doesn't start with "local_"
+ foreach ($database[$name]["indexes"] AS $indexname => $fieldnames) {
+ $current_index_definition = implode(",",$fieldnames);
+ if (isset($structure["indexes"][$indexname])) {
+ $new_index_definition = implode(",",$structure["indexes"][$indexname]);
+ } else {
+ $new_index_definition = "__NOT_SET__";
+ }
+ if ($current_index_definition != $new_index_definition && substr($indexname, 0, 6) != 'local_') {
$sql2=db_drop_index($indexname);
if ($sql3 == "")
$sql3 = "ALTER TABLE `".$name."` ".$sql2;
else
$sql3 .= ", ".$sql2;
}
-
+ }
// Compare the field structure field by field
foreach ($structure["fields"] AS $fieldname => $parameters) {
if (!isset($database[$name]["fields"][$fieldname])) {
}
}
- // Create the index
- foreach ($structure["indexes"] AS $indexname => $fieldnames) {
- if (!isset($database[$name]["indexes"][$indexname])) {
- $sql2=db_create_index($indexname, $fieldnames);
- if ($sql2 != "") {
- if ($sql3 == "")
- $sql3 = "ALTER TABLE `".$name."` ".$sql2;
- else
- $sql3 .= ", ".$sql2;
+ // Create the index if the index don't exists in database
+ // or the definition differ from the current status.
+ // Don't create keys if table is new
+ if (!$is_new_table) {
+ foreach ($structure["indexes"] AS $indexname => $fieldnames) {
+ if (isset($database[$name]["indexes"][$indexname])) {
+ $current_index_definition = implode(",",$database[$name]["indexes"][$indexname]);
+ } else {
+ $current_index_definition = "__NOT_SET__";
+ }
+ $new_index_definition = implode(",",$fieldnames);
+ if ($current_index_definition != $new_index_definition) {
+ $sql2=db_create_index($indexname, $fieldnames);
+ if ($sql2 != "") {
+ if ($sql3 == "")
+ $sql3 = "ALTER TABLE `".$name."` ".$sql2;
+ else
+ $sql3 .= ", ".$sql2;
+ }
}
}
}
-
if ($sql3 != "") {
$sql3 .= ";";
if ($parameters["not null"])
$fieldstruct .= " NOT NULL";
- if (isset($parameters["default"]))
- $fieldstruct .= " DEFAULT '".$parameters["default"]."'";
-
+ if (isset($parameters["default"])){
+ if (strpos(strtolower($parameters["type"]),"int")!==false) {
+ $fieldstruct .= " DEFAULT ".$parameters["default"];
+ } else {
+ $fieldstruct .= " DEFAULT '".$parameters["default"]."'";
+ }
+ }
if ($parameters["extra"] != "")
$fieldstruct .= " ".$parameters["extra"];
- if (($parameters["primary"] != "") AND $create)
- $fieldstruct .= " PRIMARY KEY";
+ /*if (($parameters["primary"] != "") AND $create)
+ $fieldstruct .= " PRIMARY KEY";*/
return($fieldstruct);
}
-function db_create_table($name, $fields, $verbose, $action) {
+function db_create_table($name, $fields, $verbose, $action, $indexes=null) {
global $a, $db;
$r = true;
$sql = "";
+
+ $sql_rows = array();
+ $primary_keys = array();
foreach($fields AS $fieldname => $field) {
- if ($sql != "")
- $sql .= ",\n";
+ $sql_rows[] = "`".dbesc($fieldname)."` ".db_field_command($field);
+ if (x($field,'primary') and $field['primary']!=''){
+ $primary_keys[] = $fieldname;
+ }
+ }
- $sql .= "`".dbesc($fieldname)."` ".db_field_command($field);
+ if (!is_null($indexes)) {
+ foreach ($indexes AS $indexname => $fieldnames) {
+ $sql_index = db_create_index($indexname, $fieldnames, "");
+ if (!is_null($sql_index)) $sql_rows[] = $sql_index;
+ }
}
- $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n", dbesc($name)).$sql."\n) DEFAULT CHARSET=utf8";
+ $sql = implode(",\n\t", $sql_rows);
+ $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", dbesc($name)).$sql."\n) DEFAULT CHARSET=utf8";
if ($verbose)
echo $sql.";\n";
return($sql);
}
-function db_create_index($indexname, $fieldnames) {
+function db_create_index($indexname, $fieldnames, $method="ADD") {
+
+ $method = strtoupper(trim($method));
+ if ($method!="" && $method!="ADD") {
+ throw new Exception("Invalid parameter 'method' in db_create_index(): '$method'");
+ killme();
+ }
+
- if ($indexname == "PRIMARY")
- return;
+ if ($indexname == "PRIMARY") {
+ return sprintf("%s PRIMARY KEY(`%s`)", $method, implode("`,`", $fieldnames));
+ }
$names = "";
foreach ($fieldnames AS $fieldname) {
$names .= "`".dbesc($fieldname)."`";
}
- $sql = sprintf("ADD INDEX `%s` (%s)", dbesc($indexname), $names);
+
+ $sql = sprintf("%s INDEX `%s` (%s)", $method, dbesc($indexname), $names);
return($sql);
}
"fields" => array(
"k" => array("type" => "varchar(255)", "not null" => "1", "primary" => "1"),
"v" => array("type" => "text", "not null" => "1"),
+ "expire_mode" => array("type" => "int(11)", "not null" => "1", "default" => "0"),
"updated" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
),
"indexes" => array(
"hub-verify" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
"last-update" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
"success_update" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
+ "failure_update" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
"name-date" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
"uri-date" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
"avatar-date" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
"term-date" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
+ "last-item" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
"priority" => array("type" => "tinyint(3)", "not null" => "1", "default" => "0"),
"blocked" => array("type" => "tinyint(1)", "not null" => "1", "default" => "1"),
"readonly" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
"fields" => array(
"id" => array("type" => "int(10) unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
"name" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "nick" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
"url" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
"nurl" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
"photo" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
"connect" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "created" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
"updated" => array("type" => "datetime", "default" => "0000-00-00 00:00:00"),
+ "last_contact" => array("type" => "datetime", "default" => "0000-00-00 00:00:00"),
+ "last_failure" => array("type" => "datetime", "default" => "0000-00-00 00:00:00"),
"location" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
"about" => array("type" => "text", "not null" => "1"),
"keywords" => array("type" => "text", "not null" => "1"),
"gender" => array("type" => "varchar(32)", "not null" => "1", "default" => ""),
+ "birthday" => array("type" => "varchar(32)", "not null" => "1", "default" => "0000-00-00"),
+ "community" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
+ "hide" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
+ "nsfw" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
"network" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "addr" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "notify" => array("type" => "text", "not null" => "1"),
+ "alias" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
"generation" => array("type" => "tinyint(3)", "not null" => "1", "default" => "0"),
+ "server_url" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
),
"indexes" => array(
"PRIMARY" => array("id"),
"nurl" => array("nurl"),
+ "updated" => array("updated"),
)
);
$database["glink"] = array(
"uid_gid_contactid" => array("uid","gid","contact-id"),
)
);
+ $database["gserver"] = array(
+ "fields" => array(
+ "id" => array("type" => "int(10) unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
+ "url" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "nurl" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "version" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "site_name" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "info" => array("type" => "text", "not null" => "1"),
+ "register_policy" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
+ "poco" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "noscrape" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "network" => array("type" => "varchar(32)", "not null" => "1", "default" => ""),
+ "platform" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "created" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
+ "last_poco_query" => array("type" => "datetime", "default" => "0000-00-00 00:00:00"),
+ "last_contact" => array("type" => "datetime", "default" => "0000-00-00 00:00:00"),
+ "last_failure" => array("type" => "datetime", "default" => "0000-00-00 00:00:00"),
+ ),
+ "indexes" => array(
+ "PRIMARY" => array("id"),
+ "nurl" => array("nurl"),
+ )
+ );
$database["guid"] = array(
"fields" => array(
"id" => array("type" => "int(10) unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
"guid" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "plink" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "uri" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "network" => array("type" => "varchar(32)", "not null" => "1", "default" => ""),
),
"indexes" => array(
"PRIMARY" => array("id"),
"guid" => array("guid"),
+ "plink" => array("plink"),
+ "uri" => array("uri"),
)
);
$database["hook"] = array(
"uri" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
"uid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0"),
"contact-id" => array("type" => "int(11)", "not null" => "1", "default" => "0"),
+ "gcontact-id" => array("type" => "int(11) unsigned", "not null" => "1", "default" => "0"),
"type" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
"wall" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
"gravity" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
"uid_thrparent" => array("uid","thr-parent"),
"uid_parenturi" => array("uid","parent-uri"),
"uid_contactid_created" => array("uid","contact-id","created"),
+ "gcontactid_uid_created" => array("gcontact-id","uid","created"),
"wall_body" => array("wall","body(6)"),
"uid_visible_moderated_created" => array("uid","visible","moderated","created"),
"uid_uri" => array("uid","uri"),
"msg" => array("type" => "mediumtext", "not null" => "1"),
"uid" => array("type" => "int(11)", "not null" => "1", "default" => "0"),
"link" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+ "iid" => array("type" => "int(11)", "not null" => "1", "default" => "0"),
"parent" => array("type" => "int(11)", "not null" => "1", "default" => "0"),
"seen" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
"verb" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
"oid_otype_type_term" => array("oid","otype","type","term"),
"uid_term_tid" => array("uid","term","tid"),
"type_term" => array("type","term"),
- "uid_otype_type_term_created" => array("uid","otype","type","term","created"),
+ "uid_otype_type_term_global_created" => array("uid","otype","type","term","global","created"),
"otype_type_term_tid" => array("otype","type","term","tid"),
"guid" => array("guid"),
)
"iid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0", "primary" => "1"),
"uid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0"),
"contact-id" => array("type" => "int(11) unsigned", "not null" => "1", "default" => "0"),
+ "gcontact-id" => array("type" => "int(11) unsigned", "not null" => "1", "default" => "0"),
"created" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
"edited" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
"commented" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
"uid_network_created" => array("uid","network","created"),
"uid_contactid_commented" => array("uid","contact-id","commented"),
"uid_contactid_created" => array("uid","contact-id","created"),
+ "uid_gcontactid_commented" => array("uid","gcontact-id","commented"),
+ "uid_gcontactid_created" => array("uid","gcontact-id","created"),
"wall_private_received" => array("wall","private","received"),
"uid_created" => array("uid","created"),
"uid_commented" => array("uid","commented"),
"PRIMARY" => array("id"),
)
);
- $database["unique_contacts"] = array(
- "fields" => array(
- "id" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
- "url" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
- "nick" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
- "name" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
- "avatar" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
- "location" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
- "about" => array("type" => "text", "not null" => "1"),
- ),
- "indexes" => array(
- "PRIMARY" => array("id"),
- "url" => array("url"),
- )
- );
$database["user"] = array(
"fields" => array(
"uid" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
"username" => array("username"),
)
);
+ $database["workerqueue"] = array(
+ "fields" => array(
+ "id" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
+ "parameter" => array("type" => "text", "not null" => "1"),
+ "priority" => array("type" => "tinyint(3) unsigned", "not null" => "1", "default" => "0"),
+ "created" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
+ "pid" => array("type" => "int(11)", "not null" => "1", "default" => "0"),
+ "executed" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
+ ),
+ "indexes" => array(
+ "PRIMARY" => array("id"),
+ "created" => array("created"),
+ )
+ );
return($database);
}
unset($db_host, $db_user, $db_pass, $db_data);
}
- update_structure(true, true);
+ if ($argc==2) {
+ switch ($argv[1]) {
+ case "update":
+ update_structure(true, true);
+ return;
+ case "dumpsql":
+ print_structure(db_definition());
+ return;
+ }
+ }
+
+
+ // print help
+ echo $argv[0]." <command>\n";
+ echo "\n";
+ echo "commands:\n";
+ echo "update update database schema\n";
+ echo "dumpsql dump database schema\n";
+ return;
+
+
+
+
}
if (array_search(__file__,get_included_files())===0){