Skip to content

Commit

Permalink
Remove unnecesary SchemaCache::class. (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Aug 30, 2023
1 parent aa0f671 commit b9bb077
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 62 deletions.
32 changes: 0 additions & 32 deletions src/Migrator.php
Expand Up @@ -6,7 +6,6 @@

use Symfony\Component\Console\Style\SymfonyStyle;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Query\Query;
use Yiisoft\Yii\Db\Migration\Informer\MigrationInformerInterface;
Expand All @@ -15,11 +14,9 @@
final class Migrator
{
private bool $checkMigrationHistoryTable = true;
private bool $schemaCacheEnabled = false;

public function __construct(
private ConnectionInterface $db,
private SchemaCache $schemaCache,
private MigrationInformerInterface $informer,
private string $historyTable = '{{%migration}}',
private ?int $migrationNameLimit = 180
Expand All @@ -40,31 +37,23 @@ public function up(MigrationInterface $migration): void
{
$this->checkMigrationHistoryTable();

$this->beforeMigrate();

match ($migration instanceof TransactionalMigrationInterface) {
true => $this->db->transaction(fn () => $migration->up($this->createBuilder())),
false => $migration->up($this->createBuilder()),
};

$this->afterMigrate();

$this->addMigrationToHistory($migration);
}

public function down(RevertibleMigrationInterface $migration): void
{
$this->checkMigrationHistoryTable();

$this->beforeMigrate();

match ($migration instanceof TransactionalMigrationInterface) {
true => $this->db->transaction(fn () => $migration->down($this->createBuilder())),
false => $migration->down($this->createBuilder()),
};

$this->afterMigrate();

$this->removeMigrationFromHistory($migration);
}

Expand Down Expand Up @@ -155,8 +144,6 @@ private function createMigrationHistoryTable(): void
$tableName = $this->db->getSchema()->getRawTableName($this->historyTable);
$this->informer->beginCreateHistoryTable('Creating migration history table "' . $tableName . '"...');

$this->beforeMigrate();

$b = $this->createBuilder(new NullMigrationInformer());

$b->createTable($this->historyTable, [
Expand All @@ -165,28 +152,9 @@ private function createMigrationHistoryTable(): void
'apply_time' => $b->integer()->notNull(),
]);

$this->afterMigrate();

$this->informer->endCreateHistoryTable('Done.');
}

private function beforeMigrate(): void
{
$this->schemaCacheEnabled = $this->schemaCache->isEnabled();
if ($this->schemaCacheEnabled) {
$this->schemaCache->setEnabled(false);
}
}

private function afterMigrate(): void
{
if ($this->schemaCacheEnabled) {
$this->schemaCache->setEnabled(true);
}

$this->db->getSchema()->refresh();
}

private function createBuilder(?MigrationInformerInterface $informer = null): MigrationBuilder
{
return new MigrationBuilder(
Expand Down
32 changes: 16 additions & 16 deletions tests/Common/AbstractMigrationBuilderTest.php
Expand Up @@ -115,7 +115,7 @@ public function testDelete(): void
public function testCreateTable(): void
{
$this->builder->createTable('test', ['id' => $this->builder->primaryKey()]);
$tableSchema = $this->db->getTableSchema('test', true);
$tableSchema = $this->db->getTableSchema('test');
$column = $tableSchema->getColumn('id');

$this->assertNotEmpty($tableSchema);
Expand All @@ -131,7 +131,7 @@ public function testCreateTable(): void
public function testCreateTableWithStringColumnDefinition(): void
{
$this->builder->createTable('test', ['name' => 'varchar(50)']);
$tableSchema = $this->db->getTableSchema('test', true);
$tableSchema = $this->db->getTableSchema('test');
$column = $tableSchema->getColumn('name');

$this->assertNotEmpty($tableSchema);
Expand Down Expand Up @@ -214,7 +214,7 @@ public function testAddColumn(string $type, string $expectedComment = null): voi
$this->builder->createTable('test', ['id' => 'int']);
$this->builder->addColumn('test', 'code', $type);

$tableSchema = $this->db->getTableSchema('test', true);
$tableSchema = $this->db->getTableSchema('test');
$column = $tableSchema->getColumn('code');

$this->assertNotEmpty($tableSchema);
Expand All @@ -232,7 +232,7 @@ public function testDropColumn(): void
$this->builder->createTable('test', ['id' => $this->builder->primaryKey(), 'name' => $this->builder->string()]);
$this->builder->dropColumn('test', 'name');

$tableSchema = $this->db->getTableSchema('test', true);
$tableSchema = $this->db->getTableSchema('test');

$this->assertSame(['id'], $tableSchema->getColumnNames());
$this->assertInformerOutputContains(' > drop column name from table test ... Done in');
Expand All @@ -245,7 +245,7 @@ public function testRenameColumn(): void
$this->builder->createTable('test', ['id' => $this->builder->integer()]);
$this->builder->renameColumn('test', 'id', 'id_new');

$tableSchema = $this->db->getTableSchema('test', true);
$tableSchema = $this->db->getTableSchema('test');

$this->assertSame(['id_new'], $tableSchema->getColumnNames());
$this->assertInformerOutputContains(' > Rename column id in table test to id_new ... Done in');
Expand Down Expand Up @@ -294,7 +294,7 @@ public function testAlterColumn(string $type, string|null $defaultValue = null,
$this->builder->createTable('test', ['id' => $this->builder->integer()]);
$this->builder->alterColumn('test', 'id', $type);

$tableSchema = $this->db->getTableSchema('test', true);
$tableSchema = $this->db->getTableSchema('test');
$column = $tableSchema->getColumn('id');

$this->assertNotEmpty($tableSchema);
Expand Down Expand Up @@ -323,7 +323,7 @@ public function testAddPrimaryKey(): void
$this->builder->createTable('test', ['id' => $fieldType]);
$this->builder->addPrimaryKey('test', 'id', ['id']);

$tableSchema = $this->db->getTableSchema('test', true);
$tableSchema = $this->db->getTableSchema('test');
$column = $tableSchema->getColumn('id');

$this->assertNotEmpty($tableSchema);
Expand All @@ -350,7 +350,7 @@ public function testDropPrimaryKey(): void

$this->builder->dropPrimaryKey('test', 'test_pk');

$tableSchema = $this->db->getTableSchema('test', true);
$tableSchema = $this->db->getTableSchema('test');
$column = $tableSchema->getColumn('id');

$this->assertNotEmpty($tableSchema);
Expand Down Expand Up @@ -422,7 +422,7 @@ public function testCreateIndex(): void
$this->builder->createTable('test_table', ['id' => 'int']);
$this->builder->createIndex('test_table', 'unique_index', 'id', 'UNIQUE');

$indexes = $this->db->getSchema()->getTableIndexes('test_table', true);
$indexes = $this->db->getSchema()->getTableIndexes('test_table');

$this->assertCount(1, $indexes);

Expand Down Expand Up @@ -468,7 +468,7 @@ public function testDropIndex(): void
$this->builder->createIndex('test_table', 'test_index', 'id');
$this->builder->dropIndex('test_table', 'test_index');

$indexes = $this->db->getSchema()->getTableIndexes('test_table', true);
$indexes = $this->db->getSchema()->getTableIndexes('test_table');

$this->assertCount(0, $indexes);
$this->assertInformerOutputContains(' > Drop index test_index on test_table ... Done in ');
Expand All @@ -481,7 +481,7 @@ public function testDropIndexNoExist(): void
$this->builder->createTable('test_table', ['id' => $this->builder->integer()]);
$this->builder->dropIndex('test_table', 'test_index');

$indexes = $this->db->getSchema()->getTableIndexes('test_table', true);
$indexes = $this->db->getSchema()->getTableIndexes('test_table');

$this->assertCount(0, $indexes);
$this->assertInformerOutputContains(' > Drop index test_index on test_table skipped. Index does not exist.');
Expand All @@ -495,7 +495,7 @@ public function testDropIndexUnique(): void
$this->builder->createIndex('test_table', 'test_index', 'id', 'UNIQUE');
$this->builder->dropIndex('test_table', 'test_index');

$indexes = $this->db->getSchema()->getTableIndexes('test_table', true);
$indexes = $this->db->getSchema()->getTableIndexes('test_table');

$this->assertCount(0, $indexes);
$this->assertInformerOutputContains(' > Drop index test_index on test_table ... Done in ');
Expand All @@ -508,7 +508,7 @@ public function testAddCommentOnColumn(): void
$this->builder->createTable('test_table', ['id' => $this->builder->integer()]);
$this->builder->addCommentOnColumn('test_table', 'id', 'test comment');

$tableSchema = $this->db->getTableSchema('test_table', true);
$tableSchema = $this->db->getTableSchema('test_table');
$column = $tableSchema->getColumn('id');

$this->assertSame('test comment', $column->getComment());
Expand All @@ -522,7 +522,7 @@ public function testAddCommentOnTable(): void
$this->builder->createTable('test_table', ['id' => $this->builder->integer()]);
$this->builder->addCommentOnTable('test_table', 'test comment');

$tableSchema = $this->builder->getDb()->getTableSchema('test_table', true);
$tableSchema = $this->builder->getDb()->getTableSchema('test_table');

$this->assertSame('test comment', $tableSchema?->getComment());
$this->assertInformerOutputContains(' > Add comment on table test_table ... Done ');
Expand All @@ -536,7 +536,7 @@ public function testDropCommentFromColumn(): void
$this->builder->addCommentOnColumn('test_table', 'id', 'comment');
$this->builder->dropCommentFromColumn('test_table', 'id');

$tableSchema = $this->builder->getDb()->getTableSchema('test_table', true);
$tableSchema = $this->builder->getDb()->getTableSchema('test_table');
$column = $tableSchema->getColumn('id');

match ($this->builder->getDb()->getDriverName()) {
Expand All @@ -555,7 +555,7 @@ public function testDropCommentFromTable(): void
$this->builder->addCommentOnTable('test_table', 'comment');
$this->builder->dropCommentFromTable('test_table');

$tableSchema = $this->builder->getDb()->getTableSchema('test_table', true);
$tableSchema = $this->builder->getDb()->getTableSchema('test_table');

match ($this->builder->getDb()->getDriverName()) {
'mysql' => $this->assertEmpty($tableSchema?->getComment()),
Expand Down
5 changes: 0 additions & 5 deletions tests/Common/AbstractMigratorTest.php
Expand Up @@ -6,7 +6,6 @@

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Yii\Db\Migration\Informer\NullMigrationInformer;
use Yiisoft\Yii\Db\Migration\Migrator;
Expand All @@ -20,7 +19,6 @@ public function testGetMigrationNameLimitPredefined(): void
{
$migrator = new Migrator(
$this->container->get(ConnectionInterface::class),
$this->container->get(SchemaCache::class),
new NullMigrationInformer(),
'{{%migration}}',
42
Expand All @@ -33,7 +31,6 @@ public function testGetMigrationNameLimitWithoutHistoryTable(): void
{
$migrator = new Migrator(
$this->container->get(ConnectionInterface::class),
$this->container->get(SchemaCache::class),
new NullMigrationInformer(),
'{{%migration}}',
null
Expand All @@ -52,7 +49,6 @@ public function testGetMigrationNameLimitWithoutColumnSize(): void

$migrator = new Migrator(
$db,
$this->container->get(SchemaCache::class),
new NullMigrationInformer(),
'{{%migration}}',
null
Expand All @@ -75,7 +71,6 @@ public function testGetMigrationNameLimitFromSchema(): void
{
$migrator = new Migrator(
$this->container->get(ConnectionInterface::class),
$this->container->get(SchemaCache::class),
new NullMigrationInformer(),
'{{%migration}}',
null
Expand Down
2 changes: 1 addition & 1 deletion tests/Common/Command/AbstractUpdateCommandTest.php
Expand Up @@ -189,7 +189,7 @@ public function testExecuteExtended(): void
$this->assertFalse($studentSchema->getColumn('department_id')->isAllowNull());
$this->assertSame(
['department_id'],
$dbSchema->getTableForeignKeys('student', true)[0]->getColumnNames()
$dbSchema->getTableForeignKeys('student')[0]->getColumnNames()
);

/** Check table student field dateofbirth */
Expand Down
2 changes: 1 addition & 1 deletion tests/Driver/Mssql/MigrationBuilderTest.php
Expand Up @@ -28,7 +28,7 @@ public function testCreateTableAnotherSchema(): void
$command->setSql('CREATE SCHEMA yii')->execute();

$this->builder->createTable('yii.test', ['id' => $this->builder->primaryKey()]);
$tableSchema = $db->getSchema()->getTableSchema('yii.test', true);
$tableSchema = $db->getSchema()->getTableSchema('yii.test');
$column = $tableSchema->getColumn('id');

$this->assertNotEmpty($tableSchema);
Expand Down
2 changes: 1 addition & 1 deletion tests/Driver/Mysql/MigrationBuilderTest.php
Expand Up @@ -28,7 +28,7 @@ public function testCreateTableAnotherSchema(): void
$command->setSql('CREATE SCHEMA yii')->execute();

$this->builder->createTable('yii.test', ['id' => $this->builder->primaryKey()]);
$tableSchema = $db->getSchema()->getTableSchema('yii.test', true);
$tableSchema = $db->getSchema()->getTableSchema('yii.test');
$column = $tableSchema->getColumn('id');

$this->assertNotEmpty($tableSchema);
Expand Down
2 changes: 1 addition & 1 deletion tests/Driver/Oracle/MigrationBuilderTest.php
Expand Up @@ -28,7 +28,7 @@ public function testCreateTableAnotherSchema(): void
$command->setSql('CREATE USER yii IDENTIFIED BY yiiSCHEMA')->execute();

$this->builder->createTable('YII.test', ['id' => $this->builder->primaryKey()]);
$tableSchema = $db->getSchema()->getTableSchema('YII.test', true);
$tableSchema = $db->getSchema()->getTableSchema('YII.test');
$column = $tableSchema->getColumn('id');

$this->assertNotEmpty($tableSchema);
Expand Down
2 changes: 1 addition & 1 deletion tests/Driver/Pgsql/MigrationBuilderTest.php
Expand Up @@ -28,7 +28,7 @@ public function testCreateTableAnotherSchema(): void
$command->setSql('CREATE SCHEMA yii')->execute();

$this->builder->createTable('yii.test', ['id' => $this->builder->primaryKey()]);
$tableSchema = $db->getSchema()->getTableSchema('yii.test', true);
$tableSchema = $db->getSchema()->getTableSchema('yii.test');
$column = $tableSchema->getColumn('id');

$this->assertNotEmpty($tableSchema);
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Factory/OracleFactory.php
Expand Up @@ -76,7 +76,7 @@ public static function clearDatabase(ContainerInterface $container): void
];

foreach ($tables as $table) {
if ($db->getTableSchema($table, true) !== null) {
if ($db->getTableSchema($table) !== null) {
$db->createCommand()->dropTable($table)->execute();
}
}
Expand Down
2 changes: 0 additions & 2 deletions tests/Support/Helper/ContainerHelper.php
Expand Up @@ -6,7 +6,6 @@

use Psr\Container\ContainerInterface;
use Yiisoft\Aliases\Aliases;
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Injector\Injector;
use Yiisoft\Test\Support\Container\Exception\NotFoundException;
Expand Down Expand Up @@ -47,7 +46,6 @@ public static function get(ContainerInterface $container, string $id, ContainerC
case Migrator::class:
return new Migrator(
$container->get(ConnectionInterface::class),
$container->get(SchemaCache::class),
$container->get(ConsoleMigrationInformer::class),
);

Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Helper/DbHelper.php
Expand Up @@ -31,7 +31,7 @@ public static function dropTable(ConnectionInterface $db, string $name): void
public static function checkSchema(ConnectionInterface $db, string $name): bool
{
$schema = $db->getSchema();
$tableSchema = $schema->getTableSchema($name, true);
$tableSchema = $schema->getTableSchema($name);

return $tableSchema !== null;
}
Expand Down

0 comments on commit b9bb077

Please sign in to comment.