Fix SQLite CommandTest skipped tests and minor fixes. (#20648)

This commit is contained in:
Wilmer Arambula
2025-10-29 11:51:58 -03:00
committed by GitHub
parent d1ecf2ec2a
commit ed6989d87b
2 changed files with 345 additions and 106 deletions

View File

@@ -741,10 +741,6 @@ SQL;
public function testAlterTable(): void
{
if ($this->driverName === 'sqlite') {
$this->markTestSkipped('Sqlite does not support alterTable');
}
$db = $this->getConnection();
if ($db->getSchema()->getTableSchema('testAlterTable') !== null) {
@@ -1081,64 +1077,151 @@ SQL;
}
*/
public function testAddDropPrimaryKey(): void
public static function addPrimaryKeyProvider(): array
{
$db = $this->getConnection(false);
$tableName = 'test_pk';
$name = 'test_pk_constraint';
/** @var \yii\db\pgsql\Schema $schema */
$schema = $db->getSchema();
if ($schema->getTableSchema($tableName) !== null) {
$db->createCommand()->dropTable($tableName)->execute();
}
$db->createCommand()->createTable($tableName, [
'int1' => 'integer not null',
'int2' => 'integer not null',
])->execute();
$this->assertNull($schema->getTablePrimaryKey($tableName, true));
$db->createCommand()->addPrimaryKey($name, $tableName, ['int1'])->execute();
$this->assertEquals(['int1'], $schema->getTablePrimaryKey($tableName, true)->columnNames);
$db->createCommand()->dropPrimaryKey($name, $tableName)->execute();
$this->assertNull($schema->getTablePrimaryKey($tableName, true));
$db->createCommand()->addPrimaryKey($name, $tableName, ['int1', 'int2'])->execute();
$this->assertEquals(['int1', 'int2'], $schema->getTablePrimaryKey($tableName, true)->columnNames);
return [
[
'{{test_pk_constraint_1}}',
'{{test_pk}}',
'int1',
],
[
'{{test_pk_constraint_2}}',
'{{test_pk}}',
['int1'],
],
[
'{{test_pk_constraint_3}}',
'{{test_pk}}',
[
'int1',
'int2',
],
],
];
}
public function testAddDropForeignKey(): void
/**
* @dataProvider addPrimaryKeyProvider
*
* @param string $name
* @param string $tableName
* @param array|string $pk
*
* @phpstan-param list<string> $pk
*/
public function testAddDropPrimaryKey(string $name, string $tableName, $pk): void
{
$db = $this->getConnection(false);
$tableName = 'test_fk';
$name = 'test_fk_constraint';
/** @var \yii\db\pgsql\Schema $schema */
$schema = $db->getSchema();
if ($schema->getTableSchema($tableName) !== null) {
$db->createCommand()->dropTable($tableName)->execute();
}
$db->createCommand()->createTable($tableName, [
'int1' => 'integer not null unique',
'int2' => 'integer not null unique',
'int3' => 'integer not null unique',
'int4' => 'integer not null unique',
'unique ([[int1]], [[int2]])',
'unique ([[int3]], [[int4]])',
])->execute();
$db->createCommand()->createTable(
$tableName,
[
'int1' => 'integer not null',
'int2' => 'integer not null',
],
)->execute();
$this->assertNull($schema->getTablePrimaryKey($tableName, true));
$db->createCommand()->addPrimaryKey($name, $tableName, $pk)->execute();
$this->assertSame((array) $pk, $schema->getTablePrimaryKey($tableName, true)->columnNames);
$db->createCommand()->dropPrimaryKey($name, $tableName)->execute();
$this->assertNull($schema->getTablePrimaryKey($tableName, true));
$db->createCommand()->dropTable($tableName)->execute();
}
public static function addForeignKeyProvider(): array
{
return [
[
'{{test_fk_constraint_1}}',
'{{test_fk}}',
'int1',
'int3',
],
[
'{{test_fk_constraint_2}}',
'{{test_fk}}',
['int1'],
['int3'],
],
[
'{{test_fk_constraint_3}}',
'{{test_fk}}',
[
'int1',
'int2',
],
[
'int3',
'int4',
],
],
];
}
/**
* @dataProvider addForeignKeyProvider
*
* @param string $name
* @param string $tableName
* @param array|string $fkColumns
* @param array|string $refColumns
*
* @phpstan-param list<string> $fkColumns
* @phpstan-param list<string> $refColumns
*/
public function testAddDropForeignKey(string $name, string $tableName, $fkColumns, $refColumns): void
{
$db = $this->getConnection(false);
$schema = $db->getSchema();
if ($schema->getTableSchema($tableName) !== null) {
$db->createCommand()->dropTable($tableName)->execute();
}
$db->createCommand()->createTable(
$tableName,
[
'int1' => 'integer not null unique',
'int2' => 'integer not null unique',
'int3' => 'integer not null unique',
'int4' => 'integer not null unique',
'unique ([[int1]], [[int2]])',
'unique ([[int3]], [[int4]])',
],
)->execute();
$this->assertEmpty($schema->getTableForeignKeys($tableName, true));
$db->createCommand()->addForeignKey($name, $tableName, ['int1'], $tableName, ['int3'])->execute();
$this->assertEquals(['int1'], $schema->getTableForeignKeys($tableName, true)[0]->columnNames);
$this->assertEquals(['int3'], $schema->getTableForeignKeys($tableName, true)[0]->foreignColumnNames);
$db->createCommand()->addForeignKey(
$name,
$tableName,
(array) $fkColumns,
$tableName,
(array) $refColumns,
)->execute();
$this->assertSame((array) $fkColumns, $schema->getTableForeignKeys($tableName, true)[0]->columnNames);
$this->assertSame((array) $refColumns, $schema->getTableForeignKeys($tableName, true)[0]->foreignColumnNames);
$db->createCommand()->dropForeignKey($name, $tableName)->execute();
$this->assertEmpty($schema->getTableForeignKeys($tableName, true));
$db->createCommand()->addForeignKey($name, $tableName, ['int1', 'int2'], $tableName, ['int3', 'int4'])->execute();
$this->assertEquals(['int1', 'int2'], $schema->getTableForeignKeys($tableName, true)[0]->columnNames);
$this->assertEquals(['int3', 'int4'], $schema->getTableForeignKeys($tableName, true)[0]->foreignColumnNames);
$db->createCommand()->dropTable($tableName)->execute();
}
public function testCreateDropIndex(): void
@@ -1185,54 +1268,96 @@ SQL;
$this->assertTrue($schema->getTableIndexes($tableName, true)[0]->isUnique);
}
public function testAddDropUnique(): void
public static function addUniqueProvider(): array
{
return [
[
'{{test_unique_constraint_1}}',
'{{test_unique}}',
'int1',
],
[
'{{test_unique_constraint_2}}',
'{{test_unique}}',
['int1'],
],
[
'{{test_unique_constraint_3}}',
'{{test_unique}}',
[
'int1',
'int2',
],
],
];
}
/**
* @dataProvider addUniqueProvider
*
* @param string $name
* @param string $tableName
* @param array|string $columns
*
* @phpstan-param list<string> $columns
*/
public function testAddDropUnique(string $name, string $tableName, $columns): void
{
$db = $this->getConnection(false);
$tableName = 'test_uq';
$name = 'test_uq_constraint';
/** @var \yii\db\pgsql\Schema $schema */
$schema = $db->getSchema();
if ($schema->getTableSchema($tableName) !== null) {
$db->createCommand()->dropTable($tableName)->execute();
}
$db->createCommand()->createTable($tableName, [
'int1' => 'integer not null',
'int2' => 'integer not null',
])->execute();
$db->createCommand()->createTable(
$tableName,
[
'int1' => 'integer not null',
'int2' => 'integer not null',
],
)->execute();
$this->assertEmpty($schema->getTableUniques($tableName, true));
$db->createCommand()->addUnique($name, $tableName, ['int1'])->execute();
$this->assertEquals(['int1'], $schema->getTableUniques($tableName, true)[0]->columnNames);
$db->createCommand()->addUnique($name, $tableName, $columns)->execute();
$this->assertSame((array) $columns, $schema->getTableUniques($tableName, true)[0]->columnNames);
$db->createCommand()->dropUnique($name, $tableName)->execute();
$this->assertEmpty($schema->getTableUniques($tableName, true));
$db->createCommand()->addUnique($name, $tableName, ['int1', 'int2'])->execute();
$this->assertEquals(['int1', 'int2'], $schema->getTableUniques($tableName, true)[0]->columnNames);
$db->createCommand()->dropTable($tableName)->execute();
}
public function testAddDropCheck(): void
{
$db = $this->getConnection(false);
if (version_compare($db->getServerVersion(), '8.0.16', '<')) {
if ($db->getDriverName() === 'mysql' && version_compare($db->getServerVersion(), '8.0.16', '<')) {
$this->markTestSkipped('MySQL < 8.0.16 does not support CHECK constraints.');
}
$tableName = 'test_ck';
$name = 'test_ck_constraint';
$schema = $db->getSchema();
if ($schema->getTableSchema($tableName) !== null) {
$db->createCommand()->dropTable($tableName)->execute();
}
$db->createCommand()->createTable($tableName, [
'int1' => 'integer',
])->execute();
$db->createCommand()->createTable(
$tableName,
['int1' => 'integer'],
)->execute();
$this->assertEmpty($schema->getTableChecks($tableName, true));
$db->createCommand()->addCheck($name, $tableName, '[[int1]] > 1')->execute();
$this->assertMatchesRegularExpression(
'/^.*int1.*>.*1.*$/',
$schema->getTableChecks($tableName, true)[0]->expression