Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);
/*
* (c) shopware AG <[email protected]>
* 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`);');
}
}
}
2 changes: 2 additions & 0 deletions src/Migration/MigrationFix/SwagMigrationFixDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);
Expand Down
24 changes: 24 additions & 0 deletions src/Migration/MigrationFix/SwagMigrationFixEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class SwagMigrationFixEntity extends Entity

protected string $path;

protected string $entityId;

protected string $entityName;

public function getConnectionId(): string
{
return $this->connectionId;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);
/*
* (c) shopware AG <[email protected]>
* 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'));
}
}
55 changes: 55 additions & 0 deletions tests/TableHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types=1);
/*
* (c) shopware AG <[email protected]>
* 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);
}
}
Loading