Skip to content

Commit

Permalink
Fix using string primary_key value for sqlite
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Peveler <matt.peveler@gmail.com>
  • Loading branch information
MasterOdin committed Jun 4, 2024
1 parent 6e522d6 commit d20e3e5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Phinx/Db/Adapter/SQLiteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ public function createTable(Table $table, array $columns = [], array $indexes =

$sql = 'CREATE TABLE ';
$sql .= $this->quoteTableName($table->getName()) . ' (';
if (isset($options['primary_key'])) {
$options['primary_key'] = (array)$options['primary_key'];
}
foreach ($columns as $column) {
$sql .= $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column) . ', ';

Expand All @@ -446,9 +449,7 @@ public function createTable(Table $table, array $columns = [], array $indexes =
if (isset($options['primary_key'])) {
$sql = rtrim($sql);
$sql .= ' PRIMARY KEY (';
if (is_string($options['primary_key'])) { // handle primary_key => 'id'
$sql .= $this->quoteColumnName($options['primary_key']);
} elseif (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id')
if (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id')
$sql .= implode(',', array_map([$this, 'quoteColumnName'], $options['primary_key']));
}
$sql .= ')';
Expand Down
16 changes: 16 additions & 0 deletions tests/Phinx/Db/Adapter/SQLiteAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ public function testCreateTableIdentityIdColumn()

$this->assertTrue($this->adapter->hasTable('ntable'));
$this->assertTrue($this->adapter->hasColumn('ntable', 'custom_id'));
$this->assertTrue($this->adapter->hasPrimaryKey('ntable', 'custom_id'));

/** @var \Phinx\Db\Table\Column $idColumn */
$idColumn = $this->adapter->getColumns('ntable')[0];
$this->assertTrue($idColumn->getIdentity());
}

public function testCreateTableIdentityIdColumnStringPrimaryKey()
{
$table = new Table('ntable', ['id' => false, 'primary_key' => 'custom_id'], $this->adapter);
$table->addColumn('custom_id', 'integer', ['identity' => true])
->save();

$this->assertTrue($this->adapter->hasTable('ntable'));
$this->assertTrue($this->adapter->hasColumn('ntable', 'custom_id'));
$this->assertTrue($this->adapter->hasPrimaryKey('ntable', 'custom_id'));

/** @var \Phinx\Db\Table\Column $idColumn */
$idColumn = $this->adapter->getColumns('ntable')[0];
Expand Down

0 comments on commit d20e3e5

Please sign in to comment.