Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix drop column with array #2578

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Expand Down
22 changes: 18 additions & 4 deletions system/Database/SQLite3/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
76 changes: 76 additions & 0 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
15 changes: 11 additions & 4 deletions user_guide_src/source/dbmgmt/forge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ Examples::
'preferences' => ['type' => 'TEXT', 'first' => TRUE]
];

Dropping a Column From a Table
Dropping Columns From a Table
==============================

**$forge->dropColumn()**
Expand All @@ -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
=============================
Expand Down Expand Up @@ -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)

Expand Down