1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tests\Functional\Schema;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\BlobType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
use function array_keys;
use function array_shift;
#[RequiresPhpunit('< 11.5.18')]
class SQLiteSchemaManagerTest extends SchemaManagerFunctionalTestCase
{
protected function supportsPlatform(AbstractPlatform $platform): bool
{
return $platform instanceof SQLitePlatform;
}
/**
* SQLITE does not support databases.
*/
public function testListDatabases(): void
{
$this->expectException(Exception::class);
$this->schemaManager->listDatabases();
}
public function createListTableColumns(): Table
{
$table = parent::createListTableColumns();
$table->getColumn('id')->setAutoincrement(true);
return $table;
}
public function testListForeignKeysFromExistingDatabase(): void
{
$this->connection->executeStatement('DROP TABLE IF EXISTS user');
$this->connection->executeStatement(<<<'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 = [
new ForeignKeyConstraint(
['page'],
'page',
['key'],
'FK_1',
['onUpdate' => 'NO ACTION', 'onDelete' => 'NO ACTION', 'deferrable' => true, 'deferred' => true],
),
new ForeignKeyConstraint(
['parent'],
'user',
['id'],
'',
['onUpdate' => 'NO ACTION', 'onDelete' => 'CASCADE', 'deferrable' => false, 'deferred' => false],
),
new ForeignKeyConstraint(
['log'],
'log',
[],
'FK_3',
['onUpdate' => 'SET NULL', 'onDelete' => 'NO ACTION', 'deferrable' => false, 'deferred' => false],
),
];
self::assertEquals($expected, $this->schemaManager->listTableForeignKeys('user'));
}
public function testColumnCollation(): void
{
$table = new Table('test_collation');
$table->addColumn('id', Types::INTEGER);
$table->addColumn('text', Types::TEXT);
$table->addColumn('foo', Types::TEXT)->setPlatformOption('collation', 'BINARY');
$table->addColumn('bar', Types::TEXT)->setPlatformOption('collation', 'NOCASE');
$this->dropAndCreateTable($table);
$columns = $this->schemaManager->listTableColumns('test_collation');
self::assertArrayNotHasKey('collation', $columns['id']->getPlatformOptions());
self::assertEquals('BINARY', $columns['text']->getPlatformOption('collation'));
self::assertEquals('BINARY', $columns['foo']->getPlatformOption('collation'));
self::assertEquals('NOCASE', $columns['bar']->getPlatformOption('collation'));
}
/**
* SQLite stores BINARY columns as BLOB
*/
protected function assertBinaryColumnIsValid(Table $table, string $columnName, int $expectedLength): void
{
self::assertInstanceOf(BlobType::class, $table->getColumn($columnName)->getType());
}
/**
* SQLite stores VARBINARY columns as BLOB
*/
protected function assertVarBinaryColumnIsValid(Table $table, string $columnName, int $expectedLength): void
{
self::assertInstanceOf(BlobType::class, $table->getColumn($columnName)->getType());
}
public function testListTableColumnsWithWhitespacesInTypeDeclarations(): void
{
$sql = <<<'SQL'
CREATE TABLE dbal_1779 (
foo VARCHAR (64) ,
bar TEXT (100)
)
SQL;
$this->connection->executeStatement($sql);
$columns = $this->schemaManager->listTableColumns('dbal_1779');
self::assertCount(2, $columns);
self::assertArrayHasKey('foo', $columns);
self::assertArrayHasKey('bar', $columns);
self::assertSame(Type::getType(Types::STRING), $columns['foo']->getType());
self::assertSame(Type::getType(Types::TEXT), $columns['bar']->getType());
self::assertSame(64, $columns['foo']->getLength());
self::assertSame(100, $columns['bar']->getLength());
}
public function testPrimaryKeyNoAutoIncrement(): void
{
$table = new Table('test_pk_auto_increment');
$table->addColumn('id', Types::INTEGER);
$table->addColumn('text', Types::TEXT);
$table->setPrimaryKey(['id']);
$this->dropAndCreateTable($table);
$this->connection->insert('test_pk_auto_increment', ['text' => '1']);
$this->connection->executeStatement('DELETE FROM test_pk_auto_increment');
$this->connection->insert('test_pk_auto_increment', ['text' => '2']);
$lastUsedIdAfterDelete = (int) $this->connection->fetchOne(
'SELECT id FROM test_pk_auto_increment WHERE text = "2"',
);
// with an empty table, non autoincrement rowid is always 1
self::assertEquals(1, $lastUsedIdAfterDelete);
}
public function testOnlyOwnCommentIsParsed(): void
{
$table = new Table('own_column_comment');
$table->addColumn('col1', Types::STRING, ['length' => 16]);
$table->addColumn('col2', Types::STRING, ['length' => 16, 'comment' => 'Column #2']);
$table->addColumn('col3', Types::STRING, ['length' => 16]);
$sm = $this->connection->createSchemaManager();
$sm->createTable($table);
self::assertSame('', $sm->introspectTable('own_column_comment')
->getColumn('col1')
->getComment());
}
public function testNonSimpleAlterTableCreatedFromDDL(): void
{
$this->dropTableIfExists('nodes');
$ddl = <<<'DDL'
CREATE TABLE nodes (
id INTEGER NOT NULL,
parent_id INTEGER,
name TEXT,
PRIMARY KEY (id),
FOREIGN KEY (parent_id) REFERENCES nodes (id)
)
DDL;
$this->connection->executeStatement($ddl);
$schemaManager = $this->connection->createSchemaManager();
$table1 = $schemaManager->introspectTable('nodes');
$table2 = clone $table1;
$table2->addIndex(['name'], 'idx_name');
$comparator = $schemaManager->createComparator();
$diff = $comparator->compareTables($table1, $table2);
$schemaManager->alterTable($diff);
$table = $schemaManager->introspectTable('nodes');
$index = $table->getIndex('idx_name');
self::assertSame(['name'], $index->getColumns());
}
public function testAlterTableWithSchema(): void
{
$this->dropTableIfExists('t');
$table = new Table('main.t');
$table->addColumn('a', Types::INTEGER);
$this->schemaManager->createTable($table);
self::assertSame(['a'], array_keys($this->schemaManager->listTableColumns('t')));
$tableDiff = new TableDiff($table, changedColumns: [
'a' => new ColumnDiff(
new Column('a', Type::getType(Types::INTEGER)),
new Column('b', Type::getType(Types::INTEGER)),
),
]);
$this->schemaManager->alterTable($tableDiff);
self::assertSame(['b'], array_keys($this->schemaManager->listTableColumns('t')));
}
public function testIntrospectMultipleAnonymousForeignKeyConstraints(): void
{
$this->dropTableIfExists('album');
$this->dropTableIfExists('song');
$ddl = <<<'DDL'
CREATE TABLE artist(
id INTEGER,
name TEXT,
PRIMARY KEY(id)
);
CREATE TABLE album(
id INTEGER,
name TEXT,
PRIMARY KEY(id)
);
CREATE TABLE song(
id INTEGER,
album_id INTEGER,
artist_id INTEGER,
FOREIGN KEY(album_id) REFERENCES album(id),
FOREIGN KEY(artist_id) REFERENCES artist(id)
);
DDL;
$this->connection->executeStatement($ddl);
$schemaManager = $this->connection->createSchemaManager();
$song = $schemaManager->introspectTable('song');
$foreignKeys = $song->getForeignKeys();
self::assertCount(2, $foreignKeys);
$foreignKey1 = array_shift($foreignKeys);
self::assertNotNull($foreignKey1);
self::assertEmpty($foreignKey1->getName());
self::assertSame(['album_id'], $foreignKey1->getLocalColumns());
self::assertSame(['id'], $foreignKey1->getForeignColumns());
$foreignKey2 = array_shift($foreignKeys);
self::assertNotNull($foreignKey2);
self::assertEmpty($foreignKey2->getName());
self::assertSame(['artist_id'], $foreignKey2->getLocalColumns());
self::assertSame(['id'], $foreignKey2->getForeignColumns());
}
public function testNoWhitespaceInForeignKeyReference(): void
{
$this->dropTableIfExists('notes');
$this->dropTableIfExists('users');
$ddl = <<<'DDL'
CREATE TABLE "users" (
"id" INTEGER
);
CREATE TABLE "notes" (
"id" INTEGER,
"created_by" INTEGER,
FOREIGN KEY("created_by") REFERENCES "users"("id"));
DDL;
$this->connection->executeStatement($ddl);
$notes = $this->schemaManager->introspectTable('notes');
$foreignKeys = $notes->getForeignKeys();
self::assertCount(1, $foreignKeys);
$foreignKey = array_shift($foreignKeys);
self::assertNotNull($foreignKey);
self::assertSame(['created_by'], $foreignKey->getLocalColumns());
self::assertSame('users', $foreignKey->getForeignTableName());
self::assertSame(['id'], $foreignKey->getForeignColumns());
}
public function testShorthandInForeignKeyReference(): void
{
$this->dropTableIfExists('artist');
$this->dropTableIfExists('track');
$ddl = <<<'DDL'
CREATE TABLE artist(
artistid INTEGER PRIMARY KEY,
artistname TEXT
);
CREATE TABLE track(
trackid INTEGER,
trackname TEXT,
trackartist INTEGER REFERENCES artist
);
DDL;
$this->connection->executeStatement($ddl);
$schemaManager = $this->connection->createSchemaManager();
$song = $schemaManager->introspectTable('track');
$foreignKeys = $song->getForeignKeys();
self::assertCount(1, $foreignKeys);
$foreignKey1 = array_shift($foreignKeys);
self::assertNotNull($foreignKey1);
self::assertEmpty($foreignKey1->getName());
self::assertSame(['trackartist'], $foreignKey1->getLocalColumns());
self::assertSame(['artistid'], $foreignKey1->getForeignColumns());
}
public function testShorthandInForeignKeyReferenceWithMultipleColumns(): void
{
$this->dropTableIfExists('artist');
$this->dropTableIfExists('track');
$ddl = <<<'DDL'
CREATE TABLE artist(
artistid INTEGER,
isrc TEXT,
artistname TEXT,
PRIMARY KEY (artistid, isrc)
);
CREATE TABLE track(
trackid INTEGER,
trackname TEXT,
trackartist INTEGER REFERENCES artist
);
DDL;
$this->connection->executeStatement($ddl);
$schemaManager = $this->connection->createSchemaManager();
$track = $schemaManager->introspectTable('track');
$foreignKeys = $track->getForeignKeys();
self::assertCount(1, $foreignKeys);
$foreignKey1 = array_shift($foreignKeys);
self::assertNotNull($foreignKey1);
self::assertEmpty($foreignKey1->getName());
self::assertSame(['trackartist'], $foreignKey1->getLocalColumns());
self::assertSame(['artistid', 'isrc'], $foreignKey1->getForeignColumns());
$createTableTrackSql = $this->connection->getDatabasePlatform()->getCreateTableSQL($track);
self::assertSame(
[
'CREATE TABLE track (trackid INTEGER DEFAULT NULL, trackname CLOB DEFAULT NULL COLLATE "BINARY",'
. ' trackartist INTEGER DEFAULT NULL, FOREIGN KEY (trackartist) REFERENCES artist (artistid, isrc) ON'
. ' UPDATE NO ACTION ON DELETE NO ACTION NOT DEFERRABLE INITIALLY IMMEDIATE)',
'CREATE INDEX IDX_D6E3F8A6FB96D8BC ON track (trackartist)',
],
$createTableTrackSql,
);
}
/**
* This test duplicates {@see parent::testCommentInTable()} with the only difference that the name of the table
* being created is quoted. It is only meant to cover the logic of parsing the SQLite CREATE TABLE statement
* when the table name is quoted.
*
* Running the same test for all platforms, on the one hand, won't produce additional coverage, and on the other,
* is not feasible due to the differences in case sensitivity depending on whether the name is quoted.
*
* Once all identifiers are quoted by default, this test can be removed.
*/
public function testCommentInQuotedTable(): void
{
$table = new Table('"table_with_comment"');
$table->addColumn('id', Types::INTEGER);
$table->setComment('This is a comment');
$this->dropAndCreateTable($table);
$table = $this->schemaManager->introspectTable('table_with_comment');
self::assertSame('This is a comment', $table->getComment());
}
}
|