1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-03-22 16:40:21 +01:00

[ticket/14992] Check index name length in a more proper way

[ticket/14992] Add indexes to user_notifications table
This commit is contained in:
David Colón 2017-03-01 22:55:13 -05:00
parent e5eb702514
commit 59ac4a71b6
7 changed files with 181 additions and 17 deletions

View File

@ -13,7 +13,7 @@
namespace phpbb\db\migration\data\v32x;
class user_notifications_table_indexes extends \phpbb\db\migration\migration
class user_notifications_table_index_p1 extends \phpbb\db\migration\migration
{
static public function depends_on()
{
@ -28,8 +28,6 @@ class user_notifications_table_indexes extends \phpbb\db\migration\migration
'add_index' => array(
$this->table_prefix . 'user_notifications' => array(
'user_id' => array('user_id'),
'user_id_item_id' => array('user_id', 'item_id'),
'user_itm_type_id' => array('user_id', 'item_type', 'item_id'),
),
),
);
@ -41,8 +39,6 @@ class user_notifications_table_indexes extends \phpbb\db\migration\migration
'drop_keys' => array(
$this->table_prefix . 'user_notifications' => array(
'user_id',
'user_id_item_id',
'user_itm_type_id',
),
),
);

View File

@ -0,0 +1,46 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\migration\data\v32x;
class user_notifications_table_index_p2 extends \phpbb\db\migration\migration
{
static public function depends_on()
{
return array(
'\phpbb\db\migration\data\v32x\user_notifications_table_index_p1',
);
}
public function update_schema()
{
return array(
'add_index' => array(
$this->table_prefix . 'user_notifications' => array(
'usr_id_itm_id' => array('user_id', 'item_id'),
),
),
);
}
public function revert_schema()
{
return array(
'drop_keys' => array(
$this->table_prefix . 'user_notifications' => array(
'usr_id_itm_id',
),
),
);
}
}

View File

@ -0,0 +1,46 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\migration\data\v32x;
class user_notifications_table_index_p3 extends \phpbb\db\migration\migration
{
static public function depends_on()
{
return array(
'\phpbb\db\migration\data\v32x\user_notifications_table_index_p2',
);
}
public function update_schema()
{
return array(
'add_index' => array(
$this->table_prefix . 'user_notifications' => array(
'usr_itm_tpe' => array('user_id', 'item_type', 'item_id'),
),
),
);
}
public function revert_schema()
{
return array(
'drop_keys' => array(
$this->table_prefix . 'user_notifications' => array(
'usr_itm_tpe',
),
),
);
}
}

View File

@ -18,7 +18,7 @@ class user_notifications_table_remove_duplicates extends \phpbb\db\migration\mig
static public function depends_on()
{
return array(
'\phpbb\db\migration\data\v32x\user_notifications_table_indexes',
'\phpbb\db\migration\data\v32x\user_notifications_table_temp_index',
);
}

View File

@ -0,0 +1,46 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\migration\data\v32x;
class user_notifications_table_temp_index extends \phpbb\db\migration\migration
{
static public function depends_on()
{
return array(
'\phpbb\db\migration\data\v32x\user_notifications_table_index_p3',
);
}
public function update_schema()
{
return array(
'add_index' => array(
$this->table_prefix . 'user_notifications' => array(
'itm_usr_mthd' => array('item_type', 'item_id', 'user_id', 'method'),
),
),
);
}
public function revert_schema()
{
return array(
'drop_keys' => array(
$this->table_prefix . 'user_notifications' => array(
'itm_usr_mthd',
),
),
);
}
}

View File

@ -25,7 +25,12 @@ class user_notifications_table_unique_index extends \phpbb\db\migration\migratio
public function update_schema()
{
return array(
'add_unique_index' => array(
'drop_keys' => array(
$this->table_prefix . 'user_notifications' => array(
'itm_usr_mthd',
),
),
'add_unique_index' => array(
$this->table_prefix . 'user_notifications' => array(
'itm_usr_mthd' => array('item_type', 'item_id', 'user_id', 'method'),
),

View File

@ -946,7 +946,8 @@ class tools implements tools_interface
{
case 'oracle':
case 'sqlite3':
$row[$col] = substr($row[$col], strlen($table_name) + 1);
$index_name = $this->check_index_name_length($table_name, $table_name . '_' . $index_name, false);
$row[$col] = strpos($row[$col], $table_name) === 0 ? substr($row[$col], strlen($table_name) + 1) : $row[$col];
break;
}
@ -1359,12 +1360,14 @@ class tools implements tools_interface
{
case 'mysql_40':
case 'mysql_41':
$index_name = $this->check_index_name_length($table_name, $index_name, false);
$statements[] = 'DROP INDEX ' . $index_name . ' ON ' . $table_name;
break;
case 'oracle':
case 'sqlite3':
$statements[] = 'DROP INDEX ' . $table_name . '_' . $index_name;
$index_name = $this->check_index_name_length($table_name, $table_name . '_' . $index_name, false);
$statements[] = 'DROP INDEX ' . $index_name;
break;
}
@ -1491,13 +1494,13 @@ class tools implements tools_interface
{
case 'oracle':
case 'sqlite3':
$this->check_index_name_length($table_name, $table_name . '_' . $index_name);
$statements[] = 'CREATE UNIQUE INDEX ' . $table_name . '_' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
$index_name = $this->check_index_name_length($table_name, $table_name . '_' . $index_name);
$statements[] = 'CREATE UNIQUE INDEX ' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
break;
case 'mysql_40':
case 'mysql_41':
$this->check_index_name_length($table_name, $index_name);
$index_name = $this->check_index_name_length($table_name, $index_name);
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD UNIQUE INDEX ' . $index_name . '(' . implode(', ', $column) . ')';
break;
}
@ -1522,8 +1525,8 @@ class tools implements tools_interface
{
case 'oracle':
case 'sqlite3':
$this->check_index_name_length($table_name, $table_name . '_' . $index_name);
$statements[] = 'CREATE INDEX ' . $table_name . '_' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
$index_name = $this->check_index_name_length($table_name, $table_name . '_' . $index_name);
$statements[] = 'CREATE INDEX ' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
break;
case 'mysql_40':
@ -1538,7 +1541,7 @@ class tools implements tools_interface
}
// no break
case 'mysql_41':
$this->check_index_name_length($table_name, $index_name);
$index_name = $this->check_index_name_length($table_name, $index_name);
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD INDEX ' . $index_name . ' (' . implode(', ', $column) . ')';
break;
}
@ -1551,13 +1554,35 @@ class tools implements tools_interface
*
* @param string $table_name
* @param string $index_name
* @param bool $throw_error
* @return string The index name, shortened if too long
*/
protected function check_index_name_length($table_name, $index_name)
protected function check_index_name_length($table_name, $index_name, $throw_error = true)
{
if (strlen($index_name) > 30)
{
trigger_error("Index name '$index_name' on table '$table_name' is too long. The maximum is 30 characters.", E_USER_ERROR);
// Try removing the table prefix if it's at the beginning
$table_prefix = substr(CONFIG_TABLE, 0, -6); // strlen(config)
if (strpos($index_name, $table_prefix) === 0)
{
$index_name = substr($index_name, strlen($table_prefix) + 1);
return $this->check_index_name_length($table_name, $index_name);
}
// Try removing the table name then
if (strpos($index_name, $table_name) === 0)
{
$index_name = substr($index_name, strlen($table_name) + 1);
return $this->check_index_name_length($table_name, $index_name);
}
if ($throw_error)
{
trigger_error("Index name '$index_name' on table '$table_name' is too long. The maximum is 30 characters.", E_USER_ERROR);
}
}
return $index_name;
}
/**