diff options
-rw-r--r-- | phpBB/phpbb/db/tools/mssql.php | 20 | ||||
-rw-r--r-- | phpBB/phpbb/db/tools/tools.php | 15 | ||||
-rw-r--r-- | tests/dbal/db_tools_test.php | 24 |
3 files changed, 46 insertions, 13 deletions
diff --git a/phpBB/phpbb/db/tools/mssql.php b/phpBB/phpbb/db/tools/mssql.php index 1e4d3aee2f..b84c0db403 100644 --- a/phpBB/phpbb/db/tools/mssql.php +++ b/phpBB/phpbb/db/tools/mssql.php @@ -545,10 +545,7 @@ class mssql extends tools { $statements = array(); - if ($this->mssql_is_sql_server_2000()) - { - $this->check_index_name_length($table_name, $index_name); - } + $this->check_index_name_length($table_name, $index_name); // remove index length $column = preg_replace('#:.*$#', '', $column); @@ -559,6 +556,21 @@ class mssql extends tools } /** + * {@inheritdoc} + */ + protected function get_max_index_name_length() + { + if ($this->mssql_is_sql_server_2000()) + { + return parent::get_max_index_name_length(); + } + else + { + return 128; + } + } + + /** * {@inheritDoc} */ function sql_list_index($table_name) diff --git a/phpBB/phpbb/db/tools/tools.php b/phpBB/phpbb/db/tools/tools.php index 2f891e43d5..d21d34b8a9 100644 --- a/phpBB/phpbb/db/tools/tools.php +++ b/phpBB/phpbb/db/tools/tools.php @@ -1561,7 +1561,8 @@ class tools implements tools_interface */ protected function check_index_name_length($table_name, $index_name, $throw_error = true) { - if (strlen($index_name) > 30) + $max_index_name_length = $this->get_max_index_name_length(); + if (strlen($index_name) > $max_index_name_length) { // Try removing the table prefix if it's at the beginning $table_prefix = substr(CONFIG_TABLE, 0, -6); // strlen(config) @@ -1582,7 +1583,7 @@ class tools implements tools_interface if ($throw_error) { - trigger_error("Index name '$index_name' on table '$table_name' is too long. The maximum is 30 characters.", E_USER_ERROR); + trigger_error("Index name '$index_name' on table '$table_name' is too long. The maximum is $max_index_name_length characters.", E_USER_ERROR); } } @@ -1590,6 +1591,16 @@ class tools implements tools_interface } /** + * Get maximum index name length. Might vary depending on db type + * + * @return int Maximum index name length + */ + protected function get_max_index_name_length() + { + return 30; + } + + /** * {@inheritDoc} */ function sql_list_index($table_name) diff --git a/tests/dbal/db_tools_test.php b/tests/dbal/db_tools_test.php index 72a3718662..0365463a48 100644 --- a/tests/dbal/db_tools_test.php +++ b/tests/dbal/db_tools_test.php @@ -439,29 +439,39 @@ class phpbb_dbal_db_tools_test extends phpbb_database_test_case $this->markTestIncomplete('The table prefix length is too long for proper testing of index shortening function.'); } + $max_index_length = 30; + + if ($this->tools instanceof \phpbb\db\tools\mssql) + { + $max_length_method = new ReflectionMethod('\phpbb\db\tools\mssql', 'get_max_index_name_length'); + $max_length_method->setAccessible(true); + $max_index_length = $max_length_method->invoke($this->tools); + } + $table_suffix = str_repeat('a', 25 - strlen($table_prefix)); $table_name = $table_prefix . $table_suffix; $this->tools->sql_create_table($table_name, $this->table_data); - // Index name and table suffix and table prefix have > 30 chars in total. - // Index name and table suffix have <= 30 chars in total. - $long_index_name = str_repeat('i', 30 - strlen($table_suffix)); + // Index name and table suffix and table prefix have > maximum index length chars in total. + // Index name and table suffix have <= maximum index length chars in total. + $long_index_name = str_repeat('i', $max_index_length - strlen($table_suffix)); $this->assertFalse($this->tools->sql_index_exists($table_name, $long_index_name)); $this->assertTrue($this->tools->sql_create_index($table_name, $long_index_name, array('c_timestamp'))); $this->assertTrue($this->tools->sql_index_exists($table_name, $long_index_name)); - // Index name and table suffix have > 30 chars in total. - $very_long_index_name = str_repeat('i', 30); + // Index name and table suffix have > maximum index length chars in total. + $very_long_index_name = str_repeat('i', $max_index_length); $this->assertFalse($this->tools->sql_index_exists($table_name, $very_long_index_name)); $this->assertTrue($this->tools->sql_create_index($table_name, $very_long_index_name, array('c_timestamp'))); $this->assertTrue($this->tools->sql_index_exists($table_name, $very_long_index_name)); $this->tools->sql_table_drop($table_name); - // Index name has > 30 chars - that should not be possible. - $too_long_index_name = str_repeat('i', 31); + // Index name has > maximum index length chars - that should not be possible. + $too_long_index_name = str_repeat('i', $max_index_length + 1); $this->assertFalse($this->tools->sql_index_exists('prefix_table_name', $too_long_index_name)); + $this->setExpectedTriggerError(E_USER_ERROR); $this->tools->sql_create_index('prefix_table_name', $too_long_index_name, array('c_timestamp')); } } |