Several bugfixes for the export module (expecially pgsql).
This commit is contained in:
parent
6348e5f709
commit
75dbc8c841
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -283,5 +283,68 @@ class Doctrine_Export_Pgsql extends Doctrine_Export
|
||||
return 'DROP SEQUENCE ' . $sequenceName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a table.
|
||||
*
|
||||
* @param unknown_type $name
|
||||
* @param array $fields
|
||||
* @param array $options
|
||||
* @return unknown
|
||||
*/
|
||||
public function createTableSql($name, array $fields, array $options = array())
|
||||
{
|
||||
if ( ! $name) {
|
||||
throw new Doctrine_Export_Exception('no valid table name specified');
|
||||
}
|
||||
|
||||
if (empty($fields)) {
|
||||
throw new Doctrine_Export_Exception('no fields specified for table ' . $name);
|
||||
}
|
||||
|
||||
$queryFields = $this->getFieldDeclarationList($fields);
|
||||
|
||||
|
||||
if (isset($options['primary']) && ! empty($options['primary'])) {
|
||||
$queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')';
|
||||
}
|
||||
|
||||
$name = $this->conn->quoteIdentifier($name, true);
|
||||
$query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
|
||||
|
||||
$sql[] = $query;
|
||||
|
||||
if (isset($options['indexes']) && ! empty($options['indexes'])) {
|
||||
foreach($options['indexes'] as $index => $definition) {
|
||||
$sql[] = $this->createIndexSql($name, $index, $definition);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($options['foreignKeys'])) {
|
||||
|
||||
foreach ((array) $options['foreignKeys'] as $k => $definition) {
|
||||
if (is_array($definition)) {
|
||||
$sql[] = $this->createForeignKeySql($name, $definition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* createForeignKeySql
|
||||
*
|
||||
* @param string $table name of the table on which the foreign key is to be created
|
||||
* @param array $definition associative array that defines properties of the foreign key to be created.
|
||||
* @return string
|
||||
*/
|
||||
public function createForeignKeySql($table, array $definition)
|
||||
{
|
||||
$table = $this->conn->quoteIdentifier($table);
|
||||
|
||||
$query = 'ALTER TABLE ' . $table . ' ADD ' . $this->getForeignKeyDeclaration($definition);
|
||||
|
||||
return $query;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,98 +1,98 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Doctrine_Export_Mysql_TestCase
|
||||
*
|
||||
* @package Doctrine
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
*/
|
||||
class Doctrine_Export_Pgsql_TestCase extends Doctrine_UnitTestCase
|
||||
{
|
||||
public function testCreateDatabaseExecutesSql()
|
||||
{
|
||||
$this->export->createDatabase('db');
|
||||
$this->assertEqual($this->adapter->pop(), 'CREATE DATABASE db');
|
||||
}
|
||||
public function testDropDatabaseExecutesSql()
|
||||
{
|
||||
$this->export->dropDatabase('db');
|
||||
|
||||
$this->assertEqual($this->adapter->pop(), 'DROP DATABASE db');
|
||||
}
|
||||
public function testCreateTableSupportsAutoincPks()
|
||||
{
|
||||
$name = 'mytable';
|
||||
|
||||
$fields = array('id' => array('type' => 'integer', 'unsigned' => 1, 'autoincrement' => true));
|
||||
$options = array('primary' => array('id'));
|
||||
|
||||
$this->export->createTable($name, $fields, $options);
|
||||
|
||||
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (id SERIAL, PRIMARY KEY(id))');
|
||||
}
|
||||
public function testCreateTableSupportsDefaultAttribute()
|
||||
{
|
||||
$name = 'mytable';
|
||||
$fields = array('name' => array('type' => 'char', 'length' => 10, 'default' => 'def'),
|
||||
'type' => array('type' => 'integer', 'length' => 3, 'default' => 12)
|
||||
);
|
||||
|
||||
$options = array('primary' => array('name', 'type'));
|
||||
$this->export->createTable($name, $fields, $options);
|
||||
|
||||
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (name CHAR(10) DEFAULT \'def\', type INT DEFAULT 12, PRIMARY KEY(name, type))');
|
||||
}
|
||||
public function testCreateTableSupportsMultiplePks()
|
||||
{
|
||||
$name = 'mytable';
|
||||
$fields = array('name' => array('type' => 'char', 'length' => 10),
|
||||
'type' => array('type' => 'integer', 'length' => 3));
|
||||
|
||||
$options = array('primary' => array('name', 'type'));
|
||||
$this->export->createTable($name, $fields, $options);
|
||||
|
||||
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (name CHAR(10), type INT, PRIMARY KEY(name, type))');
|
||||
}
|
||||
public function testExportSql()
|
||||
{
|
||||
$sql = $this->export->exportSql(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
|
||||
|
||||
$this->assertEqual($sql, array ( 0 => 'CREATE TABLE foo_reference (foo1 BIGINT, foo2 BIGINT, PRIMARY KEY(foo1, foo2))',
|
||||
1 => 'CREATE TABLE foo_locally_owned (id BIGSERIAL, name VARCHAR(200), PRIMARY KEY(id))',
|
||||
2 => 'CREATE TABLE foo_foreignly_owned_with_pk (id BIGSERIAL, name VARCHAR(200), PRIMARY KEY(id))',
|
||||
3 => 'CREATE TABLE foo_foreignly_owned (id BIGSERIAL, name VARCHAR(200), fooid BIGINT, PRIMARY KEY(id))',
|
||||
4 => 'CREATE TABLE foo_bar_record (fooid BIGINT, barid BIGINT, PRIMARY KEY(fooid, barid))',
|
||||
5 => 'CREATE TABLE foo (id BIGSERIAL, name VARCHAR(200) NOT NULL, parent_id BIGINT, local_foo BIGINT, PRIMARY KEY(id))',
|
||||
6 => 'CREATE TABLE bar (id BIGSERIAL, name VARCHAR(200), PRIMARY KEY(id))',
|
||||
7 => 'ALTER TABLE foo_reference ADD CONSTRAINT FOREIGN KEY (foo1) REFERENCES foo(foo1, foo2) NOT DEFERRABLE INITIALLY IMMEDIATE',
|
||||
8 => 'ALTER TABLE foo_bar_record ADD CONSTRAINT FOREIGN KEY (fooId) REFERENCES foo(fooid, barid) NOT DEFERRABLE INITIALLY IMMEDIATE',
|
||||
9 => 'ALTER TABLE foo ADD CONSTRAINT FOREIGN KEY (parent_id) REFERENCES foo(id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE',
|
||||
10 => 'ALTER TABLE foo ADD CONSTRAINT FOREIGN KEY (local_foo) REFERENCES foo_locally_owned(id) ON DELETE RESTRICT NOT DEFERRABLE INITIALLY IMMEDIATE',
|
||||
));
|
||||
}
|
||||
}
|
||||
?>
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Doctrine_Export_Mysql_TestCase
|
||||
*
|
||||
* @package Doctrine
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
*/
|
||||
class Doctrine_Export_Pgsql_TestCase extends Doctrine_UnitTestCase
|
||||
{
|
||||
public function testCreateDatabaseExecutesSql()
|
||||
{
|
||||
$this->export->createDatabase('db');
|
||||
$this->assertEqual($this->adapter->pop(), 'CREATE DATABASE db');
|
||||
}
|
||||
public function testDropDatabaseExecutesSql()
|
||||
{
|
||||
$this->export->dropDatabase('db');
|
||||
|
||||
$this->assertEqual($this->adapter->pop(), 'DROP DATABASE db');
|
||||
}
|
||||
public function testCreateTableSupportsAutoincPks()
|
||||
{
|
||||
$name = 'mytable';
|
||||
|
||||
$fields = array('id' => array('type' => 'integer', 'unsigned' => 1, 'autoincrement' => true));
|
||||
$options = array('primary' => array('id'));
|
||||
|
||||
$this->export->createTable($name, $fields, $options);
|
||||
|
||||
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (id SERIAL, PRIMARY KEY(id))');
|
||||
}
|
||||
public function testCreateTableSupportsDefaultAttribute()
|
||||
{
|
||||
$name = 'mytable';
|
||||
$fields = array('name' => array('type' => 'char', 'length' => 10, 'default' => 'def'),
|
||||
'type' => array('type' => 'integer', 'length' => 3, 'default' => 12)
|
||||
);
|
||||
|
||||
$options = array('primary' => array('name', 'type'));
|
||||
$this->export->createTable($name, $fields, $options);
|
||||
|
||||
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (name CHAR(10) DEFAULT \'def\', type INT DEFAULT 12, PRIMARY KEY(name, type))');
|
||||
}
|
||||
public function testCreateTableSupportsMultiplePks()
|
||||
{
|
||||
$name = 'mytable';
|
||||
$fields = array('name' => array('type' => 'char', 'length' => 10),
|
||||
'type' => array('type' => 'integer', 'length' => 3));
|
||||
|
||||
$options = array('primary' => array('name', 'type'));
|
||||
$this->export->createTable($name, $fields, $options);
|
||||
|
||||
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (name CHAR(10), type INT, PRIMARY KEY(name, type))');
|
||||
}
|
||||
public function testExportSql()
|
||||
{
|
||||
$sql = $this->export->exportSql(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
|
||||
|
||||
$this->assertEqual($sql, array ( 0 => 'CREATE TABLE foo_reference (foo1 BIGINT, foo2 BIGINT, PRIMARY KEY(foo1, foo2))',
|
||||
1 => 'CREATE TABLE foo_locally_owned (id BIGSERIAL, name VARCHAR(200), PRIMARY KEY(id))',
|
||||
2 => 'CREATE TABLE foo_foreignly_owned_with_pk (id BIGSERIAL, name VARCHAR(200), PRIMARY KEY(id))',
|
||||
3 => 'CREATE TABLE foo_foreignly_owned (id BIGSERIAL, name VARCHAR(200), fooid BIGINT, PRIMARY KEY(id))',
|
||||
4 => 'CREATE TABLE foo_bar_record (fooid BIGINT, barid BIGINT, PRIMARY KEY(fooid, barid))',
|
||||
5 => 'CREATE TABLE foo (id BIGSERIAL, name VARCHAR(200) NOT NULL, parent_id BIGINT, local_foo BIGINT, PRIMARY KEY(id))',
|
||||
6 => 'CREATE TABLE bar (id BIGSERIAL, name VARCHAR(200), PRIMARY KEY(id))',
|
||||
7 => 'ALTER TABLE foo_reference ADD FOREIGN KEY (foo1) REFERENCES foo(id) NOT DEFERRABLE INITIALLY IMMEDIATE',
|
||||
8 => 'ALTER TABLE foo_bar_record ADD FOREIGN KEY (fooId) REFERENCES foo(id) NOT DEFERRABLE INITIALLY IMMEDIATE',
|
||||
9 => 'ALTER TABLE foo ADD FOREIGN KEY (parent_id) REFERENCES foo(id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE',
|
||||
10 => 'ALTER TABLE foo ADD FOREIGN KEY (local_foo) REFERENCES foo_locally_owned(id) ON DELETE RESTRICT NOT DEFERRABLE INITIALLY IMMEDIATE',
|
||||
));
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@ -49,6 +49,14 @@ class Doctrine_NestedSet_SingleRoot_TestCase extends Doctrine_UnitTestCase
|
||||
$node2->name = 'node2';
|
||||
$node2->getNode()->insertAsLastChildOf($node);
|
||||
}
|
||||
|
||||
public function testLftRgtValues()
|
||||
{
|
||||
$treeMngr = $this->conn->getTable('NestedSetTest_SingleRootNode')->getTree();
|
||||
$root = $treeMngr->fetchRoot();
|
||||
$this->assertEqual(1, $root['lft']);
|
||||
$this->assertEqual(4, $root['rgt']);
|
||||
}
|
||||
|
||||
public function testGetDescendants()
|
||||
{
|
||||
|
@ -336,7 +336,7 @@ $test->addTestCase(new Doctrine_Search_TestCase());
|
||||
|
||||
//$test->addTestCase(new Doctrine_AuditLog_TestCase());
|
||||
|
||||
//$test->addTestCase(new Doctrine_NestedSet_SingleRoot_TestCase());
|
||||
$test->addTestCase(new Doctrine_NestedSet_SingleRoot_TestCase());
|
||||
|
||||
// Cache tests
|
||||
//$test->addTestCase(new Doctrine_Cache_Query_SqliteTestCase());
|
||||
|
Loading…
x
Reference in New Issue
Block a user