Merge pull request #1449 from fabrixxm/issue-1370

Create `database.sql` from `include\dbstructure.php`
This commit is contained in:
Michael Vogel 2015-03-25 22:02:50 +01:00
commit dbb164ca4a
2 changed files with 881 additions and 1104 deletions

File diff suppressed because it is too large Load diff

View file

@ -106,29 +106,17 @@ function table_structure($table) {
} }
function print_structure($database) { 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) { 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"; echo "\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";
} }
} }
@ -231,9 +219,13 @@ function db_field_command($parameters, $create = true) {
if ($parameters["not null"]) if ($parameters["not null"])
$fieldstruct .= " NOT NULL"; $fieldstruct .= " NOT NULL";
if (isset($parameters["default"])) if (isset($parameters["default"])){
if (strpos(strtolower($parameters["type"]),"int")!==false) {
$fieldstruct .= " DEFAULT ".$parameters["default"];
} else {
$fieldstruct .= " DEFAULT '".$parameters["default"]."'"; $fieldstruct .= " DEFAULT '".$parameters["default"]."'";
}
}
if ($parameters["extra"] != "") if ($parameters["extra"] != "")
$fieldstruct .= " ".$parameters["extra"]; $fieldstruct .= " ".$parameters["extra"];
@ -243,20 +235,28 @@ function db_field_command($parameters, $create = true) {
return($fieldstruct); return($fieldstruct);
} }
function db_create_table($name, $fields, $verbose, $action) { function db_create_table($name, $fields, $verbose, $action, $indexes=null) {
global $a, $db; global $a, $db;
$r = true; $r = true;
$sql = ""; $sql = "";
$sql_rows = array();
foreach($fields AS $fieldname => $field) { foreach($fields AS $fieldname => $field) {
if ($sql != "") $sql_rows[] = "`".dbesc($fieldname)."` ".db_field_command($field);
$sql .= ",\n";
$sql .= "`".dbesc($fieldname)."` ".db_field_command($field);
} }
$sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n", dbesc($name)).$sql."\n) DEFAULT CHARSET=utf8"; 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 = implode(",\n\t", $sql_rows);
$sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", dbesc($name)).$sql."\n) DEFAULT CHARSET=utf8";
if ($verbose) if ($verbose)
echo $sql.";\n"; echo $sql.";\n";
@ -282,7 +282,7 @@ function db_drop_index($indexname) {
return($sql); return($sql);
} }
function db_create_index($indexname, $fieldnames) { function db_create_index($indexname, $fieldnames, $method="ADD") {
if ($indexname == "PRIMARY") if ($indexname == "PRIMARY")
return; return;
@ -298,7 +298,13 @@ function db_create_index($indexname, $fieldnames) {
$names .= "`".dbesc($fieldname)."`"; $names .= "`".dbesc($fieldname)."`";
} }
$sql = sprintf("ADD INDEX `%s` (%s)", dbesc($indexname), $names); $method = strtoupper(trim($method));
if ($method!="" && $method!="ADD") {
throw new Exception("Invalid parameter 'method' in db_create_index(): '$method'");
killme();
}
$sql = sprintf("%s INDEX `%s` (%s)", $method, dbesc($indexname), $names);
return($sql); return($sql);
} }
@ -1357,7 +1363,29 @@ function dbstructure_run(&$argv, &$argc) {
unset($db_host, $db_user, $db_pass, $db_data); unset($db_host, $db_user, $db_pass, $db_data);
} }
if ($argc==2) {
switch ($argv[1]) {
case "update":
update_structure(true, true); 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){ if (array_search(__file__,get_included_files())===0){