Skip to content

Commit

Permalink
Fix drop column with array
Browse files Browse the repository at this point in the history
  • Loading branch information
Usman authored and musmanikram committed Feb 19, 2020
1 parent 091a438 commit 8e8f761
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 7 deletions.
7 changes: 4 additions & 3 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
*
* @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 @@ -908,6 +908,7 @@ protected function _alterTable(string $alter_type, string $table, $field)
// DROP has everything it needs now.
if ($alter_type === 'DROP')
{
var_dump($this->db->escapeIdentifiers($field));
return $sql . 'DROP COLUMN ' . $this->db->escapeIdentifiers($field);
}

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);
}
}

0 comments on commit 8e8f761

Please sign in to comment.