SqliteSchemaManagerTest.php 7.8 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

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

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

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

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
    /**
     * @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);
    }

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

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

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

        return $table;
72
    }
73 74 75

    public function testListForeignKeysFromExistingDatabase()
    {
76
        $this->_conn->exec(<<<EOS
77 78 79 80 81 82 83 84 85 86 87
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(
88
            new Schema\ForeignKeyConstraint(array('log'), 'log', array(null), 'FK_3',
89
                array('onUpdate' => 'SET NULL', 'onDelete' => 'NO ACTION', 'deferrable' => false, 'deferred' => false)),
90
            new Schema\ForeignKeyConstraint(array('parent'), 'user', array('id'), '1',
91
                array('onUpdate' => 'NO ACTION', 'onDelete' => 'CASCADE', 'deferrable' => false, 'deferred' => false)),
92
            new Schema\ForeignKeyConstraint(array('page'), 'page', array('key'), 'FK_1',
93 94 95 96 97
                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
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    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
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    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());
    }
136 137 138

    public function testNonDefaultPKOrder()
    {
139 140 141 142
        if ( ! extension_loaded('sqlite3')) {
            $this->markTestSkipped('This test requires the SQLite3 extension.');
        }

143 144 145 146
        $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.');
        }
147
        $this->_conn->exec(<<<EOS
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
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()));
    }
163

164 165 166 167 168 169 170 171 172 173 174 175
    /**
     * @group DBAL-1779
     */
    public function testListTableColumnsWithWhitespacesInTypeDeclarations()
    {
        $sql = <<<SQL
CREATE TABLE dbal_1779 (
    foo VARCHAR (64) ,
    bar TEXT (100)
)
SQL;

176
        $this->_conn->exec($sql);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

        $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());
    }

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 229 230
    /**
     * @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),
        );
    }
231
}