diff --git a/src/Migrator.php b/src/Migrator.php index 0e000175..a0f1ffff 100644 --- a/src/Migrator.php +++ b/src/Migrator.php @@ -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; @@ -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 @@ -40,15 +37,11 @@ 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); } @@ -56,15 +49,11 @@ 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); } @@ -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, [ @@ -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( diff --git a/tests/Common/AbstractMigrationBuilderTest.php b/tests/Common/AbstractMigrationBuilderTest.php index 13bd3120..ec08a966 100644 --- a/tests/Common/AbstractMigrationBuilderTest.php +++ b/tests/Common/AbstractMigrationBuilderTest.php @@ -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); @@ -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); @@ -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); @@ -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'); @@ -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'); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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 '); @@ -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.'); @@ -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 '); @@ -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()); @@ -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 '); @@ -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()) { @@ -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()), diff --git a/tests/Common/AbstractMigratorTest.php b/tests/Common/AbstractMigratorTest.php index 57a37852..d4ff1c0d 100644 --- a/tests/Common/AbstractMigratorTest.php +++ b/tests/Common/AbstractMigratorTest.php @@ -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; @@ -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 @@ -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 @@ -52,7 +49,6 @@ public function testGetMigrationNameLimitWithoutColumnSize(): void $migrator = new Migrator( $db, - $this->container->get(SchemaCache::class), new NullMigrationInformer(), '{{%migration}}', null @@ -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 diff --git a/tests/Common/Command/AbstractUpdateCommandTest.php b/tests/Common/Command/AbstractUpdateCommandTest.php index 4c8bd09b..0ac1c863 100644 --- a/tests/Common/Command/AbstractUpdateCommandTest.php +++ b/tests/Common/Command/AbstractUpdateCommandTest.php @@ -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 */ diff --git a/tests/Driver/Mssql/MigrationBuilderTest.php b/tests/Driver/Mssql/MigrationBuilderTest.php index edb70e0a..389c9e0d 100644 --- a/tests/Driver/Mssql/MigrationBuilderTest.php +++ b/tests/Driver/Mssql/MigrationBuilderTest.php @@ -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); diff --git a/tests/Driver/Mysql/MigrationBuilderTest.php b/tests/Driver/Mysql/MigrationBuilderTest.php index 88f7c6a2..3fe21ac5 100644 --- a/tests/Driver/Mysql/MigrationBuilderTest.php +++ b/tests/Driver/Mysql/MigrationBuilderTest.php @@ -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); diff --git a/tests/Driver/Oracle/MigrationBuilderTest.php b/tests/Driver/Oracle/MigrationBuilderTest.php index 4ae2c767..9f5c37a0 100644 --- a/tests/Driver/Oracle/MigrationBuilderTest.php +++ b/tests/Driver/Oracle/MigrationBuilderTest.php @@ -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); diff --git a/tests/Driver/Pgsql/MigrationBuilderTest.php b/tests/Driver/Pgsql/MigrationBuilderTest.php index 6586a54a..cfb9fb09 100644 --- a/tests/Driver/Pgsql/MigrationBuilderTest.php +++ b/tests/Driver/Pgsql/MigrationBuilderTest.php @@ -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); diff --git a/tests/Support/Factory/OracleFactory.php b/tests/Support/Factory/OracleFactory.php index 6250bf4c..8479e0db 100644 --- a/tests/Support/Factory/OracleFactory.php +++ b/tests/Support/Factory/OracleFactory.php @@ -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(); } } diff --git a/tests/Support/Helper/ContainerHelper.php b/tests/Support/Helper/ContainerHelper.php index 44627e53..e31f1ca1 100644 --- a/tests/Support/Helper/ContainerHelper.php +++ b/tests/Support/Helper/ContainerHelper.php @@ -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; @@ -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), ); diff --git a/tests/Support/Helper/DbHelper.php b/tests/Support/Helper/DbHelper.php index b8b40b4c..ba4d8f9a 100644 --- a/tests/Support/Helper/DbHelper.php +++ b/tests/Support/Helper/DbHelper.php @@ -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; }