Skip to content
Permalink
Browse files

Add 'autoIncrement' support to MySQL.

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 a288c0ef1c49d2fd1e03b4f4c20f6fb85a5d38e4
Showing with 72 additions and 3 deletions.
  1. +7 −3 Cake/Database/Schema/MysqlSchema.php
  2. +65 −0 Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php
@@ -119,7 +119,6 @@ protected function _convertColumn($column) {
/**
* {@inheritdoc}
*
*/
public function convertFieldDescription(Table $table, $row) {
$field = $this->_convertColumn($row['Type']);
@@ -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;
@@ -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) {
@@ -249,6 +249,7 @@ public function testDescribeTable() {
'length' => 20,
'precision' => null,
'comment' => null,
'autoIncrement' => true,
],
'title' => [
'type' => 'string',
@@ -275,6 +276,7 @@ public function testDescribeTable() {
'length' => 11,
'precision' => null,
'comment' => null,
'autoIncrement' => null,
],
'published' => [
'type' => 'boolean',
@@ -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
*

0 comments on commit a288c0e

Please sign in to comment.
You can’t perform that action at this time.