Skip to content

Commit 29bbf56

Browse files
Merge pull request #41 from run-as-root/develop
Adds patch data script to handle table data migration
2 parents 1754c15 + 4d60d49 commit 29bbf56

File tree

4 files changed

+104
-22
lines changed

4 files changed

+104
-22
lines changed

Diff for: src/Setup/Patch/Data/CopyDataFromOldMessageTable.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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('run_as_root_message')" in the db_schema.xml
14+
*/
15+
class CopyDataFromOldMessageTable 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+
$connection->query($connection->insertFromSelect($select, $newMessageTable));
42+
43+
$this->moduleDataSetup->endSetup();
44+
45+
return $this;
46+
}
47+
48+
public static function getDependencies(): array
49+
{
50+
return [];
51+
}
52+
}

Diff for: src/Setup/Patch/Data/DeleteOldMessageTable.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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('run_as_root_message')" in the db_schema.xml
14+
*/
15+
class DeleteOldMessageTable 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+
34+
if (!$connection->isTableExists($oldMessageTable)) {
35+
$this->moduleDataSetup->endSetup();
36+
return $this;
37+
}
38+
39+
$connection->dropTable($oldMessageTable);
40+
41+
$this->moduleDataSetup->endSetup();
42+
43+
return $this;
44+
}
45+
46+
public static function getDependencies(): array
47+
{
48+
return [ CopyDataFromOldMessageTable::class ];
49+
}
50+
}

Diff for: src/etc/db_schema.xml

+1-2
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"/>

Diff for: src/etc/db_schema_whitelist.json

+1-20
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)