Skip to content

Commit 29bffc3

Browse files
committed
Add very basic primary key support.
Add basic single and multi column primary key support.
1 parent 5bfa029 commit 29bffc3

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

lib/Cake/Database/Schema/MysqlSchema.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,19 @@ protected function _value($value) {
274274
/**
275275
* Generate the SQL fragment for a single index in MySQL
276276
*
277+
* @param Cake\Database\Schema\Table $table The table object the column is in.
277278
* @param string $name The name of the column.
278-
* @param array $data The attributes for the column.
279279
* @return string SQL fragment.
280280
*/
281-
public function indexSql($name, $data) {
281+
public function indexSql(Table $table, $name) {
282+
$data = $table->index($name);
283+
if ($data['type'] === Table::INDEX_PRIMARY) {
284+
$columns = array_map(
285+
[$this->_driver, 'quoteIdentifier'],
286+
$data['columns']
287+
);
288+
return sprintf('PRIMARY KEY (%s)', implode(', ', $columns));
289+
}
282290
}
283291

284292
}

lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ public static function columnSqlProvider() {
368368
* @return void
369369
*/
370370
public function testColumnSql($name, $data, $expected) {
371-
$schema = $this->_getMockedSchema();
371+
$driver = $this->_getMockedDriver();
372+
$schema = new MysqlSchema($driver);
372373

373374
$table = (new Table('articles'))->addColumn($name, $data);
374375
$this->assertEquals($expected, $schema->columnSql($table, $name));
@@ -380,7 +381,8 @@ public function testColumnSql($name, $data, $expected) {
380381
* @return void
381382
*/
382383
public function testColumnSqlPrimaryKey() {
383-
$schema = $this->_getMockedSchema();
384+
$driver = $this->_getMockedDriver();
385+
$schema = new MysqlSchema($driver);
384386

385387
$table = new Table('articles');
386388
$table->addColumn('id', [
@@ -429,22 +431,11 @@ public function testCreateTableSql() {
429431
'columns' => ['id']
430432
]);
431433

434+
$driver = $this->_getMockedDriver();
432435
$connection = $this->getMock('Cake\Database\Connection', array(), array(), '', false);
433-
$driver = new \Cake\Database\Driver\Mysql();
434-
$mock = $this->getMock('FakePdo', ['quote']);
435-
$driver->connection($mock);
436-
437-
$dialect = new MysqlSchema($driver);
438-
439436
$connection->expects($this->any())->method('driver')
440437
->will($this->returnValue($driver));
441438

442-
$mock->expects($this->any())
443-
->method('quote')
444-
->will($this->returnCallback(function ($value) {
445-
return '"' . $value . '"';
446-
}));
447-
448439
$result = $table->createTableSql($connection);
449440
$expected = <<<SQL
450441
CREATE TABLE `posts` (
@@ -463,16 +454,16 @@ public function testCreateTableSql() {
463454
*
464455
* @return MysqlSchema
465456
*/
466-
protected function _getMockedSchema() {
457+
protected function _getMockedDriver() {
467458
$driver = new \Cake\Database\Driver\Mysql();
468-
$mock = $this->getMock('FakePdo', ['quote']);
459+
$mock = $this->getMock('FakePdo', ['quote', 'quoteIdentifier']);
469460
$mock->expects($this->any())
470461
->method('quote')
471462
->will($this->returnCallback(function ($value) {
472463
return '"' . $value . '"';
473464
}));
474465
$driver->connection($mock);
475-
return new MysqlSchema($driver);
466+
return $driver;
476467
}
477468

478469
}

0 commit comments

Comments
 (0)