mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-22 11:18:52 +00:00
Merge pull request #2172 from nickvergessen/ticket/11459
[Ticket/11459] Deduplicate database schema definiton
This commit is contained in:
commit
f1f162d741
19 changed files with 4798 additions and 11555 deletions
|
@ -12,6 +12,15 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$schema_path = dirname(__FILE__) . '/../install/schemas/';
|
$schema_path = dirname(__FILE__) . '/../install/schemas/';
|
||||||
|
$supported_dbms = array(
|
||||||
|
'firebird',
|
||||||
|
'mssql',
|
||||||
|
'mysql_40',
|
||||||
|
'mysql_41',
|
||||||
|
'oracle',
|
||||||
|
'postgres',
|
||||||
|
'sqlite',
|
||||||
|
);
|
||||||
|
|
||||||
if (!is_writable($schema_path))
|
if (!is_writable($schema_path))
|
||||||
{
|
{
|
||||||
|
@ -19,34 +28,30 @@ if (!is_writable($schema_path))
|
||||||
}
|
}
|
||||||
|
|
||||||
define('IN_PHPBB', true);
|
define('IN_PHPBB', true);
|
||||||
|
define('IN_INSTALL', true);
|
||||||
|
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
|
||||||
|
$phpEx = substr(strrchr(__FILE__, '.'), 1);
|
||||||
|
include($phpbb_root_path . 'common.' . $phpEx);
|
||||||
|
|
||||||
require(dirname(__FILE__) . '/../phpbb/db/tools.php');
|
$classes = $phpbb_container->get('ext.manager')
|
||||||
|
->get_finder()
|
||||||
|
->core_path('phpbb/')
|
||||||
|
->directory('db/migration/data')
|
||||||
|
->get_classes();
|
||||||
|
$db_tools = new \phpbb\db\tools($db, true);
|
||||||
|
|
||||||
|
$schema_generator = new \phpbb\db\migration\schema_generator($classes, $config, $db, $db_tools, $phpbb_root_path, $phpEx, 'phpbb_');
|
||||||
|
$schema_data = $schema_generator->get_schema();
|
||||||
$dbms_type_map = phpbb\db\tools::get_dbms_type_map();
|
$dbms_type_map = phpbb\db\tools::get_dbms_type_map();
|
||||||
|
|
||||||
// A list of types being unsigned for better reference in some db's
|
$fp = fopen($schema_path . 'schema.json', 'wb');
|
||||||
$unsigned_types = array('UINT', 'UINT:', 'USINT', 'BOOL', 'TIMESTAMP');
|
fwrite($fp, json_encode($schema_data, JSON_PRETTY_PRINT));
|
||||||
$supported_dbms = array('firebird', 'mssql', 'mysql_40', 'mysql_41', 'oracle', 'postgres', 'sqlite');
|
fclose($fp);
|
||||||
|
|
||||||
foreach ($supported_dbms as $dbms)
|
foreach ($supported_dbms as $dbms)
|
||||||
{
|
{
|
||||||
include(dirname(__FILE__) . '/../includes/db/schema_data.php');
|
|
||||||
if ($dbms == 'mssql')
|
|
||||||
{
|
|
||||||
foreach ($schema_data as $table_name => $table_data)
|
|
||||||
{
|
|
||||||
if (!isset($table_data['PRIMARY_KEY']))
|
|
||||||
{
|
|
||||||
$schema_data[$table_name]['COLUMNS']['mssqlindex'] = array('UINT', NULL, 'auto_increment');
|
|
||||||
$schema_data[$table_name]['PRIMARY_KEY'] = 'mssqlindex';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$fp = fopen($schema_path . $dbms . '_schema.sql', 'wb');
|
$fp = fopen($schema_path . $dbms . '_schema.sql', 'wb');
|
||||||
|
|
||||||
$line = '';
|
|
||||||
|
|
||||||
// Write Header
|
// Write Header
|
||||||
switch ($dbms)
|
switch ($dbms)
|
||||||
{
|
{
|
||||||
|
@ -74,16 +79,9 @@ foreach ($supported_dbms as $dbms)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$line = '';
|
||||||
switch ($dbms)
|
switch ($dbms)
|
||||||
{
|
{
|
||||||
case 'firebird':
|
|
||||||
$line .= custom_data('firebird') . "\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'sqlite':
|
|
||||||
$line .= "BEGIN TRANSACTION;\n\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'oracle':
|
case 'oracle':
|
||||||
$line .= custom_data('oracle') . "\n";
|
$line .= custom_data('oracle') . "\n";
|
||||||
break;
|
break;
|
||||||
|
@ -91,501 +89,7 @@ foreach ($supported_dbms as $dbms)
|
||||||
case 'postgres':
|
case 'postgres':
|
||||||
$line .= "BEGIN;\n\n";
|
$line .= "BEGIN;\n\n";
|
||||||
$line .= custom_data('postgres') . "\n";
|
$line .= custom_data('postgres') . "\n";
|
||||||
break;
|
$line .= "COMMIT;\n\n";
|
||||||
}
|
|
||||||
|
|
||||||
fwrite($fp, $line);
|
|
||||||
|
|
||||||
foreach ($schema_data as $table_name => $table_data)
|
|
||||||
{
|
|
||||||
// Write comment about table
|
|
||||||
switch ($dbms)
|
|
||||||
{
|
|
||||||
case 'mysql_40':
|
|
||||||
case 'mysql_41':
|
|
||||||
case 'firebird':
|
|
||||||
case 'sqlite':
|
|
||||||
fwrite($fp, "# Table: '{$table_name}'\n");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'mssql':
|
|
||||||
case 'oracle':
|
|
||||||
case 'postgres':
|
|
||||||
fwrite($fp, "/*\n\tTable: '{$table_name}'\n*/\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create Table statement
|
|
||||||
$generator = $textimage = false;
|
|
||||||
$line = '';
|
|
||||||
|
|
||||||
switch ($dbms)
|
|
||||||
{
|
|
||||||
case 'mysql_40':
|
|
||||||
case 'mysql_41':
|
|
||||||
case 'firebird':
|
|
||||||
case 'oracle':
|
|
||||||
case 'sqlite':
|
|
||||||
case 'postgres':
|
|
||||||
$line = "CREATE TABLE {$table_name} (\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'mssql':
|
|
||||||
$line = "CREATE TABLE [{$table_name}] (\n";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Table specific so we don't get overlap
|
|
||||||
$modded_array = array();
|
|
||||||
|
|
||||||
// Write columns one by one...
|
|
||||||
foreach ($table_data['COLUMNS'] as $column_name => $column_data)
|
|
||||||
{
|
|
||||||
if (strlen($column_name) > 30)
|
|
||||||
{
|
|
||||||
trigger_error("Column name '$column_name' on table '$table_name' is too long. The maximum is 30 characters.", E_USER_ERROR);
|
|
||||||
}
|
|
||||||
if (isset($column_data[2]) && $column_data[2] == 'auto_increment' && strlen($column_name) > 26) // "${column_name}_gen"
|
|
||||||
{
|
|
||||||
trigger_error("Index name '${column_name}_gen' on table '$table_name' is too long. The maximum is 30 characters.", E_USER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get type
|
|
||||||
if (strpos($column_data[0], ':') !== false)
|
|
||||||
{
|
|
||||||
list($orig_column_type, $column_length) = explode(':', $column_data[0]);
|
|
||||||
if (!is_array($dbms_type_map[$dbms][$orig_column_type . ':']))
|
|
||||||
{
|
|
||||||
$column_type = sprintf($dbms_type_map[$dbms][$orig_column_type . ':'], $column_length);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (isset($dbms_type_map[$dbms][$orig_column_type . ':']['rule']))
|
|
||||||
{
|
|
||||||
switch ($dbms_type_map[$dbms][$orig_column_type . ':']['rule'][0])
|
|
||||||
{
|
|
||||||
case 'div':
|
|
||||||
$column_length /= $dbms_type_map[$dbms][$orig_column_type . ':']['rule'][1];
|
|
||||||
$column_length = ceil($column_length);
|
|
||||||
$column_type = sprintf($dbms_type_map[$dbms][$orig_column_type . ':'][0], $column_length);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($dbms_type_map[$dbms][$orig_column_type . ':']['limit']))
|
|
||||||
{
|
|
||||||
switch ($dbms_type_map[$dbms][$orig_column_type . ':']['limit'][0])
|
|
||||||
{
|
|
||||||
case 'mult':
|
|
||||||
$column_length *= $dbms_type_map[$dbms][$orig_column_type . ':']['limit'][1];
|
|
||||||
if ($column_length > $dbms_type_map[$dbms][$orig_column_type . ':']['limit'][2])
|
|
||||||
{
|
|
||||||
$column_type = $dbms_type_map[$dbms][$orig_column_type . ':']['limit'][3];
|
|
||||||
$modded_array[$column_name] = $column_type;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$column_type = sprintf($dbms_type_map[$dbms][$orig_column_type . ':'][0], $column_length);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$orig_column_type .= ':';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$orig_column_type = $column_data[0];
|
|
||||||
$column_type = $dbms_type_map[$dbms][$column_data[0]];
|
|
||||||
if ($column_type == 'text' || $column_type == 'blob')
|
|
||||||
{
|
|
||||||
$modded_array[$column_name] = $column_type;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adjust default value if db-dependent specified
|
|
||||||
if (is_array($column_data[1]))
|
|
||||||
{
|
|
||||||
$column_data[1] = (isset($column_data[1][$dbms])) ? $column_data[1][$dbms] : $column_data[1]['default'];
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($dbms)
|
|
||||||
{
|
|
||||||
case 'mysql_40':
|
|
||||||
case 'mysql_41':
|
|
||||||
$line .= "\t{$column_name} {$column_type} ";
|
|
||||||
|
|
||||||
// For hexadecimal values do not use single quotes
|
|
||||||
if (!is_null($column_data[1]) && substr($column_type, -4) !== 'text' && substr($column_type, -4) !== 'blob')
|
|
||||||
{
|
|
||||||
$line .= (strpos($column_data[1], '0x') === 0) ? "DEFAULT {$column_data[1]} " : "DEFAULT '{$column_data[1]}' ";
|
|
||||||
}
|
|
||||||
$line .= 'NOT NULL';
|
|
||||||
|
|
||||||
if (isset($column_data[2]))
|
|
||||||
{
|
|
||||||
if ($column_data[2] == 'auto_increment')
|
|
||||||
{
|
|
||||||
$line .= ' auto_increment';
|
|
||||||
}
|
|
||||||
else if ($dbms === 'mysql_41' && $column_data[2] == 'true_sort')
|
|
||||||
{
|
|
||||||
$line .= ' COLLATE utf8_unicode_ci';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$line .= ",\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'sqlite':
|
|
||||||
if (isset($column_data[2]) && $column_data[2] == 'auto_increment')
|
|
||||||
{
|
|
||||||
$line .= "\t{$column_name} INTEGER PRIMARY KEY ";
|
|
||||||
$generator = $column_name;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$line .= "\t{$column_name} {$column_type} ";
|
|
||||||
}
|
|
||||||
|
|
||||||
$line .= 'NOT NULL ';
|
|
||||||
$line .= (!is_null($column_data[1])) ? "DEFAULT '{$column_data[1]}'" : '';
|
|
||||||
$line .= ",\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'firebird':
|
|
||||||
$line .= "\t{$column_name} {$column_type} ";
|
|
||||||
|
|
||||||
if (!is_null($column_data[1]))
|
|
||||||
{
|
|
||||||
$line .= 'DEFAULT ' . ((is_numeric($column_data[1])) ? $column_data[1] : "'{$column_data[1]}'") . ' ';
|
|
||||||
}
|
|
||||||
|
|
||||||
$line .= 'NOT NULL';
|
|
||||||
|
|
||||||
// This is a UNICODE column and thus should be given it's fair share
|
|
||||||
if (preg_match('/^X?STEXT_UNI|VCHAR_(CI|UNI:?)/', $column_data[0]))
|
|
||||||
{
|
|
||||||
$line .= ' COLLATE UNICODE';
|
|
||||||
}
|
|
||||||
|
|
||||||
$line .= ",\n";
|
|
||||||
|
|
||||||
if (isset($column_data[2]) && $column_data[2] == 'auto_increment')
|
|
||||||
{
|
|
||||||
$generator = $column_name;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'mssql':
|
|
||||||
if ($column_type == '[text]')
|
|
||||||
{
|
|
||||||
$textimage = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$line .= "\t[{$column_name}] {$column_type} ";
|
|
||||||
|
|
||||||
if (!is_null($column_data[1]))
|
|
||||||
{
|
|
||||||
// For hexadecimal values do not use single quotes
|
|
||||||
if (strpos($column_data[1], '0x') === 0)
|
|
||||||
{
|
|
||||||
$line .= 'DEFAULT (' . $column_data[1] . ') ';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$line .= 'DEFAULT (' . ((is_numeric($column_data[1])) ? $column_data[1] : "'{$column_data[1]}'") . ') ';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($column_data[2]) && $column_data[2] == 'auto_increment')
|
|
||||||
{
|
|
||||||
$line .= 'IDENTITY (1, 1) ';
|
|
||||||
}
|
|
||||||
|
|
||||||
$line .= 'NOT NULL';
|
|
||||||
$line .= " ,\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'oracle':
|
|
||||||
$line .= "\t{$column_name} {$column_type} ";
|
|
||||||
$line .= (!is_null($column_data[1])) ? "DEFAULT '{$column_data[1]}' " : '';
|
|
||||||
|
|
||||||
// In Oracle empty strings ('') are treated as NULL.
|
|
||||||
// Therefore in oracle we allow NULL's for all DEFAULT '' entries
|
|
||||||
$line .= ($column_data[1] === '') ? ",\n" : "NOT NULL,\n";
|
|
||||||
|
|
||||||
if (isset($column_data[2]) && $column_data[2] == 'auto_increment')
|
|
||||||
{
|
|
||||||
$generator = $column_name;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'postgres':
|
|
||||||
$line .= "\t{$column_name} {$column_type} ";
|
|
||||||
|
|
||||||
if (isset($column_data[2]) && $column_data[2] == 'auto_increment')
|
|
||||||
{
|
|
||||||
$line .= "DEFAULT nextval('{$table_name}_seq'),\n";
|
|
||||||
|
|
||||||
// Make sure the sequence will be created before creating the table
|
|
||||||
$line = "CREATE SEQUENCE {$table_name}_seq;\n\n" . $line;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$line .= (!is_null($column_data[1])) ? "DEFAULT '{$column_data[1]}' " : '';
|
|
||||||
$line .= "NOT NULL";
|
|
||||||
|
|
||||||
// Unsigned? Then add a CHECK contraint
|
|
||||||
if (in_array($orig_column_type, $unsigned_types))
|
|
||||||
{
|
|
||||||
$line .= " CHECK ({$column_name} >= 0)";
|
|
||||||
}
|
|
||||||
|
|
||||||
$line .= ",\n";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($dbms)
|
|
||||||
{
|
|
||||||
case 'firebird':
|
|
||||||
// Remove last line delimiter...
|
|
||||||
$line = substr($line, 0, -2);
|
|
||||||
$line .= "\n);;\n\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'mssql':
|
|
||||||
$line = substr($line, 0, -2);
|
|
||||||
$line .= "\n)";// ON [PRIMARY]" . (($textimage) ? ' TEXTIMAGE_ON [PRIMARY]' : '') . "\n";
|
|
||||||
$line .= "GO\n\n";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write primary key
|
|
||||||
if (isset($table_data['PRIMARY_KEY']))
|
|
||||||
{
|
|
||||||
if (!is_array($table_data['PRIMARY_KEY']))
|
|
||||||
{
|
|
||||||
$table_data['PRIMARY_KEY'] = array($table_data['PRIMARY_KEY']);
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($dbms)
|
|
||||||
{
|
|
||||||
case 'mysql_40':
|
|
||||||
case 'mysql_41':
|
|
||||||
case 'postgres':
|
|
||||||
$line .= "\tPRIMARY KEY (" . implode(', ', $table_data['PRIMARY_KEY']) . "),\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'firebird':
|
|
||||||
$line .= "ALTER TABLE {$table_name} ADD PRIMARY KEY (" . implode(', ', $table_data['PRIMARY_KEY']) . ");;\n\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'sqlite':
|
|
||||||
if ($generator === false || !in_array($generator, $table_data['PRIMARY_KEY']))
|
|
||||||
{
|
|
||||||
$line .= "\tPRIMARY KEY (" . implode(', ', $table_data['PRIMARY_KEY']) . "),\n";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'mssql':
|
|
||||||
$line .= "ALTER TABLE [{$table_name}] WITH NOCHECK ADD \n";
|
|
||||||
$line .= "\tCONSTRAINT [PK_{$table_name}] PRIMARY KEY CLUSTERED \n";
|
|
||||||
$line .= "\t(\n";
|
|
||||||
$line .= "\t\t[" . implode("],\n\t\t[", $table_data['PRIMARY_KEY']) . "]\n";
|
|
||||||
$line .= "\t)\n";
|
|
||||||
$line .= "GO\n\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'oracle':
|
|
||||||
$line .= "\tCONSTRAINT pk_{$table_name} PRIMARY KEY (" . implode(', ', $table_data['PRIMARY_KEY']) . "),\n";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($dbms)
|
|
||||||
{
|
|
||||||
case 'oracle':
|
|
||||||
// UNIQUE contrains to be added?
|
|
||||||
if (isset($table_data['KEYS']))
|
|
||||||
{
|
|
||||||
foreach ($table_data['KEYS'] as $key_name => $key_data)
|
|
||||||
{
|
|
||||||
if (!is_array($key_data[1]))
|
|
||||||
{
|
|
||||||
$key_data[1] = array($key_data[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($key_data[0] == 'UNIQUE')
|
|
||||||
{
|
|
||||||
$line .= "\tCONSTRAINT u_phpbb_{$key_name} UNIQUE (" . implode(', ', $key_data[1]) . "),\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove last line delimiter...
|
|
||||||
$line = substr($line, 0, -2);
|
|
||||||
$line .= "\n)\n/\n\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'postgres':
|
|
||||||
// Remove last line delimiter...
|
|
||||||
$line = substr($line, 0, -2);
|
|
||||||
$line .= "\n);\n\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'sqlite':
|
|
||||||
// Remove last line delimiter...
|
|
||||||
$line = substr($line, 0, -2);
|
|
||||||
$line .= "\n);\n\n";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write Keys
|
|
||||||
if (isset($table_data['KEYS']))
|
|
||||||
{
|
|
||||||
foreach ($table_data['KEYS'] as $key_name => $key_data)
|
|
||||||
{
|
|
||||||
if (!is_array($key_data[1]))
|
|
||||||
{
|
|
||||||
$key_data[1] = array($key_data[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strlen($table_name . $key_name) > 30)
|
|
||||||
{
|
|
||||||
trigger_error("Index name '${table_name}_$key_name' on table '$table_name' is too long. The maximum is 30 characters.", E_USER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($dbms)
|
|
||||||
{
|
|
||||||
case 'mysql_40':
|
|
||||||
case 'mysql_41':
|
|
||||||
$line .= ($key_data[0] == 'INDEX') ? "\tKEY" : '';
|
|
||||||
$line .= ($key_data[0] == 'UNIQUE') ? "\tUNIQUE" : '';
|
|
||||||
foreach ($key_data[1] as $key => $col_name)
|
|
||||||
{
|
|
||||||
if (isset($modded_array[$col_name]))
|
|
||||||
{
|
|
||||||
switch ($modded_array[$col_name])
|
|
||||||
{
|
|
||||||
case 'text':
|
|
||||||
case 'blob':
|
|
||||||
$key_data[1][$key] = $col_name . '(255)';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$line .= ' ' . $key_name . ' (' . implode(', ', $key_data[1]) . "),\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'firebird':
|
|
||||||
$line .= ($key_data[0] == 'INDEX') ? 'CREATE INDEX' : '';
|
|
||||||
$line .= ($key_data[0] == 'UNIQUE') ? 'CREATE UNIQUE INDEX' : '';
|
|
||||||
|
|
||||||
$line .= ' ' . $table_name . '_' . $key_name . ' ON ' . $table_name . '(' . implode(', ', $key_data[1]) . ");;\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'mssql':
|
|
||||||
$line .= ($key_data[0] == 'INDEX') ? 'CREATE INDEX' : '';
|
|
||||||
$line .= ($key_data[0] == 'UNIQUE') ? 'CREATE UNIQUE INDEX' : '';
|
|
||||||
$line .= " [{$key_name}] ON [{$table_name}]([" . implode('], [', $key_data[1]) . "])\n";
|
|
||||||
$line .= "GO\n\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'oracle':
|
|
||||||
if ($key_data[0] == 'UNIQUE')
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$line .= ($key_data[0] == 'INDEX') ? 'CREATE INDEX' : '';
|
|
||||||
|
|
||||||
$line .= " {$table_name}_{$key_name} ON {$table_name} (" . implode(', ', $key_data[1]) . ")\n";
|
|
||||||
$line .= "/\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'sqlite':
|
|
||||||
$line .= ($key_data[0] == 'INDEX') ? 'CREATE INDEX' : '';
|
|
||||||
$line .= ($key_data[0] == 'UNIQUE') ? 'CREATE UNIQUE INDEX' : '';
|
|
||||||
|
|
||||||
$line .= " {$table_name}_{$key_name} ON {$table_name} (" . implode(', ', $key_data[1]) . ");\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'postgres':
|
|
||||||
$line .= ($key_data[0] == 'INDEX') ? 'CREATE INDEX' : '';
|
|
||||||
$line .= ($key_data[0] == 'UNIQUE') ? 'CREATE UNIQUE INDEX' : '';
|
|
||||||
|
|
||||||
$line .= " {$table_name}_{$key_name} ON {$table_name} (" . implode(', ', $key_data[1]) . ");\n";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($dbms)
|
|
||||||
{
|
|
||||||
case 'mysql_40':
|
|
||||||
// Remove last line delimiter...
|
|
||||||
$line = substr($line, 0, -2);
|
|
||||||
$line .= "\n);\n\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'mysql_41':
|
|
||||||
// Remove last line delimiter...
|
|
||||||
$line = substr($line, 0, -2);
|
|
||||||
$line .= "\n) CHARACTER SET `utf8` COLLATE `utf8_bin`;\n\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Create Generator
|
|
||||||
case 'firebird':
|
|
||||||
if ($generator !== false)
|
|
||||||
{
|
|
||||||
$line .= "\nCREATE GENERATOR {$table_name}_gen;;\n";
|
|
||||||
$line .= 'SET GENERATOR ' . $table_name . "_gen TO 0;;\n\n";
|
|
||||||
|
|
||||||
$line .= 'CREATE TRIGGER t_' . $table_name . ' FOR ' . $table_name . "\n";
|
|
||||||
$line .= "BEFORE INSERT\nAS\nBEGIN\n";
|
|
||||||
$line .= "\tNEW.{$generator} = GEN_ID({$table_name}_gen, 1);\nEND;;\n\n";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'oracle':
|
|
||||||
if ($generator !== false)
|
|
||||||
{
|
|
||||||
$line .= "\nCREATE SEQUENCE {$table_name}_seq\n/\n\n";
|
|
||||||
|
|
||||||
$line .= "CREATE OR REPLACE TRIGGER t_{$table_name}\n";
|
|
||||||
$line .= "BEFORE INSERT ON {$table_name}\n";
|
|
||||||
$line .= "FOR EACH ROW WHEN (\n";
|
|
||||||
$line .= "\tnew.{$generator} IS NULL OR new.{$generator} = 0\n";
|
|
||||||
$line .= ")\nBEGIN\n";
|
|
||||||
$line .= "\tSELECT {$table_name}_seq.nextval\n";
|
|
||||||
$line .= "\tINTO :new.{$generator}\n";
|
|
||||||
$line .= "\tFROM dual;\nEND;\n/\n\n";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
fwrite($fp, $line . "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
$line = '';
|
|
||||||
|
|
||||||
// Write custom function at the end for some db's
|
|
||||||
switch ($dbms)
|
|
||||||
{
|
|
||||||
case 'mssql':
|
|
||||||
// No need to do this, no transaction support for schema changes
|
|
||||||
//$line = "\nCOMMIT\nGO\n\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'sqlite':
|
|
||||||
$line = "\nCOMMIT;";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'postgres':
|
|
||||||
$line = "\nCOMMIT;";
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title></title>
|
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body bgcolor="#FFFFFF" text="#000000">
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
File diff suppressed because it is too large
Load diff
|
@ -1151,13 +1151,9 @@ class install_install extends module
|
||||||
|
|
||||||
// How should we treat this schema?
|
// How should we treat this schema?
|
||||||
$delimiter = $available_dbms[$data['dbms']]['DELIM'];
|
$delimiter = $available_dbms[$data['dbms']]['DELIM'];
|
||||||
|
|
||||||
$sql_query = @file_get_contents($dbms_schema);
|
$sql_query = @file_get_contents($dbms_schema);
|
||||||
|
|
||||||
$sql_query = preg_replace('#phpbb_#i', $data['table_prefix'], $sql_query);
|
$sql_query = preg_replace('#phpbb_#i', $data['table_prefix'], $sql_query);
|
||||||
|
|
||||||
$sql_query = phpbb_remove_comments($sql_query);
|
$sql_query = phpbb_remove_comments($sql_query);
|
||||||
|
|
||||||
$sql_query = split_sql_file($sql_query, $delimiter);
|
$sql_query = split_sql_file($sql_query, $delimiter);
|
||||||
|
|
||||||
foreach ($sql_query as $sql)
|
foreach ($sql_query as $sql)
|
||||||
|
@ -1171,6 +1167,27 @@ class install_install extends module
|
||||||
}
|
}
|
||||||
unset($sql_query);
|
unset($sql_query);
|
||||||
|
|
||||||
|
// Ok we have the db info go ahead and work on building the table
|
||||||
|
$db_table_schema = @file_get_contents('schemas/schema.json');
|
||||||
|
$db_table_schema = json_decode($db_table_schema, true);
|
||||||
|
|
||||||
|
if (!defined('CONFIG_TABLE'))
|
||||||
|
{
|
||||||
|
// CONFIG_TABLE is required by sql_create_index() to check the
|
||||||
|
// length of index names. However table_prefix is not defined
|
||||||
|
// here yet, so we need to create the constant ourselves.
|
||||||
|
define('CONFIG_TABLE', $data['table_prefix'] . 'config');
|
||||||
|
}
|
||||||
|
|
||||||
|
$db_tools = new \phpbb\db\tools($db);
|
||||||
|
foreach ($db_table_schema as $table_name => $table_data)
|
||||||
|
{
|
||||||
|
$db_tools->sql_create_table(
|
||||||
|
$data['table_prefix'] . substr($table_name, 6),
|
||||||
|
$table_data
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Ok tables have been built, let's fill in the basic information
|
// Ok tables have been built, let's fill in the basic information
|
||||||
$sql_query = file_get_contents('schemas/schema_data.sql');
|
$sql_query = file_get_contents('schemas/schema_data.sql');
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
3324
phpBB/install/schemas/schema.json
Normal file
3324
phpBB/install/schemas/schema.json
Normal file
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
1177
phpBB/phpbb/db/migration/data/v30x/release_3_0_0.php
Normal file
1177
phpBB/phpbb/db/migration/data/v30x/release_3_0_0.php
Normal file
File diff suppressed because it is too large
Load diff
|
@ -23,6 +23,7 @@ class dev extends \phpbb\db\migration\migration
|
||||||
'\phpbb\db\migration\data\v310\style_update_p2',
|
'\phpbb\db\migration\data\v310\style_update_p2',
|
||||||
'\phpbb\db\migration\data\v310\timezone_p2',
|
'\phpbb\db\migration\data\v310\timezone_p2',
|
||||||
'\phpbb\db\migration\data\v310\reported_posts_display',
|
'\phpbb\db\migration\data\v310\reported_posts_display',
|
||||||
|
'\phpbb\db\migration\data\v310\migrations_table',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
47
phpBB/phpbb/db/migration/data/v310/migrations_table.php
Normal file
47
phpBB/phpbb/db/migration/data/v310/migrations_table.php
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @package migration
|
||||||
|
* @copyright (c) 2014 phpBB Group
|
||||||
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace phpbb\db\migration\data\v310;
|
||||||
|
|
||||||
|
class migrations_table extends \phpbb\db\migration\migration
|
||||||
|
{
|
||||||
|
public function effectively_installed()
|
||||||
|
{
|
||||||
|
return $this->db_tools->sql_table_exists($this->table_prefix . 'migrations');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update_schema()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
'add_tables' => array(
|
||||||
|
$this->table_prefix . 'migrations' => array(
|
||||||
|
'COLUMNS' => array(
|
||||||
|
'migration_name' => array('VCHAR', ''),
|
||||||
|
'migration_depends_on' => array('TEXT', ''),
|
||||||
|
'migration_schema_done' => array('BOOL', 0),
|
||||||
|
'migration_data_done' => array('BOOL', 0),
|
||||||
|
'migration_data_state' => array('TEXT', ''),
|
||||||
|
'migration_start_time' => array('TIMESTAMP', 0),
|
||||||
|
'migration_end_time' => array('TIMESTAMP', 0),
|
||||||
|
),
|
||||||
|
'PRIMARY_KEY' => 'migration_name',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function revert_schema()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
'drop_tables' => array(
|
||||||
|
$this->table_prefix . 'migrations',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
179
phpBB/phpbb/db/migration/schema_generator.php
Normal file
179
phpBB/phpbb/db/migration/schema_generator.php
Normal file
|
@ -0,0 +1,179 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @package db
|
||||||
|
* @copyright (c) 2014 phpBB Group
|
||||||
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace phpbb\db\migration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The schema generator generates the schema based on the existing migrations
|
||||||
|
*
|
||||||
|
* @package db
|
||||||
|
*/
|
||||||
|
class schema_generator
|
||||||
|
{
|
||||||
|
/** @var \phpbb\config\config */
|
||||||
|
protected $config;
|
||||||
|
|
||||||
|
/** @var \phpbb\db\driver\driver */
|
||||||
|
protected $db;
|
||||||
|
|
||||||
|
/** @var \phpbb\db\tools */
|
||||||
|
protected $db_tools;
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
protected $class_names;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $table_prefix;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $phpbb_root_path;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $php_ext;
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
protected $tables;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public function __construct(array $class_names, \phpbb\config\config $config, \phpbb\db\driver\driver $db, \phpbb\db\tools $db_tools, $phpbb_root_path, $php_ext, $table_prefix)
|
||||||
|
{
|
||||||
|
$this->config = $config;
|
||||||
|
$this->db = $db;
|
||||||
|
$this->db_tools = $db_tools;
|
||||||
|
$this->class_names = $class_names;
|
||||||
|
$this->phpbb_root_path = $phpbb_root_path;
|
||||||
|
$this->php_ext = $php_ext;
|
||||||
|
$this->table_prefix = $table_prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads all migrations and their application state from the database.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function get_schema()
|
||||||
|
{
|
||||||
|
if (!empty($this->tables))
|
||||||
|
{
|
||||||
|
return $this->tables;
|
||||||
|
}
|
||||||
|
|
||||||
|
$migrations = $this->class_names;
|
||||||
|
|
||||||
|
$tree = array();
|
||||||
|
while (!empty($migrations))
|
||||||
|
{
|
||||||
|
foreach ($migrations as $migration_class)
|
||||||
|
{
|
||||||
|
$open_dependencies = array_diff($migration_class::depends_on(), $tree);
|
||||||
|
if (empty($open_dependencies))
|
||||||
|
{
|
||||||
|
$migration = new $migration_class($this->config, $this->db, $this->db_tools, $this->phpbb_root_path, $this->php_ext, $this->table_prefix);
|
||||||
|
$tree[] = $migration_class;
|
||||||
|
$migration_key = array_search($migration_class, $migrations);
|
||||||
|
|
||||||
|
foreach ($migration->update_schema() as $change_type => $data)
|
||||||
|
{
|
||||||
|
if ($change_type === 'add_tables')
|
||||||
|
{
|
||||||
|
foreach ($data as $table => $table_data)
|
||||||
|
{
|
||||||
|
$this->tables[$table] = $table_data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ($change_type === 'drop_tables')
|
||||||
|
{
|
||||||
|
foreach ($data as $table)
|
||||||
|
{
|
||||||
|
unset($this->tables[$table]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ($change_type === 'add_columns')
|
||||||
|
{
|
||||||
|
foreach ($data as $table => $add_columns)
|
||||||
|
{
|
||||||
|
foreach ($add_columns as $column => $column_data)
|
||||||
|
{
|
||||||
|
$this->tables[$table]['COLUMNS'][$column] = $column_data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ($change_type === 'change_columns')
|
||||||
|
{
|
||||||
|
foreach ($data as $table => $change_columns)
|
||||||
|
{
|
||||||
|
foreach ($change_columns as $column => $column_data)
|
||||||
|
{
|
||||||
|
$this->tables[$table]['COLUMNS'][$column] = $column_data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ($change_type === 'drop_columns')
|
||||||
|
{
|
||||||
|
foreach ($data as $table => $drop_columns)
|
||||||
|
{
|
||||||
|
if (is_array($drop_columns))
|
||||||
|
{
|
||||||
|
foreach ($drop_columns as $column)
|
||||||
|
{
|
||||||
|
unset($this->tables[$table]['COLUMNS'][$column]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
unset($this->tables[$table]['COLUMNS'][$drop_columns]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ($change_type === 'add_unique_index')
|
||||||
|
{
|
||||||
|
foreach ($data as $table => $add_index)
|
||||||
|
{
|
||||||
|
foreach ($add_index as $key => $index_data)
|
||||||
|
{
|
||||||
|
$this->tables[$table]['KEYS'][$key] = array('UNIQUE', $index_data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ($change_type === 'add_index')
|
||||||
|
{
|
||||||
|
foreach ($data as $table => $add_index)
|
||||||
|
{
|
||||||
|
foreach ($add_index as $key => $index_data)
|
||||||
|
{
|
||||||
|
$this->tables[$table]['KEYS'][$key] = array('INDEX', $index_data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ($change_type === 'drop_keys')
|
||||||
|
{
|
||||||
|
foreach ($data as $table => $drop_keys)
|
||||||
|
{
|
||||||
|
foreach ($drop_keys as $key)
|
||||||
|
{
|
||||||
|
unset($this->tables[$table]['KEYS'][$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var_dump($change_type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unset($migrations[$migration_key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ksort($this->tables);
|
||||||
|
return $this->tables;
|
||||||
|
}
|
||||||
|
}
|
|
@ -161,7 +161,7 @@ class phpbb_auth_provider_apache_test extends phpbb_database_test_case
|
||||||
'user_inactive_time' => '0',
|
'user_inactive_time' => '0',
|
||||||
'user_posts' => '0',
|
'user_posts' => '0',
|
||||||
'user_lang' => '',
|
'user_lang' => '',
|
||||||
'user_timezone' => 'UTC',
|
'user_timezone' => '',
|
||||||
'user_dateformat' => 'd M Y H:i',
|
'user_dateformat' => 'd M Y H:i',
|
||||||
'user_style' => '0',
|
'user_style' => '0',
|
||||||
'user_rank' => '0',
|
'user_rank' => '0',
|
||||||
|
|
|
@ -138,7 +138,7 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
|
||||||
|
|
||||||
if (!self::$already_connected)
|
if (!self::$already_connected)
|
||||||
{
|
{
|
||||||
$manager->load_schema();
|
$manager->load_schema($this->new_dbal());
|
||||||
self::$already_connected = true;
|
self::$already_connected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -169,12 +169,12 @@ class phpbb_database_test_connection_manager
|
||||||
/**
|
/**
|
||||||
* Load the phpBB database schema into the database
|
* Load the phpBB database schema into the database
|
||||||
*/
|
*/
|
||||||
public function load_schema()
|
public function load_schema($db)
|
||||||
{
|
{
|
||||||
$this->ensure_connected(__METHOD__);
|
$this->ensure_connected(__METHOD__);
|
||||||
|
|
||||||
$directory = dirname(__FILE__) . '/../../phpBB/install/schemas/';
|
$directory = dirname(__FILE__) . '/../../phpBB/install/schemas/';
|
||||||
$this->load_schema_from_file($directory);
|
$this->load_schema_from_file($directory, $db);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -321,7 +321,7 @@ class phpbb_database_test_connection_manager
|
||||||
* Compile the correct schema filename (as per create_schema_files) and
|
* Compile the correct schema filename (as per create_schema_files) and
|
||||||
* load it into the database.
|
* load it into the database.
|
||||||
*/
|
*/
|
||||||
protected function load_schema_from_file($directory)
|
protected function load_schema_from_file($directory, \phpbb\db\driver\driver $db)
|
||||||
{
|
{
|
||||||
$schema = $this->dbms['SCHEMA'];
|
$schema = $this->dbms['SCHEMA'];
|
||||||
|
|
||||||
|
@ -351,6 +351,23 @@ class phpbb_database_test_connection_manager
|
||||||
{
|
{
|
||||||
$this->pdo->exec($query);
|
$this->pdo->exec($query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ok we have the db info go ahead and work on building the table
|
||||||
|
$db_table_schema = file_get_contents($directory . 'schema.json');
|
||||||
|
$db_table_schema = json_decode($db_table_schema, true);
|
||||||
|
|
||||||
|
$db_tools = new \phpbb\db\tools($db, true);
|
||||||
|
foreach ($db_table_schema as $table_name => $table_data)
|
||||||
|
{
|
||||||
|
$queries = $db_tools->sql_create_table(
|
||||||
|
$table_name,
|
||||||
|
$table_data
|
||||||
|
);
|
||||||
|
foreach ($queries as $query)
|
||||||
|
{
|
||||||
|
$this->pdo->exec($query);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue