Skip to content

Commit

Permalink
Add foreign key reflection to sqlite.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jun 6, 2013
1 parent 59685a7 commit aad301a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/Cake/Database/Schema/Collection.php
Expand Up @@ -104,7 +104,7 @@ public function describe($name) {
);
$statement = $this->_executeSql($sql, $params);
foreach ($statement->fetchAll('assoc') as $row) {
$this->_dialect->convertForeignKeyDescription($table, $row);
$this->_dialect->convertForeignKey($table, $row);
}
return $table;
}
Expand Down
30 changes: 29 additions & 1 deletion lib/Cake/Database/Schema/SqliteSchema.php
Expand Up @@ -195,7 +195,8 @@ public function convertIndexDescription(Table $table, $row) {
* @return array List of sql, params
*/
public function describeForeignKeySql($table) {
return ['', []];
$sql = sprintf('PRAGMA foreign_key_list(%s)', $this->_driver->quoteIdentifier($table));
return [$sql, []];
}

/**
Expand All @@ -206,6 +207,33 @@ public function describeForeignKeySql($table) {
* @return void
*/
public function convertForeignKey(Table $table, $row) {
$data = [
'type' => Table::CONSTRAINT_FOREIGN,
'columns' => [$row['from']],
'references' => [$row['table'], $row['to']],
'update' => $this->_convertOnClause($row['on_update']),
'delete' => $this->_convertOnClause($row['on_delete']),
];
$name = $row['from'] . '_fk';
$table->addConstraint($name, $data);
}

/**
* Convert Sqlite on clauses to the abstract ones.
*
* @param string $clause
* @return string|null
*/
protected function _convertOnClause($clause) {
if ($clause === 'CASCADE' || $clause === 'RESTRICT') {
return strtolower($clause);
}
if ($clause === 'NO ACTION') {
return 'none';
}
if ($clause === 'SET NULL') {
return null;
}
}

/**
Expand Down
16 changes: 14 additions & 2 deletions lib/Cake/Test/TestCase/Database/Schema/SqliteSchemaTest.php
Expand Up @@ -151,7 +151,7 @@ protected function _createTables($connection) {
published BOOLEAN DEFAULT 0,
created DATETIME,
CONSTRAINT "title_idx" UNIQUE ("title", "body")
CONSTRAINT "author_idx" FOREIGN KEY ("author_id") REFERENCES "schema_authors" ("id") ON UPDATE CASCADE ON DELETE CASCADE
CONSTRAINT "author_idx" FOREIGN KEY ("author_id") REFERENCES "schema_authors" ("id") ON UPDATE CASCADE ON DELETE RESTRICT
);
SQL;
$connection->execute($table);
Expand Down Expand Up @@ -273,14 +273,26 @@ public function testDescribeTableIndexes() {
'type' => 'unique',
'columns' => ['title', 'body'],
'length' => []
],
'author_id_fk' => [
'type' => 'foreign',
'columns' => ['author_id'],
'references' => ['schema_authors', 'id'],
'length' => [],
'update' => 'cascade',
'delete' => 'restrict',
]
];
$this->assertCount(2, $result->constraints());
$this->assertCount(3, $result->constraints());
$this->assertEquals($expected['primary'], $result->constraint('primary'));
$this->assertEquals(
$expected['sqlite_autoindex_schema_articles_1'],
$result->constraint('sqlite_autoindex_schema_articles_1')
);
$this->assertEquals(
$expected['author_id_fk'],
$result->constraint('author_id_fk')
);

$this->assertCount(1, $result->indexes());
$expected = [
Expand Down

0 comments on commit aad301a

Please sign in to comment.