diff options
author | Marc Alexander <admin@m-a-styles.de> | 2017-01-29 15:56:45 +0100 |
---|---|---|
committer | Marc Alexander <admin@m-a-styles.de> | 2018-01-01 18:54:41 +0100 |
commit | 635befa00e0d9791137a2a500260b578021f60b8 (patch) | |
tree | 678ec16e13f87d79da7bea828d553e2d47315e18 | |
parent | 5bb62f5560ee913efe56c1fcb8c1a855204cc658 (diff) | |
download | forums-635befa00e0d9791137a2a500260b578021f60b8.tar forums-635befa00e0d9791137a2a500260b578021f60b8.tar.gz forums-635befa00e0d9791137a2a500260b578021f60b8.tar.bz2 forums-635befa00e0d9791137a2a500260b578021f60b8.tar.xz forums-635befa00e0d9791137a2a500260b578021f60b8.zip |
[ticket/15055] Drop primary keys when necessary and fix test comparisons
PHPBB3-15055
-rw-r--r-- | phpBB/phpbb/db/tools/mssql.php | 35 | ||||
-rw-r--r-- | tests/dbal/db_tools_test.php | 9 |
2 files changed, 43 insertions, 1 deletions
diff --git a/phpBB/phpbb/db/tools/mssql.php b/phpBB/phpbb/db/tools/mssql.php index 6f800f730d..0dfb09b1ba 100644 --- a/phpBB/phpbb/db/tools/mssql.php +++ b/phpBB/phpbb/db/tools/mssql.php @@ -448,6 +448,10 @@ class mssql extends tools } } + // Drop primary keys depending on this column + $result = $this->mssql_get_drop_default_primary_key_queries($table_name, $column_name); + $statements = array_merge($statements, $result); + // Drop default value constraint $result = $this->mssql_get_drop_default_constraints_queries($table_name, $column_name); $statements = array_merge($statements, $result); @@ -685,6 +689,37 @@ class mssql extends tools } /** + * Get queries to drop the primary keys depending on the specified column + * + * We need to drop primary keys depending on this column before being able + * to delete them. + * + * @param string $table_name + * @param string $column_name + * @return array Array with SQL statements + */ + protected function mssql_get_drop_default_primary_key_queries($table_name, $column_name) + { + $statements = array(); + + $sql = "SELECT ccu.CONSTRAINT_NAME, ccu.COLUMN_NAME + FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc + JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu ON tc.CONSTRAINT_NAME = ccu.Constraint_name + WHERE tc.TABLE_NAME = '{$table_name}' + AND tc.CONSTRAINT_TYPE = 'Primary Key'"; + + $result = $this->db->sql_query($sql); + + while ($primary_key = $this->db->sql_fetchrow($result)) + { + $statements[] = 'ALTER TABLE [' . $table_name . '] DROP CONSTRAINT [' . $primary_key['CONSTRAINT_NAME'] . ']'; + } + $this->db->sql_freeresult($result); + + return $statements; + } + + /** * Checks to see if column is an identity column * * Identity columns cannot have defaults set for them. diff --git a/tests/dbal/db_tools_test.php b/tests/dbal/db_tools_test.php index f9243e7266..f78cebdec7 100644 --- a/tests/dbal/db_tools_test.php +++ b/tests/dbal/db_tools_test.php @@ -203,8 +203,15 @@ class phpbb_dbal_db_tools_test extends phpbb_database_test_case public function test_list_columns() { + $config = $this->get_database_config(); + $table_columns = $this->table_data['COLUMNS']; + + if (strpos($config['dbms'], 'mssql') !== false) + { + ksort($table_columns); + } $this->assertEquals( - array_keys($this->table_data['COLUMNS']), + array_keys($table_columns), array_values($this->tools->sql_list_columns('prefix_table_name')) ); } |