Skip to content

Commit d7ce2ba

Browse files
authored
fix: Forge::dropColumn() always returns false on SQLite3 driver (#9351)
1 parent 0fe4bd0 commit d7ce2ba

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed

system/Database/Forge.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ public function addColumn(string $table, $fields): bool
778778
}
779779

780780
/**
781-
* @param array|string $columnNames column names to DROP
781+
* @param list<string>|string $columnNames column names to DROP
782782
*
783783
* @return bool
784784
*

system/Database/SQLite3/Forge.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,31 @@ public function dropDatabase(string $dbName): bool
110110
return true;
111111
}
112112

113+
/**
114+
* @param list<string>|string $columnNames
115+
*
116+
* @throws DatabaseException
117+
*/
118+
public function dropColumn(string $table, $columnNames): bool
119+
{
120+
$columns = is_array($columnNames) ? $columnNames : array_map(trim(...), explode(',', $columnNames));
121+
$result = (new Table($this->db, $this))
122+
->fromTable($this->db->DBPrefix . $table)
123+
->dropColumn($columns)
124+
->run();
125+
126+
if (! $result && $this->db->DBDebug) {
127+
throw new DatabaseException(sprintf(
128+
'Failed to drop column%s "%s" on "%s" table.',
129+
count($columns) > 1 ? 's' : '',
130+
implode('", "', $columns),
131+
$table,
132+
));
133+
}
134+
135+
return $result;
136+
}
137+
113138
/**
114139
* @param array|string $processedFields Processed column definitions
115140
* or column names to DROP
@@ -121,17 +146,6 @@ public function dropDatabase(string $dbName): bool
121146
protected function _alterTable(string $alterType, string $table, $processedFields)
122147
{
123148
switch ($alterType) {
124-
case 'DROP':
125-
$columnNamesToDrop = $processedFields;
126-
127-
$sqlTable = new Table($this->db, $this);
128-
129-
$sqlTable->fromTable($table)
130-
->dropColumn($columnNamesToDrop)
131-
->run();
132-
133-
return ''; // Why empty string?
134-
135149
case 'CHANGE':
136150
$fieldsToModify = [];
137151

tests/system/Database/Live/ForgeTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,8 +1319,7 @@ public function testDropColumn(): void
13191319
$this->forge->createTable('forge_test_two');
13201320

13211321
$this->assertTrue($this->db->fieldExists('name', 'forge_test_two'));
1322-
1323-
$this->forge->dropColumn('forge_test_two', 'name');
1322+
$this->assertTrue($this->forge->dropColumn('forge_test_two', 'name'));
13241323

13251324
$this->db->resetDataCache();
13261325

user_guide_src/source/changelogs/v4.5.7.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Bugs Fixed
3131
**********
3232

3333
- **Common:** Fixed a bug where the ``helper()`` method may throw `FileNotFoundException` on valid namespaced helper.
34+
- **Forge:** Fixed an issue where `SQLite3`'s Forge always returns `false` when calling ``dropColumn()``.
3435

3536
See the repo's
3637
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

utils/phpstan-baseline/missingType.iterableValue.neon

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 1686 errors
1+
# total 1685 errors
22

33
parameters:
44
ignoreErrors:
@@ -1697,11 +1697,6 @@ parameters:
16971697
count: 1
16981698
path: ../../system/Database/Forge.php
16991699

1700-
-
1701-
message: '#^Method CodeIgniter\\Database\\Forge\:\:dropColumn\(\) has parameter \$columnNames with no value type specified in iterable type array\.$#'
1702-
count: 1
1703-
path: ../../system/Database/Forge.php
1704-
17051700
-
17061701
message: '#^Method CodeIgniter\\Database\\Forge\:\:modifyColumn\(\) has parameter \$fields with no value type specified in iterable type array\.$#'
17071702
count: 1

0 commit comments

Comments
 (0)