Skip to content

Commit

Permalink
Add 'autoIncrement' support to MySQL.
Browse files Browse the repository at this point in the history
Add reflection and generation support for the autoIncrement property in
MySQL. This helps improve composite primary key support as composite
keys with one AUTO_INCREMENT column can now be created.
  • Loading branch information
markstory committed Jan 2, 2014
1 parent 32c3f5f commit a288c0e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Cake/Database/Schema/MysqlSchema.php
Expand Up @@ -119,7 +119,6 @@ protected function _convertColumn($column) {

/**
* {@inheritdoc}
*
*/
public function convertFieldDescription(Table $table, $row) {
$field = $this->_convertColumn($row['Type']);
Expand All @@ -129,12 +128,14 @@ public function convertFieldDescription(Table $table, $row) {
'collate' => $row['Collation'],
'comment' => $row['Comment'],
];
if (isset($row['Extra']) && $row['Extra'] === 'auto_increment') {
$field['autoIncrement'] = true;
}
$table->addColumn($row['Field'], $field);
}

/**
* {@inheritdoc}
*
*/
public function convertIndexDescription(Table $table, $row) {
$type = null;
Expand Down Expand Up @@ -306,7 +307,10 @@ public function columnSql(Table $table, $name) {
if (isset($data['null']) && $data['null'] === false) {
$out .= ' NOT NULL';
}
if (in_array($data['type'], ['integer', 'biginteger']) && in_array($name, (array)$table->primaryKey())) {
if (
in_array($data['type'], ['integer', 'biginteger']) &&
([$name] == (array)$table->primaryKey() || $data['autoIncrement'] === true)
) {
$out .= ' AUTO_INCREMENT';
}
if (isset($data['null']) && $data['null'] === true) {
Expand Down
65 changes: 65 additions & 0 deletions Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -249,6 +249,7 @@ public function testDescribeTable() {
'length' => 20,
'precision' => null,
'comment' => null,
'autoIncrement' => true,
],
'title' => [
'type' => 'string',
Expand All @@ -275,6 +276,7 @@ public function testDescribeTable() {
'length' => 11,
'precision' => null,
'comment' => null,
'autoIncrement' => null,
],
'published' => [
'type' => 'boolean',
Expand Down Expand Up @@ -710,6 +712,69 @@ public function testCreateSql() {
$this->assertEquals($expected, $result[0]);
}

/**
* Test primary key generation & auto-increment.
*
* @return void
*/
public function testCreateSqlCompositeIntegerKey() {
$driver = $this->_getMockedDriver();
$connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
$connection->expects($this->any())->method('driver')
->will($this->returnValue($driver));

$table = (new Table('articles_tags'))
->addColumn('article_id', [
'type' => 'integer',
'null' => false
])
->addColumn('tag_id', [
'type' => 'integer',
'null' => false,
])
->addConstraint('primary', [
'type' => 'primary',
'columns' => ['article_id', 'tag_id']
]);

$expected = <<<SQL
CREATE TABLE `articles_tags` (
`article_id` INTEGER NOT NULL,
`tag_id` INTEGER NOT NULL,
PRIMARY KEY (`article_id`, `tag_id`)
)
SQL;
$result = $table->createSql($connection);
$this->assertCount(1, $result);
$this->assertEquals($expected, $result[0]);

$table = (new Table('composite_key'))
->addColumn('id', [
'type' => 'integer',
'null' => false,
'autoIncrement' => true
])
->addColumn('account_id', [
'type' => 'integer',
'null' => false,
])
->addConstraint('primary', [
'type' => 'primary',
'columns' => ['id', 'account_id']
]);

$expected = <<<SQL
CREATE TABLE `composite_key` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`account_id` INTEGER NOT NULL,
PRIMARY KEY (`id`, `account_id`)
)
SQL;
$result = $table->createSql($connection);
$this->assertCount(1, $result);
$this->assertEquals($expected, $result[0]);
}

/**
* test dropSql
*
Expand Down

0 comments on commit a288c0e

Please sign in to comment.