diff --git a/system/Database/BaseConnection.php b/system/Database/BaseConnection.php index 0ebbd71fabe9..9d7e69bf5256 100644 --- a/system/Database/BaseConnection.php +++ b/system/Database/BaseConnection.php @@ -14,6 +14,7 @@ use Closure; use CodeIgniter\Database\Exceptions\DatabaseException; use CodeIgniter\Events\Events; +use stdClass; use Throwable; /** @@ -974,6 +975,7 @@ public function getConnectDuration(int $decimals = 6): string * @param bool $fieldExists Supplied $item contains a column name? * * @return array|string + * @phpstan-return ($item is array ? array : string) */ public function protectIdentifiers($item, bool $prefixSingle = false, ?bool $protectIdentifiers = null, bool $fieldExists = true) { @@ -1444,7 +1446,7 @@ public function fieldExists(string $fieldName, string $tableName): bool /** * Returns an object with field data * - * @return array + * @return stdClass[] */ public function getFieldData(string $table) { diff --git a/system/Database/SQLite3/Table.php b/system/Database/SQLite3/Table.php index 8f04c0f645f1..1ecb28fdf8b7 100644 --- a/system/Database/SQLite3/Table.php +++ b/system/Database/SQLite3/Table.php @@ -28,6 +28,7 @@ class Table * All of the fields this table represents. * * @var array + * @phpstan-var array> */ protected $fields = []; @@ -276,10 +277,18 @@ protected function copyData() $exFields[] = $name; } - $exFields = implode(', ', $exFields); - $newFields = implode(', ', $newFields); - - $this->db->query("INSERT INTO {$this->prefixedTableName}({$newFields}) SELECT {$exFields} FROM {$this->db->DBPrefix}temp_{$this->tableName}"); + $exFields = implode( + ', ', + array_map(fn ($item) => $this->db->protectIdentifiers($item), $exFields) + ); + $newFields = implode( + ', ', + array_map(fn ($item) => $this->db->protectIdentifiers($item), $newFields) + ); + + $this->db->query( + "INSERT INTO {$this->prefixedTableName}({$newFields}) SELECT {$exFields} FROM {$this->db->DBPrefix}temp_{$this->tableName}" + ); } /** @@ -289,6 +298,7 @@ protected function copyData() * @param array|bool $fields * * @return mixed + * @phpstan-return ($fields is array ? array : mixed) */ protected function formatFields($fields) { diff --git a/tests/system/Database/Live/SQLite/AlterTableTest.php b/tests/system/Database/Live/SQLite/AlterTableTest.php index 9f819ee36945..7eb2216139e5 100644 --- a/tests/system/Database/Live/SQLite/AlterTableTest.php +++ b/tests/system/Database/Live/SQLite/AlterTableTest.php @@ -51,6 +51,7 @@ protected function setUp(): void $config = [ 'DBDriver' => 'SQLite3', 'database' => 'database.db', + 'DBDebug' => true, ]; $this->db = db_connect($config); @@ -88,7 +89,7 @@ public function testFromTableFillsDetails() $fields = $this->getPrivateProperty($this->table, 'fields'); - $this->assertCount(4, $fields); + $this->assertCount(5, $fields); $this->assertArrayHasKey('id', $fields); $this->assertNull($fields['id']['default']); $this->assertTrue($fields['id']['null']); @@ -219,7 +220,7 @@ public function testProcessCopiesOldData() ->dropColumn('name') ->run(); - $this->dontSeeInDatabase('foo', ['name' => 'George Clinton']); + $this->assertFalse($this->db->fieldExists('name', 'foo')); $this->seeInDatabase('foo', ['email' => 'funkalicious@example.com']); } @@ -264,6 +265,11 @@ protected function createTable(string $tableName = 'foo') 'constraint' => 11, 'unsigned' => true, ], + 'group' => [ + 'type' => 'varchar', + 'constraint' => 255, + 'null' => true, + ], ]); $this->forge->addPrimaryKey('id'); $this->forge->addKey('name');