\n";
+ */
+ $field_def[$table][$field] = array(
+ 'type' => $type,
+ 'size' => $size,
+ 'default' => $default,
+ 'notnull' => $notnull,
+ 'auto_increment' => $auto_increment
);
+ }
+
+ if(preg_match("/\s*PRIMARY\s+KEY\s*\((.*)\).*/", $line, $matches))
+ {
+ // Primary key
+ $key_def[$table]['PRIMARY'] = $matches[1];
+ }
+ else if(preg_match("/\s*KEY\s+(\w+)\s*\((.*)\)/", $line, $matches))
+ {
+ // Normal key
+ $key_def[$table][$matches[1]] = $matches[2];
+ }
+ else if(preg_match("/^\s*(\w+)\s*(.*?),?\s*$/", $line, $matches))
+ {
+ // Column definition
+ $create_def[$table][$matches[1]] = $matches[2];
+ }
+ else
+ {
+ print "$line
";
+ // It's a bird! It's a plane! It's something we didn't expect ;(
+ }
}
}
/*
@@ -158,15 +190,18 @@ function get_schema()
*/
$schema['field_def'] = $field_def;
$schema['table_def'] = $table_def;
+ $schema['create_def'] = $create_def;
+ $schema['key_def'] = $key_def;
return $schema;
}
function get_inserts()
{
- $insertfile = file("db/mysql_basic.sql");
+ global $table_prefix;
+ $insertfile = file("db/schemas/mysql_basic.sql");
for($i=0; $i < count($insertfile); $i++)
{
- if (preg_match("/^(.*INSERT INTO (.*?)\s.*);$/i", $insertfile[$i], $matches))
+ if (preg_match("/^(.*INSERT INTO (.*?)\s.*);$/i", str_replace("phpbb_", $table_prefix, $insertfile[$i]), $matches))
{
$returnvalue[$matches[2]][] = $matches[1];
}
@@ -296,6 +331,21 @@ if(!isset($next)) $next = 'start';
// If debug is set we'll do all steps in one go.
$debug=1;
+// Parse the MySQL schema file into some arrays.
+$schema = get_schema();
+$table_def = $schema['table_def'];
+$field_def = $schema['field_def'];
+$key_def = $schema['key_def'];
+$create_def = $schema['create_def'];
+
+/*
+print "tables:
";
+print_r($table_def);
+print "create:
";
+print_r($create_def);
+die;
+*/
+
if(isset($next))
{
switch($next)
@@ -305,43 +355,48 @@ if(isset($next))
case 'cleanstart':
$sql = "DROP TABLE sessions";
query($sql, "Couldn't drop table 'sessions'");
- end_step('rename_tables');
+ $sql = "DROP TABLE themes";
+ query($sql, "Couldn't drop table 'themes'");
+ end_step('mod_old_tables');
- case 'rename_tables':
+ case 'mod_old_tables':
common_header();
echo "Step 2: Rename tables
\n";
- $newnames = array(
+ $modtables = array(
"banlist" => "banlist",
"catagories" => "categories",
- // Don't rename config yet, we'll create a new one and merge those later.
- //"config" => "config",
+ "config" => "old_config",
"forums" => "forums",
"disallow" => "disallow",
"posts" => "posts",
"posts_text" => "posts_text",
"priv_msgs" => "privmsgs",
"ranks" => "ranks",
- "sessions" => "sessions",
"smiles" => "smilies",
"topics" => "topics",
"users" => "users",
"words" => "words"
- );
- while(list($old, $new) = each($newnames))
+ );
+ while(list($old, $new) = each($modtables))
{
+ $sql = "SHOW INDEX FROM $old";
+ $result = query($sql, "Couldn't get list of indices for table $old");
+ while($row = mysql_fetch_array($result))
+ {
+ $index = $row['Key_name'];
+ if($index != 'PRIMARY')
+ {
+ query("ALTER TABLE $old DROP INDEX $index", "Couldn't DROP INDEX $old.$index");
+ }
+ }
+
+ // Rename table
$new = $table_prefix . $new;
$sql = "ALTER TABLE $old RENAME $new";
- if(!$result = $db->sql_query($sql))
- {
- echo "Couldn't rename '$old' to '$new'
\n";
- $sql_error = $db->sql_error();
- print $sql_error['code'] .": ". $sql_error['message']. "
\n";
- }
- else
- {
- print "Renamed '$old' to '$new'
\n";
- }
+ print "Renaming '$old' to '$new'
\n";
+ query($sql, "Failed to rename $old to $new");
+
}
common_footer();
end_step('create_tables');
@@ -350,18 +405,9 @@ if(isset($next))
common_header();
echo "Step 2: Create new phpBB2 tables
\n";
- $schema = get_schema();
- $table_def = $schema['table_def'];
- $field_def = $schema['field_def'];
-
// Create array with tables in 'old' database
$sql = 'SHOW TABLES';
- if(!$result = $db->sql_query($sql))
- {
- echo "Couldn't get list of current tables
\n";
- $sql_error = $db->sql_error();
- print $sql_error['code'] .": ". $sql_error['message']. "
\n";
- }
+ $result = query($sql, "Couldn't get list of current tables");
while ($table = $db->sql_fetchrow($result))
{
$currenttables[] = $table[0];
@@ -370,14 +416,14 @@ if(isset($next))
// Check what tables we need to CREATE
while (list($table, $definition) = each ($table_def))
{
+ print "Table: $table
";
if (!inarray($table, $currenttables))
{
print "Creating $table: ";
- if(!$result = $db->sql_query($definition))
+ $result = query($definition, "Couldn't create table $table");
+ if($db->sql_affectedrows($result) < 1)
{
- echo "Couldn't create table
\n";
- $sql_error = $db->sql_error();
- print $sql_error['code'] .": ". $sql_error['message']. "
\n";
+ echo "Couldn't create table (no affected rows)
\n";
print $definition . "
\n";
}
else
@@ -406,12 +452,13 @@ if(isset($next))
print "
";
}
}
+ print "New config table has been created with default values.\n";
//end_step('convert_config');
case 'convert_config':
common_header();
print "Starting!
";
- $sql = "SELECT * FROM config";
+ $sql = "SELECT * FROM $table_prefix"."old_config";
$result = query($sql, "Couldn't get info from old config table");
$oldconfig = $db->sql_fetchrow($result);
while(list($name, $value) = each($oldconfig))
@@ -520,6 +567,8 @@ if(isset($next))
flush();
$sql = "SELECT * from ". USERS_TABLE. " WHERE user_id BETWEEN $batchstart AND $batchend";
$result = query($sql, "Couldn't get ". USERS_TABLE .".user_id $batchstart to $batchend");
+
+ // Array with user fields that we want to check for invalid data (to few characters)
$checklength = array(
'user_occ',
'user_website',
@@ -912,16 +961,35 @@ if(isset($next))
{
// If the current is not a key of $current_def and it is not a field that is
// to be renamed then the field doesn't currently exist.
- $changes[] = "\nADD $field $type($size) $default $notnull $auto_increment ";
+ //$changes[] = "\nADD $field $type($size) $default $notnull $auto_increment ";
+ $changes[] = "\nADD $field ". $create_def[$table][$field];
}
else
{
- $changes[] = "\nCHANGE $oldfield $field $type($size) $default $notnull $auto_increment";
+ //$changes[] = "\nCHANGE $oldfield $field $type($size) $default $notnull $auto_increment";
+ $changes[] = "\nCHANGE $oldfield $field ". $create_def[$table][$field];
}
}
+
$alter_sql .= join(',', $changes);
unset($changes);
unset($current_fields);
+
+ $sql = "SHOW INDEX FROM $table";
+ $result = query($sql, "Couldn't get list of indices for table $table");
+ unset($indices);
+ while($row = mysql_fetch_array($result))
+ {
+ $indices[] = $row['Key_name'];
+ }
+
+ while (list($key_name, $key_field) = each($key_def[$table]) )
+ {
+ if(!inarray($key_name, $indices))
+ {
+ $alter_sql .= ($key_name == 'PRIMARY') ? ",\nADD PRIMARY KEY ($key_field)" : ",\nADD INDEX $key_name ($key_field)";
+ }
+ }
print "$alter_sql
\n";
query($alter_sql, "Couldn't alter table $table");
flush();