Skip to content

Commit

Permalink
Add indexSql method to SqliteSchema.
Browse files Browse the repository at this point in the history
Add the method and some tests.
  • Loading branch information
markstory committed May 14, 2013
1 parent b9e92de commit b2b9f29
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/Cake/Database/Schema/SqliteSchema.php
Expand Up @@ -202,6 +202,19 @@ public function columnSql(Table $table, $name) {
*/
public function indexSql(Table $table, $name) {
$data = $table->index($name);
$out = 'CONSTRAINT ';
$out .= $this->_driver->quoteIdentifier($name);
if ($data['type'] === Table::INDEX_PRIMARY) {
$out .= ' PRIMARY KEY';
}
if ($data['type'] === Table::INDEX_UNIQUE) {
$out .= ' UNIQUE';
}
$columns = array_map(
[$this->_driver, 'quoteIdentifier'],
$data['columns']
);
return $out . ' (' . implode(', ', $columns) . ')';
}

/**
Expand Down
39 changes: 39 additions & 0 deletions lib/Cake/Test/TestCase/Database/Schema/SqliteSchemaTest.php
Expand Up @@ -376,6 +376,45 @@ public function testColumnSql($name, $data, $expected) {
$this->assertEquals($expected, $schema->columnSql($table, $name));
}

/**
* Provide data for testing indexSql
*
* @return array
*/
public static function indexSqlProvider() {
return [
[
'primary',
['type' => 'primary', 'columns' => ['title']],
'CONSTRAINT "primary" PRIMARY KEY ("title")'
],
[
'unique_idx',
['type' => 'unique', 'columns' => ['title', 'author_id']],
'CONSTRAINT "unique_idx" UNIQUE ("title", "author_id")'
],
];
}

/**
* Test the indexSql method.
*
* @dataProvider indexSqlProvider
*/
public function testIndexSql($name, $data, $expected) {
$driver = $this->_getMockedDriver();
$schema = new SqliteSchema($driver);

$table = (new Table('articles'))->addColumn('title', [
'type' => 'string',
'length' => 255
])->addColumn('author_id', [
'type' => 'integer',
])->addIndex($name, $data);

$this->assertEquals($expected, $schema->indexSql($table, $name));
}

/**
* Get a schema instance with a mocked driver/pdo instances
*
Expand Down

0 comments on commit b2b9f29

Please sign in to comment.