Skip to content
Permalink
Browse files

Add indexSql method to SqliteSchema.

Add the method and some tests.
  • Loading branch information...
markstory committed May 14, 2013
1 parent b9e92de commit b2b9f29a6e7a8b25a0f8f6c28d70efc0c974b551
@@ -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) . ')';
}
/**
@@ -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
*

0 comments on commit b2b9f29

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