From 0e13f38c2df5a3868beb4c0e13de35f8b5ed38f2 Mon Sep 17 00:00:00 2001 From: Usman Date: Wed, 19 Feb 2020 22:48:22 +0100 Subject: [PATCH] Fix drop column with array --- system/Database/Forge.php | 23 ++++--- system/Database/SQLite3/Table.php | 22 +++++-- tests/system/Database/Live/ForgeTest.php | 76 ++++++++++++++++++++++++ user_guide_src/source/dbmgmt/forge.rst | 15 +++-- 4 files changed, 121 insertions(+), 15 deletions(-) diff --git a/system/Database/Forge.php b/system/Database/Forge.php index ebe31fd6dcf2..b40dc5ebf930 100644 --- a/system/Database/Forge.php +++ b/system/Database/Forge.php @@ -816,13 +816,13 @@ public function addColumn(string $table, $field): bool /** * Column Drop * - * @param string $table Table name - * @param string $column_name Column name + * @param string $table Table name + * @param string|array $column_name Column name Array or comma separated * * @return mixed * @throws \CodeIgniter\Database\Exceptions\DatabaseException */ - public function dropColumn(string $table, string $column_name) + public function dropColumn(string $table, $column_name) { $sql = $this->_alterTable('DROP', $this->db->DBPrefix . $table, $column_name); if ($sql === false) @@ -897,24 +897,33 @@ public function modifyColumn(string $table, $field): bool * * @param string $alter_type ALTER type * @param string $table Table name - * @param mixed $field Column definition + * @param mixed $fields Column definition * * @return string|string[] */ - protected function _alterTable(string $alter_type, string $table, $field) + protected function _alterTable(string $alter_type, string $table, $fields) { $sql = 'ALTER TABLE ' . $this->db->escapeIdentifiers($table) . ' '; // DROP has everything it needs now. if ($alter_type === 'DROP') { - return $sql . 'DROP COLUMN ' . $this->db->escapeIdentifiers($field); + if (is_string($fields)) + { + $fields = explode(',', $fields); + } + + $fields = array_map(function ($field) { + return 'DROP COLUMN ' . $this->db->escapeIdentifiers(trim($field)); + }, $fields); + + return $sql . implode(', ', $fields); } $sql .= ($alter_type === 'ADD') ? 'ADD ' : $alter_type . ' COLUMN '; $sqls = []; - foreach ($field as $data) + foreach ($fields as $data) { $sqls[] = $sql . ($data['_literal'] !== false ? $data['_literal'] : $this->_processColumn($data)); diff --git a/system/Database/SQLite3/Table.php b/system/Database/SQLite3/Table.php index 46264dae784a..0845a35634b6 100644 --- a/system/Database/SQLite3/Table.php +++ b/system/Database/SQLite3/Table.php @@ -185,15 +185,29 @@ public function run(): bool } /** - * Drops a column from the table. + * Drops columns from the table. * - * @param string $column + * @param string|array $columns * * @return \CodeIgniter\Database\SQLite3\Table */ - public function dropColumn(string $column) + public function dropColumn($columns) { - unset($this->fields[$column]); + //unset($this->fields[$column]); + + if (is_string($columns)) + { + $columns = explode(',', $columns); + } + + foreach ($columns as $column) + { + $column = trim($column); + if (isset($this->fields[$column])) + { + unset($this->fields[$column]); + } + } return $this; } diff --git a/tests/system/Database/Live/ForgeTest.php b/tests/system/Database/Live/ForgeTest.php index 7d7b88ec09c2..0c1c9e49d0a3 100644 --- a/tests/system/Database/Live/ForgeTest.php +++ b/tests/system/Database/Live/ForgeTest.php @@ -759,4 +759,80 @@ public function testDropTableSuccess() $this->assertCount(0, $this->db->getIndexData('droptest')); } } + + public function testDropMultipleColumnWithArray() + { + $this->forge->dropTable('forge_test_two', true); + + $this->forge->addField([ + 'id' => [ + 'type' => 'INTEGER', + 'constraint' => 11, + 'unsigned' => false, + 'auto_increment' => true, + ], + 'name' => [ + 'type' => 'varchar', + 'constraint' => 255, + 'null' => true, + ], + 'email' => [ + 'type' => 'varchar', + 'constraint' => 255, + 'null' => true, + ], + ]); + + $this->forge->addKey('id', true); + $this->forge->createTable('forge_test_two'); + + $this->assertTrue($this->db->fieldExists('name', 'forge_test_two')); + + $this->forge->dropColumn('forge_test_two', ['id', 'name']); + + $this->db->resetDataCache(); + + $this->assertFalse($this->db->fieldExists('id', 'forge_test_two')); + $this->assertFalse($this->db->fieldExists('name', 'forge_test_two')); + + $this->forge->dropTable('forge_test_two', true); + } + + public function testDropMultipleColumnWithString() + { + $this->forge->dropTable('forge_test_four', true); + + $this->forge->addField([ + 'id' => [ + 'type' => 'INTEGER', + 'constraint' => 11, + 'unsigned' => false, + 'auto_increment' => true, + ], + 'name' => [ + 'type' => 'varchar', + 'constraint' => 255, + 'null' => true, + ], + 'email' => [ + 'type' => 'varchar', + 'constraint' => 255, + 'null' => true, + ], + ]); + + $this->forge->addKey('id', true); + $this->forge->createTable('forge_test_four'); + + $this->assertTrue($this->db->fieldExists('name', 'forge_test_four')); + + $this->forge->dropColumn('forge_test_four', 'id, name'); + + $this->db->resetDataCache(); + + $this->assertFalse($this->db->fieldExists('id', 'forge_test_four')); + $this->assertFalse($this->db->fieldExists('name', 'forge_test_four')); + + $this->forge->dropTable('forge_test_four', true); + } } diff --git a/user_guide_src/source/dbmgmt/forge.rst b/user_guide_src/source/dbmgmt/forge.rst index 0f7badc3d6a7..9e811c437964 100644 --- a/user_guide_src/source/dbmgmt/forge.rst +++ b/user_guide_src/source/dbmgmt/forge.rst @@ -313,7 +313,7 @@ Examples:: 'preferences' => ['type' => 'TEXT', 'first' => TRUE] ]; -Dropping a Column From a Table +Dropping Columns From a Table ============================== **$forge->dropColumn()** @@ -322,7 +322,14 @@ Used to remove a column from a table. :: - $forge->dropColumn('table_name', 'column_to_drop'); + $forge->dropColumn('table_name', 'column_to_drop'); // to drop one single column + +Used to remove multiple columns from a table. + +:: + + $forge->dropColumn('table_name', 'column_1,column_2'); // by proving comma separated column names + $forge->dropColumn('table_name', ['column_1', 'column_2']); // by proving array of column names Modifying a Column in a Table ============================= @@ -415,11 +422,11 @@ Class Reference .. php:method:: dropColumn($table, $column_name) :param string $table: Table name - :param array $column_name: The column name to drop + :param mixed $column_names: Comma-delimited string or an array of column names :returns: TRUE on success, FALSE on failure :rtype: bool - Drops a column from a table. Usage: See `Dropping a Column From a Table`_. + Drops single or multiple columns from a table. Usage: See `Dropping Columns From a Table`_. .. php:method:: dropDatabase($dbName)