-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8457 from kenjis/fix-sqlite3-modifyColumn
fix: [SQLite3] Forge::modifyColumn() messes up table
- Loading branch information
Showing
5 changed files
with
206 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
tests/system/Database/Live/SQLite3/ForgeModifyColumnTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Database\Live\SQLite3; | ||
|
||
use CodeIgniter\Database\Forge; | ||
use CodeIgniter\Test\CIUnitTestCase; | ||
use Config\Database; | ||
|
||
/** | ||
* @group DatabaseLive | ||
* | ||
* @internal | ||
*/ | ||
final class ForgeModifyColumnTest extends CIUnitTestCase | ||
{ | ||
private Forge $forge; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->db = Database::connect($this->DBGroup); | ||
|
||
if ($this->db->DBDriver !== 'SQLite3') { | ||
$this->markTestSkipped('This test is only for SQLite3.'); | ||
} | ||
|
||
$this->forge = Database::forge($this->DBGroup); | ||
} | ||
|
||
public function testModifyColumnRename(): void | ||
{ | ||
$table = 'forge_test_three'; | ||
|
||
$this->forge->dropTable($table, true); | ||
|
||
$this->forge->addField([ | ||
'id' => [ | ||
'type' => 'INTEGER', | ||
'constraint' => 11, | ||
'auto_increment' => true, | ||
], | ||
'int' => [ | ||
'type' => 'INT', | ||
'constraint' => 10, | ||
'null' => false, | ||
'default' => 0, | ||
], | ||
'varchar' => [ | ||
'type' => 'VARCHAR', | ||
'constraint' => 7, | ||
'null' => false, | ||
], | ||
'decimal' => [ | ||
'type' => 'DECIMAL', | ||
'constraint' => '10,5', | ||
'default' => 0.1, | ||
], | ||
'name' => [ | ||
'type' => 'VARCHAR', | ||
'constraint' => 255, | ||
'null' => true, | ||
], | ||
]); | ||
|
||
$this->forge->addKey('id', true); | ||
$this->forge->createTable($table); | ||
|
||
$this->assertTrue($this->db->fieldExists('name', $table)); | ||
|
||
$this->forge->modifyColumn($table, [ | ||
'name' => [ | ||
'name' => 'altered', | ||
'type' => 'VARCHAR', | ||
'constraint' => 255, | ||
'null' => true, | ||
], | ||
]); | ||
|
||
$this->db->resetDataCache(); | ||
|
||
$fieldData = $this->db->getFieldData($table); | ||
$fields = []; | ||
|
||
foreach ($fieldData as $obj) { | ||
$fields[$obj->name] = $obj; | ||
} | ||
|
||
$this->assertFalse($this->db->fieldExists('name', $table)); | ||
$this->assertTrue($this->db->fieldExists('altered', $table)); | ||
|
||
$this->assertFalse($fields['int']->nullable); | ||
$this->assertSame('0', $fields['int']->default); | ||
|
||
$this->assertFalse($fields['varchar']->nullable); | ||
$this->assertNull($fields['varchar']->default); | ||
|
||
$this->assertFalse($fields['decimal']->nullable); | ||
$this->assertSame('0.1', $fields['decimal']->default); | ||
|
||
$this->assertTrue($fields['altered']->nullable); | ||
$this->assertNull($fields['altered']->default); | ||
|
||
$this->forge->dropTable($table, true); | ||
} | ||
} |