SqliteSchemaManagerTest.php 7.74 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\Tests\DBAL\Functional\Schema;

use Doctrine\DBAL\Schema;
6
use Doctrine\DBAL\Types\Type;
7 8

require_once __DIR__ . '/../../../TestInit.php';
9

romanb's avatar
romanb committed
10
class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase
11
{
romanb's avatar
romanb committed
12
    /**
13
     * SQLITE does not support databases.
14 15
     *
     * @expectedException \Doctrine\DBAL\DBALException
romanb's avatar
romanb committed
16
     */
17 18
    public function testListDatabases()
    {
romanb's avatar
romanb committed
19
        $this->_sm->listDatabases();
20 21 22 23 24 25
    }

    public function testCreateAndDropDatabase()
    {
        $path = dirname(__FILE__).'/test_create_and_drop_sqlite_database.sqlite';

jwage's avatar
jwage committed
26
        $this->_sm->createDatabase($path);
27
        $this->assertEquals(true, file_exists($path));
jwage's avatar
jwage committed
28
        $this->_sm->dropDatabase($path);
29
        $this->assertEquals(false, file_exists($path));
jwage's avatar
jwage committed
30
    }
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    /**
     * @group DBAL-1220
     */
    public function testDropsDatabaseWithActiveConnections()
    {
        $this->_sm->dropAndCreateDatabase('test_drop_database');

        $this->assertFileExists('test_drop_database');

        $params = $this->_conn->getParams();
        $params['dbname'] = 'test_drop_database';

        $user = isset($params['user']) ? $params['user'] : null;
        $password = isset($params['password']) ? $params['password'] : null;

        $connection = $this->_conn->getDriver()->connect($params, $user, $password);

        $this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $connection);

        $this->_sm->dropDatabase('test_drop_database');

        $this->assertFileNotExists('test_drop_database');

        unset($connection);
    }

58 59
    public function testRenameTable()
    {
60
        $this->createTestTable('oldname');
romanb's avatar
romanb committed
61
        $this->_sm->renameTable('oldname', 'newname');
62 63 64 65

        $tables = $this->_sm->listTableNames();
        $this->assertContains('newname', $tables);
        $this->assertNotContains('oldname', $tables);
66
    }
67

68
    public function createListTableColumns()
69
    {
70 71 72 73
        $table = parent::createListTableColumns();
        $table->getColumn('id')->setAutoincrement(true);

        return $table;
74
    }
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    public function testListForeignKeysFromExistingDatabase()
    {
        $this->_conn->executeQuery(<<<EOS
CREATE TABLE user (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    page INTEGER CONSTRAINT FK_1 REFERENCES page (key) DEFERRABLE INITIALLY DEFERRED,
    parent INTEGER REFERENCES user(id) ON DELETE CASCADE,
    log INTEGER,
    CONSTRAINT FK_3 FOREIGN KEY (log) REFERENCES log ON UPDATE SET NULL NOT DEFERRABLE
)
EOS
        );

        $expected = array(
90
            new Schema\ForeignKeyConstraint(array('log'), 'log', array(null), 'FK_3',
91
                array('onUpdate' => 'SET NULL', 'onDelete' => 'NO ACTION', 'deferrable' => false, 'deferred' => false)),
92
            new Schema\ForeignKeyConstraint(array('parent'), 'user', array('id'), '1',
93
                array('onUpdate' => 'NO ACTION', 'onDelete' => 'CASCADE', 'deferrable' => false, 'deferred' => false)),
94
            new Schema\ForeignKeyConstraint(array('page'), 'page', array('key'), 'FK_1',
95 96 97 98 99
                array('onUpdate' => 'NO ACTION', 'onDelete' => 'NO ACTION', 'deferrable' => true, 'deferred' => true)),
        );

        $this->assertEquals($expected, $this->_sm->listTableForeignKeys('user'));
    }
Steve Müller's avatar
Steve Müller committed
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    public function testColumnCollation()
    {
        $table = new Schema\Table('test_collation');
        $table->addColumn('id', 'integer');
        $table->addColumn('text', 'text');
        $table->addColumn('foo', 'text')->setPlatformOption('collation', 'BINARY');
        $table->addColumn('bar', 'text')->setPlatformOption('collation', 'NOCASE');
        $this->_sm->dropAndCreateTable($table);

        $columns = $this->_sm->listTableColumns('test_collation');

        $this->assertArrayNotHasKey('collation', $columns['id']->getPlatformOptions());
        $this->assertEquals('BINARY', $columns['text']->getPlatformOption('collation'));
        $this->assertEquals('BINARY', $columns['foo']->getPlatformOption('collation'));
        $this->assertEquals('NOCASE', $columns['bar']->getPlatformOption('collation'));
    }

Steve Müller's avatar
Steve Müller committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    public function testListTableWithBinary()
    {
        $tableName = 'test_binary_table';

        $table = new \Doctrine\DBAL\Schema\Table($tableName);
        $table->addColumn('id', 'integer');
        $table->addColumn('column_varbinary', 'binary', array());
        $table->addColumn('column_binary', 'binary', array('fixed' => true));
        $table->setPrimaryKey(array('id'));

        $this->_sm->createTable($table);

        $table = $this->_sm->listTableDetails($tableName);

        $this->assertInstanceOf('Doctrine\DBAL\Types\BlobType', $table->getColumn('column_varbinary')->getType());
        $this->assertFalse($table->getColumn('column_varbinary')->getFixed());

        $this->assertInstanceOf('Doctrine\DBAL\Types\BlobType', $table->getColumn('column_binary')->getType());
        $this->assertFalse($table->getColumn('column_binary')->getFixed());
    }
138 139 140

    public function testNonDefaultPKOrder()
    {
141 142 143 144
        $version = \SQLite3::version();
        if(version_compare($version['versionString'], '3.7.16', '<')) {
            $this->markTestSkipped('This version of sqlite doesn\'t return the order of the Primary Key.');
        }
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        $this->_conn->executeQuery(<<<EOS
CREATE TABLE non_default_pk_order (
    id INTEGER,
    other_id INTEGER,
    PRIMARY KEY(other_id, id)
)
EOS
        );

        $tableIndexes = $this->_sm->listTableIndexes('non_default_pk_order');

         $this->assertEquals(1, count($tableIndexes));

        $this->assertArrayHasKey('primary', $tableIndexes, 'listTableIndexes() has to return a "primary" array key.');
        $this->assertEquals(array('other_id', 'id'), array_map('strtolower', $tableIndexes['primary']->getColumns()));
    }
161

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    /**
     * @group DBAL-1779
     */
    public function testListTableColumnsWithWhitespacesInTypeDeclarations()
    {
        $sql = <<<SQL
CREATE TABLE dbal_1779 (
    foo VARCHAR (64) ,
    bar TEXT (100)
)
SQL;

        $this->_conn->executeQuery($sql);

        $columns = $this->_sm->listTableColumns('dbal_1779');

        $this->assertCount(2, $columns);

        $this->assertArrayHasKey('foo', $columns);
        $this->assertArrayHasKey('bar', $columns);

        $this->assertSame(Type::getType(Type::STRING), $columns['foo']->getType());
        $this->assertSame(Type::getType(Type::TEXT), $columns['bar']->getType());

        $this->assertSame(64, $columns['foo']->getLength());
        $this->assertSame(100, $columns['bar']->getLength());
    }

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    /**
     * @dataProvider getDiffListIntegerAutoincrementTableColumnsData
     * @group DBAL-924
     */
    public function testDiffListIntegerAutoincrementTableColumns($integerType, $unsigned, $expectedComparatorDiff)
    {
        $tableName = 'test_int_autoincrement_table';

        $offlineTable = new \Doctrine\DBAL\Schema\Table($tableName);
        $offlineTable->addColumn('id', $integerType, array('autoincrement' => true, 'unsigned' => $unsigned));
        $offlineTable->setPrimaryKey(array('id'));

        $this->_sm->dropAndCreateTable($offlineTable);

        $onlineTable = $this->_sm->listTableDetails($tableName);
        $comparator = new Schema\Comparator();
        $diff = $comparator->diffTable($offlineTable, $onlineTable);

        if ($expectedComparatorDiff) {
            $this->assertEmpty($this->_sm->getDatabasePlatform()->getAlterTableSQL($diff));
        } else {
            $this->assertFalse($diff);
        }
    }

    /**
     * @return array
     */
    public function getDiffListIntegerAutoincrementTableColumnsData()
    {
        return array(
            array('smallint', false, true),
            array('smallint', true, true),
            array('integer', false, false),
            array('integer', true, true),
            array('bigint', false, true),
            array('bigint', true, true),
        );
    }
229
}