Skip to content

Commit b0da9eb

Browse files
authored
Merge pull request #6396 from sclubricants/SqliteTable
Fix Sqlite Table::createTable()
2 parents c3aa797 + 9a4ee1b commit b0da9eb

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

system/Database/SQLite3/Table.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ protected function createTable()
240240

241241
$this->forge->addField($fields);
242242

243+
$fieldNames = array_keys($fields);
244+
245+
$this->keys = array_filter(
246+
$this->keys,
247+
static fn ($index) => count(array_intersect($index['fields'], $fieldNames)) === count($index['fields'])
248+
);
249+
243250
// Unique/Index keys
244251
if (is_array($this->keys)) {
245252
foreach ($this->keys as $key) {

tests/system/Database/Live/SQLite/AlterTableTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,46 @@ public function testDropColumnMaintainsKeys()
156156
$this->assertTrue($result);
157157
}
158158

159+
public function testDropColumnDropCompositeKey()
160+
{
161+
$this->forge->dropTable('actions', true);
162+
163+
$fields = [
164+
'category' => ['type' => 'varchar', 'constraint' => 63],
165+
'name' => ['type' => 'varchar', 'constraint' => 63],
166+
'created_at' => ['type' => 'datetime', 'null' => true],
167+
];
168+
169+
$this->forge->addField('id');
170+
$this->forge->addField($fields);
171+
172+
$this->forge->addKey('name');
173+
$this->forge->addKey(['category', 'name']);
174+
$this->forge->addKey('created_at');
175+
176+
$this->forge->createTable('actions');
177+
178+
$indexes = $this->db->getIndexData('actions');
179+
180+
// the composite index was created
181+
$this->assertSame(['category', 'name'], $indexes['actions_category_name']->fields);
182+
183+
// drop one of the columns in the composite index
184+
$this->forge->dropColumn('actions', 'category');
185+
186+
// get indexes again
187+
$indexes = $this->db->getIndexData('actions');
188+
189+
// check that composite index was dropped.
190+
$this->assertArrayNotHasKey('actions_category_name', $indexes);
191+
192+
// check that that other keys are present
193+
$this->assertArrayHasKey('actions_name', $indexes);
194+
$this->assertArrayHasKey('actions_created_at', $indexes);
195+
196+
$this->forge->dropTable('actions');
197+
}
198+
159199
public function testModifyColumnSuccess()
160200
{
161201
$this->createTable('janky');

0 commit comments

Comments
 (0)