|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Database\Live\SQLite3; |
| 13 | + |
| 14 | +use CodeIgniter\Database\Forge; |
| 15 | +use CodeIgniter\Test\CIUnitTestCase; |
| 16 | +use Config\Database; |
| 17 | + |
| 18 | +/** |
| 19 | + * @group DatabaseLive |
| 20 | + * |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +final class ForgeModifyColumnTest extends CIUnitTestCase |
| 24 | +{ |
| 25 | + private Forge $forge; |
| 26 | + |
| 27 | + protected function setUp(): void |
| 28 | + { |
| 29 | + parent::setUp(); |
| 30 | + |
| 31 | + $this->db = Database::connect($this->DBGroup); |
| 32 | + |
| 33 | + if ($this->db->DBDriver !== 'SQLite3') { |
| 34 | + $this->markTestSkipped('This test is only for SQLite3.'); |
| 35 | + } |
| 36 | + |
| 37 | + $this->forge = Database::forge($this->DBGroup); |
| 38 | + } |
| 39 | + |
| 40 | + public function testModifyColumnRename(): void |
| 41 | + { |
| 42 | + $table = 'forge_test_three'; |
| 43 | + |
| 44 | + $this->forge->dropTable($table, true); |
| 45 | + |
| 46 | + $this->forge->addField([ |
| 47 | + 'id' => [ |
| 48 | + 'type' => 'INTEGER', |
| 49 | + 'constraint' => 11, |
| 50 | + 'auto_increment' => true, |
| 51 | + ], |
| 52 | + 'int' => [ |
| 53 | + 'type' => 'INT', |
| 54 | + 'constraint' => 10, |
| 55 | + 'null' => false, |
| 56 | + 'default' => 0, |
| 57 | + ], |
| 58 | + 'varchar' => [ |
| 59 | + 'type' => 'VARCHAR', |
| 60 | + 'constraint' => 7, |
| 61 | + 'null' => false, |
| 62 | + ], |
| 63 | + 'decimal' => [ |
| 64 | + 'type' => 'DECIMAL', |
| 65 | + 'constraint' => '10,5', |
| 66 | + 'default' => 0.1, |
| 67 | + ], |
| 68 | + 'name' => [ |
| 69 | + 'type' => 'VARCHAR', |
| 70 | + 'constraint' => 255, |
| 71 | + 'null' => true, |
| 72 | + ], |
| 73 | + ]); |
| 74 | + |
| 75 | + $this->forge->addKey('id', true); |
| 76 | + $this->forge->createTable($table); |
| 77 | + |
| 78 | + $this->assertTrue($this->db->fieldExists('name', $table)); |
| 79 | + |
| 80 | + $this->forge->modifyColumn($table, [ |
| 81 | + 'name' => [ |
| 82 | + 'name' => 'altered', |
| 83 | + 'type' => 'VARCHAR', |
| 84 | + 'constraint' => 255, |
| 85 | + 'null' => true, |
| 86 | + ], |
| 87 | + ]); |
| 88 | + |
| 89 | + $this->db->resetDataCache(); |
| 90 | + |
| 91 | + $fieldData = $this->db->getFieldData($table); |
| 92 | + $fields = []; |
| 93 | + |
| 94 | + foreach ($fieldData as $obj) { |
| 95 | + $fields[$obj->name] = $obj; |
| 96 | + } |
| 97 | + |
| 98 | + $this->assertFalse($this->db->fieldExists('name', $table)); |
| 99 | + $this->assertTrue($this->db->fieldExists('altered', $table)); |
| 100 | + |
| 101 | + $this->assertFalse($fields['int']->nullable); |
| 102 | + $this->assertSame('0', $fields['int']->default); |
| 103 | + |
| 104 | + $this->assertFalse($fields['varchar']->nullable); |
| 105 | + $this->assertNull($fields['varchar']->default); |
| 106 | + |
| 107 | + $this->assertFalse($fields['decimal']->nullable); |
| 108 | + $this->assertSame('0.1', $fields['decimal']->default); |
| 109 | + |
| 110 | + $this->assertTrue($fields['altered']->nullable); |
| 111 | + $this->assertNull($fields['altered']->default); |
| 112 | + |
| 113 | + $this->forge->dropTable($table, true); |
| 114 | + } |
| 115 | +} |
0 commit comments