Skip to content

Commit f8d2fd3

Browse files
Merge pull request #40 from run-as-root/feature/#39
[#39] - Adds a data patch to message table migration
2 parents 9b3aa7d + f4abcea commit f8d2fd3

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RunAsRoot\MessageQueueRetry\Setup\Patch\Data;
6+
7+
use Magento\Framework\Setup\ModuleDataSetupInterface;
8+
use Magento\Framework\Setup\Patch\DataPatchInterface;
9+
10+
/**
11+
* The reason for this class is to handle the migration from the old message table to the new one.
12+
* This is necessary because errors are being generated during integration testes while using
13+
* the onCreate="migrateDataFromAnotherTable('old_table')" in the db_schema.xml
14+
*/
15+
class HandleMigrationFromMessageTablePatch implements DataPatchInterface
16+
{
17+
public function __construct(
18+
private readonly ModuleDataSetupInterface $moduleDataSetup,
19+
) {
20+
}
21+
22+
public function getAliases(): array
23+
{
24+
return [];
25+
}
26+
27+
public function apply(): self
28+
{
29+
$this->moduleDataSetup->startSetup();
30+
31+
$connection = $this->moduleDataSetup->getConnection();
32+
$oldMessageTable = $this->moduleDataSetup->getTable('run_as_root_message');
33+
$newMessageTable = $this->moduleDataSetup->getTable('run_as_root_queue_error_message');
34+
35+
if (!$connection->isTableExists($oldMessageTable) || !$connection->isTableExists($newMessageTable)) {
36+
$this->moduleDataSetup->endSetup();
37+
return $this;
38+
}
39+
40+
$select = $connection->select()->from($oldMessageTable);
41+
42+
$connection->query($connection->insertFromSelect($select, $newMessageTable));
43+
44+
$connection->dropTable($oldMessageTable);
45+
46+
$this->moduleDataSetup->endSetup();
47+
48+
return $this;
49+
}
50+
51+
public static function getDependencies(): array
52+
{
53+
return [];
54+
}
55+
}

src/etc/db_schema.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version="1.0"?>
22
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
4-
<table name="run_as_root_queue_error_message" onCreate="migrateDataFromAnotherTable(run_as_root_message)"
5-
resource="default" engine="innodb">
4+
<table name="run_as_root_queue_error_message" resource="default" engine="innodb">
65
<column xsi:type="bigint" name="entity_id" unsigned="false" nullable="false" identity="true"/>
76
<column xsi:type="varchar" name="topic_name" length="255" nullable="false"/>
87
<column xsi:type="mediumtext" name="message_body" nullable="true"/>

src/etc/db_schema_whitelist.json

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,4 @@
11
{
2-
"run_as_root_message": {
3-
"column": {
4-
"entity_id": true,
5-
"topic_name": true,
6-
"message_body": true,
7-
"failure_description": true,
8-
"creation_time": true,
9-
"update_time": true,
10-
"resource_id": true,
11-
"created_at": true,
12-
"updated_at": true
13-
},
14-
"index": {
15-
"RUN_AS_ROOT_MESSAGE_TOPIC_NAME": true
16-
},
17-
"constraint": {
18-
"PRIMARY": true
19-
}
20-
},
212
"run_as_root_queue_error_message": {
223
"column": {
234
"entity_id": true,
@@ -34,4 +15,4 @@
3415
"PRIMARY": true
3516
}
3617
}
37-
}
18+
}

0 commit comments

Comments
 (0)