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() seems to always return false. #8931

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
6 changes: 0 additions & 6 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -2677,12 +2677,6 @@
'count' => 1,
'path' => __DIR__ . '/system/Database/Forge.php',
];
$ignoreErrors[] = [
// identifier: missingType.iterableValue
'message' => '#^Method CodeIgniter\\\\Database\\\\Forge\\:\\:dropColumn\\(\\) has parameter \\$columnNames with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Database/Forge.php',
];
$ignoreErrors[] = [
// identifier: missingType.iterableValue
'message' => '#^Method CodeIgniter\\\\Database\\\\Forge\\:\\:modifyColumn\\(\\) has parameter \\$fields with no value type specified in iterable type array\\.$#',
Expand Down
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
38 changes: 27 additions & 11 deletions system/Database/SQLite3/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,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 Expand Up @@ -164,6 +153,33 @@ protected function _alterTable(string $alterType, string $table, $processedField
}
}

/**
* @param list<string>|string $columnNames column names to DROP
*
* @return bool
*
* @throws DatabaseException
*/
public function dropColumn(string $table, $columnNames)
{
$sqlTable = new Table($this->db, $this);

$sqlExecuteResult = $sqlTable->fromTable($this->db->DBPrefix . $table)
->dropColumn($columnNames)
->run();

if ($sqlExecuteResult === false) {
$columns = is_array($columnNames) ? implode(',', $columnNames) : $columnNames;

throw new DatabaseException(
'Failed to drop column. Table: "' . $table
. '", Column: "' . $columns . '"'
);
Comment on lines +174 to +177
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dropColumn() may return false when $this->db->DBDebug is false.
So make this the same, please.

public function dropColumn(string $table, $columnNames)
{
$sql = $this->_alterTable('DROP', $this->db->DBPrefix . $table, $columnNames);
if ($sql === false) {
if ($this->db->DBDebug) {
throw new DatabaseException('This feature is not available for the database you are using.');
}
return false;
}
return $this->db->query($sql);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ping-yee Can you complete this PR?
We are very close to the completion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kenjis Sorry for the stagnation, I haven’t had much energy to deal with things other than work a while ago.
I modify this right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, the rector removed the return false line, implying that if sqlExecuteResult is false, the method would still return false.

}

return $sqlExecuteResult;
}

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

public function testForeignKey(): void

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

View workflow job for this annotation

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

Took 2.7794s 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 @@ -1230,7 +1230,7 @@
$this->forge->dropTable('forge_test_1', true);
}

public function testSetKeyNames(): void

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

View workflow job for this annotation

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

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

Expand Down Expand Up @@ -1317,7 +1317,8 @@

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

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

$this->db->resetDataCache();

Expand Down Expand Up @@ -1578,7 +1579,7 @@
$this->forge->dropTable('forge_test_four', true);
}

public function testDropKey(): void

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

View workflow job for this annotation

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

Took 0.7981s from 0.5000s limit to run CodeIgniter\\Database\\Live\\ForgeTest::testDropKey
{
$this->forge->dropTable('key_test_users', true);
$keyName = 'key_test_users_id';
Expand Down Expand Up @@ -1665,7 +1666,7 @@
$this->forge->dropTable('forge_test_users', true);
}

public function testProcessIndexes(): void

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

View workflow job for this annotation

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

Took 1.4026s from 0.5000s limit to run CodeIgniter\\Database\\Live\\ForgeTest::testProcessIndexes
{
// make sure tables don't exist
$this->forge->dropTable('actions', true);
Expand Down
Loading