Skip to content

Commit

Permalink
Fix composite primary key reflection in postgres.
Browse files Browse the repository at this point in the history
Reflecting composite keys that use serial columns should behave
correctly, marking the serial column as autoIncrement.
  • Loading branch information
markstory committed Mar 6, 2015
1 parent c1a6afa commit ae1560a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/Database/Schema/PostgresSchema.php
Expand Up @@ -139,6 +139,10 @@ public function convertColumnDescription(Table $table, $row)
$row['default'] = 0;
}
}
// Sniff out serial types.
if (in_array($field['type'], ['integer', 'biginteger']) && strpos($row['default'], 'nextval(') === 0) {
$field['autoIncrement'] = true;
}
$field += [
'default' => $this->_defaultValue($row['default']),
'null' => $row['null'] === 'YES' ? true : false,
Expand Down Expand Up @@ -231,9 +235,10 @@ public function convertIndexDescription(Table $table, $row)
// If there is only one column in the primary key and it is integery,
// make it autoincrement.
$columnDef = $table->column($columns[0]);
if (count($columns) === 1 &&
in_array($columnDef['type'], ['integer', 'biginteger']) &&
$type === Table::CONSTRAINT_PRIMARY
if (
$type === Table::CONSTRAINT_PRIMARY &&
count($columns) === 1 &&
in_array($columnDef['type'], ['integer', 'biginteger'])
) {
$columnDef['autoIncrement'] = true;
$table->addColumn($columns[0], $columnDef);
Expand Down
28 changes: 28 additions & 0 deletions tests/TestCase/Database/Schema/PostgresSchemaTest.php
Expand Up @@ -345,6 +345,34 @@ public function testDescribeTable()
}
}

/**
* Test describing a table with postgres and composite keys
*
* @return void
*/
public function testDescribeTableCompositeKey()
{
$this->_needsConnection();
$connection = ConnectionManager::get('test');
$sql = <<<SQL
CREATE TABLE schema_composite (
"id" SERIAL,
"site_id" INTEGER NOT NULL,
"name" VARCHAR(255),
PRIMARY KEY("id", "site_id")
);
SQL;
$connection->execute($sql);
$schema = new SchemaCollection($connection);
$result = $schema->describe('schema_composite');
$connection->execute('DROP TABLE schema_composite');

$this->assertEquals(['id', 'site_id'], $result->primaryKey());
$this->assertNull($result->column('site_id')['autoIncrement'], 'site_id should not be autoincrement');
$this->assertTrue($result->column('id')['autoIncrement'], 'id should be autoincrement');
}


/**
* Test describing a table containing defaults with Postgres
*
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -2004,10 +2004,10 @@ public function testSaveNewErrorCompositeKeyNoIncrement()
public function testSaveNewCompositeKeyIncrement()
{
$articles = TableRegistry::get('SiteAuthors');
$article = $articles->newEntity(['site_id' => 1, 'name' => 'new guy']);
$article = $articles->newEntity(['site_id' => 3, 'name' => 'new guy']);
$this->assertSame($article, $articles->save($article));
$this->assertNotEmpty($article->id, 'Primary key should have been populated');
$this->assertSame(1, $article->site_id);
$this->assertSame(3, $article->site_id);
}

/**
Expand Down

0 comments on commit ae1560a

Please sign in to comment.