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: Forge::dropColumn() always returns false on SQLite3 driver #9351

Merged
merged 1 commit into from
Dec 31, 2024
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
2 changes: 1 addition & 1 deletion system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ public function addColumn(string $table, $fields): bool
}

/**
* @param array|string $columnNames column names to DROP
* @param list<string>|string $columnNames column names to DROP
*
* @return bool
*
Expand Down
36 changes: 25 additions & 11 deletions system/Database/SQLite3/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,31 @@ public function dropDatabase(string $dbName): bool
return true;
}

/**
* @param list<string>|string $columnNames
*
* @throws DatabaseException
*/
public function dropColumn(string $table, $columnNames): bool
{
$columns = is_array($columnNames) ? $columnNames : array_map(trim(...), explode(',', $columnNames));
michalsn marked this conversation as resolved.
Show resolved Hide resolved
$result = (new Table($this->db, $this))
->fromTable($this->db->DBPrefix . $table)
->dropColumn($columns)
->run();

if (! $result && $this->db->DBDebug) {
throw new DatabaseException(sprintf(
paulbalandan marked this conversation as resolved.
Show resolved Hide resolved
'Failed to drop column%s "%s" on "%s" table.',
count($columns) > 1 ? 's' : '',
implode('", "', $columns),
$table,
));
}

return $result;
}

/**
* @param array|string $processedFields Processed column definitions
* or column names to DROP
Expand All @@ -121,17 +146,6 @@ public function dropDatabase(string $dbName): bool
protected function _alterTable(string $alterType, string $table, $processedFields)
{
switch ($alterType) {
case 'DROP':
$columnNamesToDrop = $processedFields;

$sqlTable = new Table($this->db, $this);

$sqlTable->fromTable($table)
->dropColumn($columnNamesToDrop)
->run();

return ''; // Why empty string?

case 'CHANGE':
$fieldsToModify = [];

Expand Down
3 changes: 1 addition & 2 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@
$this->forge->dropTable('', true);
}

public function testForeignKey(): void

Check warning on line 494 in tests/system/Database/Live/ForgeTest.php

View workflow job for this annotation

GitHub Actions / DatabaseLive (8.2, OCI8, 8.0) / tests

Took 2.7767s from 0.5000s limit to run CodeIgniter\\Database\\Live\\ForgeTest::testForeignKey
{
$this->forge->dropTable('forge_test_invoices', true);
$this->forge->dropTable('forge_test_users', true);
Expand Down Expand Up @@ -1233,7 +1233,7 @@
$this->forge->dropTable('forge_test_1', true);
}

public function testSetKeyNames(): void

Check warning on line 1236 in tests/system/Database/Live/ForgeTest.php

View workflow job for this annotation

GitHub Actions / DatabaseLive (8.2, OCI8, 8.0) / tests

Took 1.0056s from 0.5000s limit to run CodeIgniter\\Database\\Live\\ForgeTest::testSetKeyNames
{
$this->forge->dropTable('forge_test_1', true);

Expand Down Expand Up @@ -1319,8 +1319,7 @@
$this->forge->createTable('forge_test_two');

$this->assertTrue($this->db->fieldExists('name', 'forge_test_two'));

$this->forge->dropColumn('forge_test_two', 'name');
$this->assertTrue($this->forge->dropColumn('forge_test_two', 'name'));

$this->db->resetDataCache();

Expand Down Expand Up @@ -1674,7 +1673,7 @@
$this->forge->dropTable('forge_test_users', true);
}

public function testProcessIndexes(): void

Check warning on line 1676 in tests/system/Database/Live/ForgeTest.php

View workflow job for this annotation

GitHub Actions / DatabaseLive (8.2, OCI8, 8.0) / tests

Took 1.4073s from 0.5000s limit to run CodeIgniter\\Database\\Live\\ForgeTest::testProcessIndexes
{
// make sure tables don't exist
$this->forge->dropTable('actions', true);
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.5.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Bugs Fixed
**********

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

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
Expand Down
7 changes: 1 addition & 6 deletions utils/phpstan-baseline/missingType.iterableValue.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 1686 errors
# total 1685 errors

parameters:
ignoreErrors:
Expand Down Expand Up @@ -1697,11 +1697,6 @@ parameters:
count: 1
path: ../../system/Database/Forge.php

-
message: '#^Method CodeIgniter\\Database\\Forge\:\:dropColumn\(\) has parameter \$columnNames with no value type specified in iterable type array\.$#'
count: 1
path: ../../system/Database/Forge.php

-
message: '#^Method CodeIgniter\\Database\\Forge\:\:modifyColumn\(\) has parameter \$fields with no value type specified in iterable type array\.$#'
count: 1
Expand Down
Loading