Skip to content

Commit b4ed61d

Browse files
committed
fix: SQLite3 Forge::modifyColumn() changes NULL incorrectly
1 parent f89ec81 commit b4ed61d

3 files changed

Lines changed: 60 additions & 9 deletions

File tree

system/Database/Forge.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -897,13 +897,19 @@ protected function _processFields(bool $createTable = false): array
897897
$this->_attributeDefault($attributes, $field);
898898

899899
if (isset($attributes['NULL'])) {
900+
$nullString = ' ' . $this->null;
901+
900902
if ($attributes['NULL'] === true) {
901-
$field['null'] = empty($this->null) ? '' : ' ' . $this->null;
903+
$field['null'] = empty($this->null) ? '' : $nullString;
904+
} elseif ($attributes['NULL'] === $nullString) {
905+
$field['null'] = $nullString;
906+
} elseif ($attributes['NULL'] === '') {
907+
$field['null'] = '';
902908
} else {
903-
$field['null'] = ' NOT NULL';
909+
$field['null'] = ' NOT ' . $this->null;
904910
}
905911
} elseif ($createTable === true) {
906-
$field['null'] = ' NOT NULL';
912+
$field['null'] = ' NOT ' . $this->null;
907913
}
908914

909915
$this->_attributeAutoIncrement($attributes, $field);

system/Database/SQLite3/Table.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,14 @@ public function dropColumn($columns)
183183
*
184184
* @return Table
185185
*/
186-
public function modifyColumn(array $field)
186+
public function modifyColumn(array $fields)
187187
{
188-
$field = $field[0];
189-
190-
$oldName = $field['name'];
191-
unset($field['name']);
188+
foreach ($fields as $field) {
189+
$oldName = $field['name'];
190+
unset($field['name']);
192191

193-
$this->fields[$oldName] = $field;
192+
$this->fields[$oldName] = $field;
193+
}
194194

195195
return $this;
196196
}

tests/system/Database/Live/ForgeTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Test\DatabaseTestTrait;
1818
use Config\Database;
1919
use RuntimeException;
20+
use stdClass;
2021
use Tests\Support\Database\Seeds\CITestSeeder;
2122

2223
/**
@@ -1276,6 +1277,50 @@ public function testModifyColumnRename()
12761277
$this->forge->dropTable('forge_test_three', true);
12771278
}
12781279

1280+
public function testModifyColumnNull()
1281+
{
1282+
if ($this->db->DBDriver === 'SQLSRV') {
1283+
$this->markTestSkipped('SQLSRV does not support getFieldData() nullable.');
1284+
}
1285+
1286+
$this->forge->dropTable('forge_test_modify', true);
1287+
1288+
$this->forge->addField([
1289+
'col1' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
1290+
'col2' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
1291+
'col3' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
1292+
]);
1293+
$this->forge->createTable('forge_test_modify');
1294+
1295+
$this->forge->modifyColumn('forge_test_modify', [
1296+
'col1' => ['type' => 'VARCHAR', 'constraint' => 1],
1297+
'col2' => ['type' => 'VARCHAR', 'constraint' => 1, 'null' => true],
1298+
'col3' => ['type' => 'VARCHAR', 'constraint' => 1, 'null' => false],
1299+
]);
1300+
1301+
$this->db->resetDataCache();
1302+
1303+
$col1 = $this->getMetaData('col1', 'forge_test_modify');
1304+
$this->assertTrue($col1->nullable);
1305+
$col2 = $this->getMetaData('col2', 'forge_test_modify');
1306+
$this->assertTrue($col2->nullable);
1307+
$col3 = $this->getMetaData('col3', 'forge_test_modify');
1308+
$this->assertFalse($col3->nullable);
1309+
1310+
$this->forge->dropTable('forge_test_modify', true);
1311+
}
1312+
1313+
private function getMetaData(string $column, string $table): stdClass
1314+
{
1315+
$fields = $this->db->getFieldData($table);
1316+
1317+
return $fields[array_search(
1318+
$column,
1319+
array_column($fields, 'name'),
1320+
true
1321+
)];
1322+
}
1323+
12791324
public function testConnectWithArrayGroup()
12801325
{
12811326
$group = config('Database');

0 commit comments

Comments
 (0)