コード例 #1
0
ファイル: dbstructure.php プロジェクト: strk/friendica
function dbstructure_run(&$argv, &$argc)
{
    global $a, $db;
    if (is_null($a)) {
        $a = new App();
    }
    if (is_null($db)) {
        @(include ".htconfig.php");
        require_once "include/dba.php";
        $db = new dba($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);
                return;
            case "dumpsql":
                print_structure(db_definition());
                return;
        }
    }
    // print help
    echo $argv[0] . " <command>\n";
    echo "\n";
    echo "commands:\n";
    echo "update\t\tupdate database schema\n";
    echo "dumpsql\t\tdump database schema\n";
    return;
}
コード例 #2
0
ファイル: dbstructure.php プロジェクト: jzacman/friendica
function update_structure($verbose, $action)
{
    global $a, $db;
    $errors = false;
    logger('updating structure', LOGGER_DEBUG);
    // Get the current structure
    $database = array();
    $tables = q("show tables");
    foreach ($tables as $table) {
        $table = current($table);
        $database[$table] = table_structure($table);
    }
    // Get the definition
    $definition = db_definition();
    // Compare it
    foreach ($definition as $name => $structure) {
        $sql3 = "";
        if (!isset($database[$name])) {
            $r = db_create_table($name, $structure["fields"], $verbose, $action);
            if (false === $r) {
                $errors .= t('Errors encountered creating database tables.') . $name . EOL;
            }
        } else {
            // Drop the index if it isn't present in the definition
            foreach ($database[$name]["indexes"] as $indexname => $fieldnames) {
                if (!isset($structure["indexes"][$indexname])) {
                    $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])) {
                    $sql2 = db_add_table_field($fieldname, $parameters);
                    if ($sql3 == "") {
                        $sql3 = "ALTER TABLE `" . $name . "` " . $sql2;
                    } else {
                        $sql3 .= ", " . $sql2;
                    }
                } else {
                    // Compare the field definition
                    $current_field_definition = implode($database[$name]["fields"][$fieldname]);
                    $new_field_definition = implode($parameters);
                    if ($current_field_definition != $new_field_definition) {
                        $sql2 = db_modify_table_field($fieldname, $parameters);
                        if ($sql3 == "") {
                            $sql3 = "ALTER TABLE `" . $name . "` " . $sql2;
                        } else {
                            $sql3 .= ", " . $sql2;
                        }
                    }
                }
            }
        }
        // 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;
                    }
                }
            }
        }
        if ($sql3 != "") {
            $sql3 .= ";";
            if ($verbose) {
                echo $sql3 . "\n";
            }
            if ($action) {
                $r = @$db->q($sql3);
                if (false === $r) {
                    $errors .= t('Errors encountered performing database changes.') . $sql3 . EOL;
                }
            }
        }
    }
    return $errors;
}