Skip to content

Commit

Permalink
Merge pull request #40 from run-as-root/feature/#39
Browse files Browse the repository at this point in the history
[#39] - Adds a data patch to message table migration
  • Loading branch information
cristiano-pacheco authored Feb 20, 2024
2 parents 9b3aa7d + f4abcea commit f8d2fd3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
55 changes: 55 additions & 0 deletions src/Setup/Patch/Data/HandleMigrationFromMessageTablePatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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('old_table')" in the db_schema.xml
*/
class HandleMigrationFromMessageTablePatch 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');
$newMessageTable = $this->moduleDataSetup->getTable('run_as_root_queue_error_message');

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

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

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

$connection->dropTable($oldMessageTable);

$this->moduleDataSetup->endSetup();

return $this;
}

public static function getDependencies(): array
{
return [];
}
}
3 changes: 1 addition & 2 deletions src/etc/db_schema.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="run_as_root_queue_error_message" onCreate="migrateDataFromAnotherTable(run_as_root_message)"
resource="default" engine="innodb">
<table name="run_as_root_queue_error_message" resource="default" engine="innodb">
<column xsi:type="bigint" name="entity_id" unsigned="false" nullable="false" identity="true"/>
<column xsi:type="varchar" name="topic_name" length="255" nullable="false"/>
<column xsi:type="mediumtext" name="message_body" nullable="true"/>
Expand Down
21 changes: 1 addition & 20 deletions src/etc/db_schema_whitelist.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
{
"run_as_root_message": {
"column": {
"entity_id": true,
"topic_name": true,
"message_body": true,
"failure_description": true,
"creation_time": true,
"update_time": true,
"resource_id": true,
"created_at": true,
"updated_at": true
},
"index": {
"RUN_AS_ROOT_MESSAGE_TOPIC_NAME": true
},
"constraint": {
"PRIMARY": true
}
},
"run_as_root_queue_error_message": {
"column": {
"entity_id": true,
Expand All @@ -34,4 +15,4 @@
"PRIMARY": true
}
}
}
}

0 comments on commit f8d2fd3

Please sign in to comment.