Skip to content

Commit

Permalink
Fix missing AUTO_INCREMENT features.
Browse files Browse the repository at this point in the history
I missed this the first time.
  • Loading branch information
markstory committed May 7, 2013
1 parent 0461394 commit acbb923
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
8 changes: 6 additions & 2 deletions lib/Cake/Database/Schema/MysqlSchema.php
Expand Up @@ -175,11 +175,12 @@ public function createTableSql($table, $lines) {
/**
* Generate the SQL fragment for a single column in MySQL
*
* @param Cake\Database\Schema\Table $table The table object the column is in.
* @param string $name The name of the column.
* @param array $data The attributes for the column.
* @return string SQL fragment.
*/
public function columnSql($name, $data) {
public function columnSql(Table $table, $name) {
$data = $table->column($name);
$out = $this->_driver->quoteIdentifier($name);
switch ($data['type']) {
case 'string':
Expand Down Expand Up @@ -216,6 +217,9 @@ public function columnSql($name, $data) {
if (isset($data['null']) && $data['null'] === false) {
$out .= ' NOT NULL';
}
if (in_array($data['type'], ['integer', 'biginteger']) && in_array($name, (array)$table->primaryKey())) {
$out .= ' AUTO_INCREMENT';
}
if (isset($data['null']) && $data['null'] === true) {
$out .= $data['type'] === 'timestamp' ? ' NULL' : ' DEFAULT NULL';
unset($data['default']);
Expand Down
8 changes: 4 additions & 4 deletions lib/Cake/Database/Schema/Table.php
Expand Up @@ -251,11 +251,11 @@ public function primaryKey() {
public function createTableSql(Connection $connection) {
$dialect = $connection->driver()->schemaDialect();
$lines = [];
foreach ($this->_columns as $name => $data) {
$lines[] = $dialect->columnSql($name, $data);
foreach (array_keys($this->_columns) as $name) {
$lines[] = $dialect->columnSql($this, $name);
}
foreach ($this->_indexes as $name => $data) {
$lines[] = $dialect->indexSql($name, $data);
foreach (array_keys($this->_indexes) as $name) {
$lines[] = $dialect->indexSql($this, $name);
}
return $dialect->createTableSql($this->_table, $lines);
}
Expand Down
61 changes: 51 additions & 10 deletions lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -351,18 +351,43 @@ public static function columnSqlProvider() {
* @return void
*/
public function testColumnSql($name, $data, $expected) {
$driver = new \Cake\Database\Driver\Mysql();
$schema = $this->_getMockedSchema();

$mock = $this->getMock('FakePdo', ['quote']);
$mock->expects($this->any())
->method('quote')
->will($this->returnCallback(function ($value) {
return '"' . $value . '"';
}));
$table = (new Table('articles'))->addColumn($name, $data);
$this->assertEquals($expected, $schema->columnSql($table, $name));
}

$driver->connection($mock);
$dialect = new MysqlSchema($driver);
$this->assertEquals($expected, $dialect->columnSql($name, $data));
/**
* Test generating a column that is a primary key.
*
* @return void
*/
public function testColumnSqlPrimaryKey() {
$schema = $this->_getMockedSchema();

$table = new Table('articles');
$table->addColumn('id', [
'type' => 'integer',
'null' => false
])
->addIndex('primary', [
'type' => 'primary',
'columns' => ['id']
]);
$result = $schema->columnSql($table, 'id');
$this->assertEquals($result, '`id` INTEGER NOT NULL AUTO_INCREMENT');

$table = new Table('articles');
$table->addColumn('id', [
'type' => 'biginteger',
'null' => false
])
->addIndex('primary', [
'type' => 'primary',
'columns' => ['id']
]);
$result = $schema->columnSql($table, 'id');
$this->assertEquals($result, '`id` BIGINT NOT NULL AUTO_INCREMENT');
}

/**
Expand Down Expand Up @@ -416,5 +441,21 @@ public function testCreateTableSql() {
$this->assertEquals($expected, $result);
}

/**
* Get a schema instance with a mocked driver/pdo instances
*
* @return MysqlSchema
*/
protected function _getMockedSchema() {
$driver = new \Cake\Database\Driver\Mysql();
$mock = $this->getMock('FakePdo', ['quote']);
$mock->expects($this->any())
->method('quote')
->will($this->returnCallback(function ($value) {
return '"' . $value . '"';
}));
$driver->connection($mock);
return new MysqlSchema($driver);
}

}

0 comments on commit acbb923

Please sign in to comment.