diff --git a/src/Core/Migration/Migration1762177450AddEntityIdAndEntityNameToFixTable.php b/src/Core/Migration/Migration1762177450AddEntityIdAndEntityNameToFixTable.php new file mode 100644 index 000000000..9bd805b46 --- /dev/null +++ b/src/Core/Migration/Migration1762177450AddEntityIdAndEntityNameToFixTable.php @@ -0,0 +1,34 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace SwagMigrationAssistant\Core\Migration; + +use Doctrine\DBAL\Connection; +use Shopware\Core\Framework\Log\Package; +use Shopware\Core\Framework\Migration\MigrationStep; + +/** + * @internal + */ +#[Package('fundamentals@after-sales')] +class Migration1762177450AddEntityIdAndEntityNameToFixTable extends MigrationStep +{ + public function getCreationTimestamp(): int + { + return 1762177450; + } + + public function update(Connection $connection): void + { + $this->addColumn($connection, 'swag_migration_fix', 'entity_id', 'BINARY(16)'); + $this->addColumn($connection, 'swag_migration_fix', 'entity_name', 'VARCHAR(255)'); + + if (!$this->indexExists($connection, 'swag_migration_fix', 'idx.entity_id')) { + $connection->executeStatement('ALTER TABLE `swag_migration_fix` ADD INDEX `idx.entity_id` (`entity_id`);'); + } + } +} diff --git a/src/Migration/MigrationFix/SwagMigrationFixDefinition.php b/src/Migration/MigrationFix/SwagMigrationFixDefinition.php index 4ad095294..43f17b62c 100644 --- a/src/Migration/MigrationFix/SwagMigrationFixDefinition.php +++ b/src/Migration/MigrationFix/SwagMigrationFixDefinition.php @@ -46,6 +46,8 @@ protected function defineFields(): FieldCollection (new IdField('main_mapping_id', 'mainMappingId'))->addFlags(new Required()), (new AnyJsonField('value', 'value'))->addFlags(new Required()), (new StringField('path', 'path'))->addFlags(new Required()), + new IdField('entity_id', 'entityId'), + new StringField('entity_name', 'entityName'), new CreatedAtField(), new UpdatedAtField(), ]); diff --git a/src/Migration/MigrationFix/SwagMigrationFixEntity.php b/src/Migration/MigrationFix/SwagMigrationFixEntity.php index 1a6faa02d..f8f8867d2 100644 --- a/src/Migration/MigrationFix/SwagMigrationFixEntity.php +++ b/src/Migration/MigrationFix/SwagMigrationFixEntity.php @@ -24,6 +24,10 @@ class SwagMigrationFixEntity extends Entity protected string $path; + protected string $entityId; + + protected string $entityName; + public function getConnectionId(): string { return $this->connectionId; @@ -63,4 +67,24 @@ public function setPath(string $path): void { $this->path = $path; } + + public function getEntityId(): string + { + return $this->entityId; + } + + public function setEntityId(string $entityId): void + { + $this->entityId = $entityId; + } + + public function getEntityName(): string + { + return $this->entityName; + } + + public function setEntityName(string $entityName): void + { + $this->entityName = $entityName; + } } diff --git a/tests/Core/Migration/Migration1762177450AddEntityIdAndEntityNameToFixTableTest.php b/tests/Core/Migration/Migration1762177450AddEntityIdAndEntityNameToFixTableTest.php new file mode 100644 index 000000000..5a7def93c --- /dev/null +++ b/tests/Core/Migration/Migration1762177450AddEntityIdAndEntityNameToFixTableTest.php @@ -0,0 +1,45 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Core\Migration; + +use PHPUnit\Framework\TestCase; +use Shopware\Core\Framework\Log\Package; +use Shopware\Core\Framework\Test\TestCaseBase\KernelLifecycleManager; +use SwagMigrationAssistant\Core\Migration\Migration1762177450AddEntityIdAndEntityNameToFixTable; +use SwagMigrationAssistant\Test\TableHelperTrait; + +/** + * @internal + */ +#[Package('fundamentals@after-sales')] +class Migration1762177450AddEntityIdAndEntityNameToFixTableTest extends TestCase +{ + use TableHelperTrait; + + public function testUpdate(): void + { + $connection = KernelLifecycleManager::getConnection(); + + $this->dropIndex($connection, 'swag_migration_fix', 'idx.entity_id'); + static::assertFalse($this->indexExists($connection, 'swag_migration_fix', 'idx.entity_id')); + + $this->dropColumn($connection, 'swag_migration_fix', 'entity_id'); + static::assertFalse($this->columnExists($connection, 'swag_migration_fix', 'entity_id')); + + $this->dropColumn($connection, 'swag_migration_fix', 'entity_name'); + static::assertFalse($this->columnExists($connection, 'swag_migration_fix', 'entity_name')); + + $migration = new Migration1762177450AddEntityIdAndEntityNameToFixTable(); + $migration->update($connection); + $migration->update($connection); + + static::assertTrue($this->indexExists($connection, 'swag_migration_fix', 'idx.entity_id')); + static::assertTrue($this->columnExists($connection, 'swag_migration_fix', 'entity_id')); + static::assertTrue($this->columnExists($connection, 'swag_migration_fix', 'entity_name')); + } +} diff --git a/tests/TableHelperTrait.php b/tests/TableHelperTrait.php new file mode 100644 index 000000000..57bbab2e6 --- /dev/null +++ b/tests/TableHelperTrait.php @@ -0,0 +1,55 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace SwagMigrationAssistant\Test; + +use Doctrine\DBAL\Connection; +use Shopware\Core\Framework\Log\Package; +use Shopware\Core\Framework\Migration\AddColumnTrait; + +/** + * @internal + */ +#[Package('after-sales')] +trait TableHelperTrait +{ + // Contains functions columnExists and addColumn + use AddColumnTrait; + + protected function indexExists(Connection $connection, string $table, string $indexName): bool + { + $exists = $connection->fetchAssociative('SHOW INDEX FROM `' . $table . '` WHERE Key_name = :indexName', ['indexName' => $indexName]); + + return !empty($exists); + } + + protected function dropTable(Connection $connection, string $table): void + { + $sql = \sprintf('DROP TABLE IF EXISTS `%s`', $table); + $connection->executeStatement($sql); + } + + protected function dropColumn(Connection $connection, string $table, string $columnName): void + { + if (!$this->columnExists($connection, $table, $columnName)) { + return; + } + + $connection->executeStatement(\sprintf('ALTER TABLE `%s` DROP COLUMN `%s`', $table, $columnName)); + } + + protected function dropIndex(Connection $connection, string $table, string $indexName): void + { + if (!$this->indexExists($connection, $table, $indexName)) { + return; + } + + $sql = \sprintf('ALTER TABLE `%s` DROP INDEX `%s`', $table, $indexName); + + $connection->executeStatement($sql); + } +}