Commit 306dd803 authored by zYne's avatar zYne

sqlite foreign key support

parent 8428338c
......@@ -144,9 +144,16 @@ class Doctrine_Export extends Doctrine_Connection_Module
}
$queryFields = $this->getFieldDeclarationList($fields);
if (isset($options['foreignKeys']) && ! empty($options['foreignKeys'])) {
foreach($options['foreignKeys'] as $definition) {
$queryFields .= ', ' . $this->getForeignKeyDeclaration($definition);
}
}
if (isset($options['primary']) && ! empty($options['primary'])) {
$queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')';
}
if (isset($options['indexes']) && ! empty($options['indexes'])) {
foreach($options['indexes'] as $index => $definition) {
$queryFields .= ', ' . $this->getIndexDeclaration($index, $definition);
......
......@@ -158,6 +158,13 @@ class Doctrine_Export_Sqlite extends Doctrine_Export
$queryFields.= ', PRIMARY KEY('.implode(', ', array_values($options['primary'])).')';
}
// sqlite doesn't support foreign key declaration but it parses those anyway
if (isset($options['foreignKeys']) && ! empty($options['foreignKeys'])) {
foreach($options['foreignKeys'] as $definition) {
$queryFields .= ', ' . $this->getForeignKeyDeclaration($definition);
}
}
if (isset($options['indexes']) && ! empty($options['indexes'])) {
foreach($options['indexes'] as $index => $definition) {
$queryFields .= ', ' . $this->getIndexDeclaration($index, $definition);
......
......@@ -263,16 +263,16 @@ class Doctrine_Export_Mysql_TestCase extends Doctrine_UnitTestCase
public function testExportSupportsForeignKeys()
{
$r = new MysqlForeignKeyTest;
$r = new ForeignKeyTest;
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE mysql_foreign_key_test (id BIGINT AUTO_INCREMENT, name TEXT, code INT, content TEXT, parent_id BIGINT, FOREIGN KEY parent_id REFERENCES mysql_foreign_key_test(id), FOREIGN KEY id REFERENCES mysql_foreign_key_test(parent_id) ON UPDATE RESTRICT ON DELETE CASCADE, PRIMARY KEY(id)) ENGINE = INNODB');
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE foreign_key_test (id BIGINT AUTO_INCREMENT, name TEXT, code INT, content TEXT, parent_id BIGINT, FOREIGN KEY parent_id REFERENCES foreign_key_test(id), FOREIGN KEY id REFERENCES foreign_key_test(parent_id) ON UPDATE RESTRICT ON DELETE CASCADE, PRIMARY KEY(id)) ENGINE = INNODB');
}
public function testExportSupportsForeignKeysWithoutAttributes()
{
$r = new MysqlForeignKeyTest2;
$r = new ForeignKeyTest2;
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE mysql_foreign_key_test2 (id BIGINT AUTO_INCREMENT, name TEXT, foreignkey BIGINT, FOREIGN KEY foreignkey REFERENCES mysql_foreign_key_test(id), PRIMARY KEY(id)) ENGINE = INNODB');
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE foreign_key_test2 (id BIGINT AUTO_INCREMENT, name TEXT, foreignkey BIGINT, FOREIGN KEY foreignkey REFERENCES foreign_key_test(id), PRIMARY KEY(id)) ENGINE = INNODB');
}
public function testExportSupportsForeignKeysForManyToManyRelations()
......@@ -290,7 +290,7 @@ class Doctrine_Export_Mysql_TestCase extends Doctrine_UnitTestCase
}
}
class MysqlForeignKeyTest extends Doctrine_Record
class ForeignKeyTest extends Doctrine_Record
{
public function setTableDefinition()
{
......@@ -299,12 +299,12 @@ class MysqlForeignKeyTest extends Doctrine_Record
$this->hasColumn('content', 'string', 4000);
$this->hasColumn('parent_id', 'integer');
$this->hasOne('MysqlForeignKeyTest as Parent',
'MysqlForeignKeyTest.parent_id'
$this->hasOne('ForeignKeyTest as Parent',
'ForeignKeyTest.parent_id'
);
$this->hasMany('MysqlForeignKeyTest as Children',
'MysqlForeignKeyTest.parent_id',
$this->hasMany('ForeignKeyTest as Children',
'ForeignKeyTest.parent_id',
array('onDelete' => 'CASCADE',
'onUpdate' => 'RESTRICT')
);
......@@ -339,14 +339,14 @@ class MysqlGroup extends Doctrine_Record
$this->hasMany('MysqlUser', 'MysqlGroupMember.user_id');
}
}
class MysqlForeignKeyTest2 extends Doctrine_Record
class ForeignKeyTest2 extends Doctrine_Record
{
public function setTableDefinition()
{
$this->hasColumn('name', 'string', null);
$this->hasColumn('foreignkey', 'integer');
$this->hasOne('MysqlForeignKeyTest', 'MysqlForeignKeyTest2.foreignkey');
$this->hasOne('ForeignKeyTest', 'ForeignKeyTest2.foreignkey');
}
}
class MysqlIndexTestRecord extends Doctrine_Record
......
......@@ -91,5 +91,11 @@ class Doctrine_Export_Sqlite_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE sometable (id INTEGER UNSIGNED PRIMARY KEY AUTOINCREMENT, name VARCHAR(4), INDEX myindex (id ASC, name DESC))');
}
public function testExportSupportsForeignKeys()
{
$r = new ForeignKeyTest;
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE foreign_key_test (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(2147483647), code INTEGER, content VARCHAR(4000), parent_id INTEGER, FOREIGN KEY parent_id REFERENCES foreign_key_test(id), FOREIGN KEY id REFERENCES foreign_key_test(parent_id) ON UPDATE RESTRICT ON DELETE CASCADE)');
}
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment