File size: 2,215 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php

declare(strict_types=1);

namespace Mautic\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Mautic\CoreBundle\Doctrine\PreUpAssertionMigration;

final class Version20230606111852 extends PreUpAssertionMigration
{
    public const OLD_STRING = 'Connect a &quot;Send Email&quot; action to the top of this decision.';

    public const NEW_STRING = 'Connect a Send Email action to the top of this decision.';

    protected static string $tableName = 'campaign_events';

    protected function preUpAssertions(): void
    {
        $this->skipAssertion(function (Schema $schema) {
            $sql         = sprintf("select id from %s where properties like '%s' limit 1", $this->getPrefixedTableName(), '%'.self::OLD_STRING.'%');
            $recordCount = $this->connection->executeQuery($sql)->fetchAllAssociative();

            return !$recordCount;
        }, 'Migration is not required.');
    }

    public function up(Schema $schema): void
    {
        $sql            = sprintf("select id, properties from %s where properties like '%s'", $this->getPrefixedTableName(), '%'.self::OLD_STRING.'%');
        $results        = $this->connection->executeQuery($sql)->fetchAllAssociative();
        $updatedRecords = 0;
        foreach ($results as $row) {
            $propertiesArray                            = unserialize($row['properties']);
            $propertiesArray['settings']['description'] = str_replace(self::OLD_STRING, self::NEW_STRING, $propertiesArray['settings']['description']);
            $propertiesString                           = serialize($propertiesArray);

            $sql  = sprintf('UPDATE %s SET properties = :properties where id = :id', $this->getPrefixedTableName());
            $stmt = $this->connection->prepare($sql);
            $stmt->bindValue('properties', $propertiesString, \PDO::PARAM_STR);
            $stmt->bindValue('id', $row['id'], \PDO::PARAM_INT);
            $updatedRecords += $stmt->executeStatement();
        }
        $this->write(sprintf('<comment>%s record(s) have been updated successfully.</comment>', $updatedRecords));
    }

    private function getPrefixedTableName(): string
    {
        return $this->prefix.self::$tableName;
    }
}