Skip to content

Commit

Permalink
[#39] - Split patch data script to avoid database deadlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiano-pacheco committed Feb 20, 2024
1 parent f8d2fd3 commit 932cde8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
/**
* The reason for this class is to handle the migration from the old message table to the new one.
* This is necessary because errors are being generated during integration testes while using
* the onCreate="migrateDataFromAnotherTable('old_table')" in the db_schema.xml
* the onCreate="migrateDataFromAnotherTable('run_as_root_message')" in the db_schema.xml
*/
class HandleMigrationFromMessageTablePatch implements DataPatchInterface
class CopyDataFromOldMessageTable implements DataPatchInterface
{
public function __construct(
private readonly ModuleDataSetupInterface $moduleDataSetup,
Expand All @@ -38,11 +38,8 @@ public function apply(): self
}

$select = $connection->select()->from($oldMessageTable);

$connection->query($connection->insertFromSelect($select, $newMessageTable));

$connection->dropTable($oldMessageTable);

$this->moduleDataSetup->endSetup();

return $this;
Expand Down
50 changes: 50 additions & 0 deletions src/Setup/Patch/Data/DeleteOldMessageTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace RunAsRoot\MessageQueueRetry\Setup\Patch\Data;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

/**
* The reason for this class is to handle the migration from the old message table to the new one.
* This is necessary because errors are being generated during integration testes while using
* the onCreate="migrateDataFromAnotherTable('run_as_root_message')" in the db_schema.xml
*/
class DeleteOldMessageTable implements DataPatchInterface
{
public function __construct(
private readonly ModuleDataSetupInterface $moduleDataSetup,
) {
}

public function getAliases(): array
{
return [];
}

public function apply(): self
{
$this->moduleDataSetup->startSetup();

$connection = $this->moduleDataSetup->getConnection();
$oldMessageTable = $this->moduleDataSetup->getTable('run_as_root_message');

if (!$connection->isTableExists($oldMessageTable)) {
$this->moduleDataSetup->endSetup();
return $this;
}

$connection->dropTable($oldMessageTable);

$this->moduleDataSetup->endSetup();

return $this;
}

public static function getDependencies(): array
{
return [CopyDataFromOldMessageTable::class];
}
}

0 comments on commit 932cde8

Please sign in to comment.