Knowledge Base

Manually update database schema
Article ID: 27
Written By: Marshalrusty
Written On: Fri Apr 27, 2007 9:47 pm
Description: If update_to_latest.php has failed, this script will update the schema to the latest version
link to this article on phpbb.com: Select All
[kb=manually-update-database-schema]Manually update database schema[/kb]
link to this article on your own board: Select All
[url=http://www.phpbb.com/kb/article/manually-update-database-schema/]Knowledge Base - Manually update database schema[/url]

Please note that this will only work if your database is MySQL.

Some hosts (Yahoo, for example) do not allow the ALTER command to be run from scripts (which may be the reason that install/update_to_latest.php failed in the first place). If you get permission errors, then you will need to run the queries manually using phpMyAdmin, a database management tool that is likely available in the host's control panel. It will hopefully have the necessary permissions.

Automatic
  1. Create a fix.php file with the following:
    Code: Select all
    <?php
    define('IN_PHPBB', true);
    include('./config.php');
    include('./includes/constants.php');

    // connect to the database server
    $db = mysql_connect($dbhost,$dbuser,$dbpasswd);
    if (!$db) die('Unable to connect to the database');

    // select the PHPBB database
    mysql_select_db($dbname, $db);

    // Run the queries
    $sql = array();
    // Added in 2.0.5
    $sql[] = "CREATE TABLE " . $table_prefix . "confirm (`confirm_id` char(32) NOT NULL default '', `session_id` char(32) NOT NULL default '', `code` char(6) NOT NULL default '', PRIMARY KEY (`session_id`,`confirm_id`)) TYPE=MyISAM;";
    // Added in 2.0.15
    $sql[] = "ALTER TABLE " . SESSIONS_TABLE . " ADD COLUMN session_admin tinyint(2) DEFAULT '0' NOT NULL";
    // Added in 2.0.18
    $sql[] = "CREATE TABLE " . $table_prefix . "sessions_keys (`key_id` varchar(32) NOT NULL default '0', `user_id` mediumint(8) NOT NULL default '0', `last_ip` varchar(8) NOT NULL default '0', `last_login` int(11) NOT NULL default '0', PRIMARY KEY (`key_id`,`user_id`), KEY `last_login` (`last_login`)) TYPE=MyISAM;";
    $sql[] = "UPDATE " . USERS_TABLE . " SET user_active = 0 WHERE user_id = -1";
    $sql[] = "INSERT INTO " . CONFIG_TABLE . " (config_name, config_value) VALUES ('allow_autologin','1')";
    $sql[] = "INSERT INTO " . CONFIG_TABLE . " (config_name, config_value) VALUES ('max_autologin_time','0')";
    // Added in 2.0.19
    $sql[] = "ALTER TABLE " . USERS_TABLE . " ADD COLUMN user_last_login_try int(11) DEFAULT '0' NOT NULL";
    $sql[] = "ALTER TABLE " . USERS_TABLE . " ADD COLUMN user_login_tries smallint(5) unsigned NOT NULL default '0'";
    $sql[] = "INSERT INTO " . CONFIG_TABLE . " (config_name, config_value) VALUES ('max_login_attempts', '5')";
    $sql[] = "INSERT INTO " . CONFIG_TABLE . " (config_name, config_value) VALUES ('login_reset_time', '30')";
    // Added in 2.0.20
    $sql[] = "ALTER TABLE " . SEARCH_TABLE . " ADD COLUMN `search_time` int(11) NOT NULL default '0'";
    $sql[] = "INSERT INTO " . CONFIG_TABLE . " (config_name, config_value) VALUES ('search_flood_interval', '15')";
    $sql[] = "INSERT INTO " . CONFIG_TABLE . " (config_name, config_value) VALUES ('rand_seed', '0')";
    // Added in 2.0.22
    $sql[] = "ALTER TABLE " . SEARCH_TABLE . " MODIFY COLUMN search_array MEDIUMTEXT NOT NULL";
    // General
    $sql[] = "UPDATE " . CONFIG_TABLE . " SET config_value = '.0.22' WHERE config_name = 'version'";
    $sql[] = "TRUNCATE TABLE " . SESSIONS_TABLE . "";
    $sql[] = "TRUNCATE TABLE " . SESSIONS_KEYS_TABLE . "";

    for( $i = 0; $i < count($sql); $i++ )
    {
       $echo .= '<b>Query ' . ($i+1) . ': </b><span style="font-size: 75%;">' . $sql[$i] . '</span><br /><b>Status: </b>' . (!($result = mysql_query($sql[$i])) ? '<font color="red">Error (' . mysql_error() . ')</font><hr />' : '<font color="green">Ran successfully</font><hr />');
    }

    die( ((strpos($echo, 'color="red">Error') !== FALSE) ? '<font color="red"><b>Some queries failed</b></font><br /><br />If the problem persists, please start a topic in the support forum with a link to this article and the exact error displayed.</b>' : '<font color="green"><b>All queries have been run successfully.</b></font><br /><br />') .  ' Be sure to delete this file! A detailed report follows:<br /><br /><table border="1"><tr><td>' . $echo . '</td></tr></table>');
    ?>
  2. Upload the file to your board's root folder (the one with config.php)
  3. Run it by navigating to the file in your browser (ex. http://www.yoursite.com/phpBB2/fix.php)
  4. If any errors come up, post the report here.
  5. Be sure to delete the file; don't leave it on your server

Manual
    Use phpMyAdmin to run the following queries. Change the prefix ('phpbb_' by default) to the one set in your config.php file.

    Code: Select all
    # Added in 2.0.5
    CREATE TABLE `phpbb_confirm` (
    `confirm_id` char(32) NOT NULL default '',
    `session_id` char(32) NOT NULL default '',
    `code` char(6) NOT NULL default '',
    PRIMARY KEY (`session_id`,`confirm_id`)
    ) TYPE=MyISAM;

    # Added in 2.0.15
    ALTER TABLE phpbb_sessions ADD COLUMN session_admin tinyint(2) DEFAULT '0' NOT NULL;

    # Added in 2.0.18
    CREATE TABLE `phpbb_sessions_keys` (
    `key_id` varchar(32) NOT NULL default '0',
    `user_id` mediumint(8) NOT NULL default '0',
    `last_ip` varchar(8) NOT NULL default '0',
    `last_login` int(11) NOT NULL default '0',
    PRIMARY KEY (`key_id`,`user_id`),
    KEY `last_login` (`last_login`)
    ) TYPE=MyISAM;
    UPDATE phpbb_users SET user_active = 0 WHERE user_id = -1;
    INSERT INTO phpbb_config (config_name, config_value) VALUES ('allow_autologin','1');
    INSERT INTO phpbb_config (config_name, config_value) VALUES ('max_autologin_time','0');

    # Added in 2.0.19
    ALTER TABLE phpbb_users ADD COLUMN user_last_login_try int(11) DEFAULT '0' NOT NULL;
    ALTER TABLE phpbb_users ADD COLUMN user_login_tries smallint(5) unsigned NOT NULL default '0';
    INSERT INTO phpbb_config (config_name, config_value) VALUES ('max_login_attempts', '5');
    INSERT INTO phpbb_config (config_name, config_value) VALUES ('login_reset_time', '30');

    # Added in 2.0.20
    ALTER TABLE phpbb_search_results ADD COLUMN `search_time` int(11) NOT NULL default '0';
    INSERT INTO phpbb_config (config_name, config_value) VALUES ('search_flood_interval', '15');
    INSERT INTO phpbb_config (config_name, config_value) VALUES ('rand_seed', '0');
    UPDATE phpbb_config SET config_value = '.0.20' WHERE config_name = 'version';
    TRUNCATE TABLE phpbb_sessions;
    TRUNCATE TABLE phpbb_sessions_keys;

    # Added in 2.0.22
    ALTER TABLE phpbb_search MODIFY COLUMN search_array MEDIUMTEXT NOT NULL;