Skip to content

Commit 2158e20

Browse files
committed
Fix altering sqlite table with indexes or triggers
Signed-off-by: Matthew Peveler <[email protected]>
1 parent d2cc4be commit 2158e20

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Phinx/Db/Adapter/SQLiteAdapter.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,13 +812,24 @@ protected function copyAndDropTmpTable($instructions, $tableName)
812812
$state['selectColumns']
813813
);
814814

815+
$rows = $this->fetchAll(
816+
sprintf(
817+
"SELECT * FROM sqlite_master WHERE `type` = 'index' OR `type` = 'trigger' AND tbl_name = %s",
818+
$this->quoteValue($tableName)
819+
)
820+
);
821+
815822
$this->execute(sprintf('DROP TABLE %s', $this->quoteTableName($tableName)));
816823
$this->execute(sprintf(
817824
'ALTER TABLE %s RENAME TO %s',
818825
$this->quoteTableName($state['tmpTableName']),
819826
$this->quoteTableName($tableName)
820827
));
821828

829+
foreach ($rows as $row) {
830+
$this->execute($row['sql']);
831+
}
832+
822833
return $state;
823834
});
824835

tests/Phinx/Db/Adapter/SQLiteAdapterTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,24 @@ public function testChangeColumnWithForeignKey()
600600
$this->assertTrue($this->adapter->hasForeignKey($table->getName(), ['ref_table_id']));
601601
}
602602

603+
public function testChangeColumnWithIndex()
604+
{
605+
$table = new \Phinx\Db\Table('t', [], $this->adapter);
606+
$table
607+
->addColumn('indexcol', 'integer')
608+
->addIndex(
609+
'indexcol',
610+
['unique' => true]
611+
)
612+
->create();
613+
614+
$this->assertTrue($this->adapter->hasIndex($table->getName(), 'indexcol'));
615+
616+
$table->changeColumn('indexcol', 'integer', ['null' => false])->update();
617+
618+
$this->assertTrue($this->adapter->hasIndex($table->getName(), 'indexcol'));
619+
}
620+
603621
public function testChangeColumnDefaultToZero()
604622
{
605623
$table = new \Phinx\Db\Table('t', [], $this->adapter);

0 commit comments

Comments
 (0)