1 | <?php |
||
2 | /* |
||
3 | * @copyright 2020 Mautic, Inc. All rights reserved |
||
4 | * @author Mautic, Inc. |
||
5 | * |
||
6 | * @link https://mautic.com |
||
7 | * |
||
8 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
9 | */ |
||
10 | |||
11 | declare(strict_types=1); |
||
12 | |||
13 | namespace Mautic\Migrations; |
||
14 | |||
15 | use Doctrine\DBAL\DBALException; |
||
16 | use Doctrine\DBAL\FetchMode; |
||
17 | use Doctrine\DBAL\Schema\Schema; |
||
18 | use Doctrine\Migrations\Exception\SkipMigration; |
||
19 | use Mautic\CoreBundle\Doctrine\AbstractMauticMigration; |
||
20 | |||
21 | class Version20200422144300 extends AbstractMauticMigration |
||
22 | { |
||
23 | /** |
||
24 | * @var array|false |
||
25 | */ |
||
26 | private $rowsToMigrateLookup; |
||
27 | |||
28 | /** |
||
29 | * @var array|false |
||
30 | */ |
||
31 | private $rowsToMigrateSelectMultiselect; |
||
32 | |||
33 | public function getDescription(): string |
||
34 | { |
||
35 | return 'Migrate mautic_lead_fields.properties to simple array'; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @throws SkipMigration |
||
40 | * @throws DBALException |
||
41 | */ |
||
42 | public function preUp(Schema $schema): void |
||
43 | { |
||
44 | $this->fetchLookupsToMigrate(); |
||
45 | $this->fetchSelectsToMigrate(); |
||
46 | |||
47 | if (false === $this->rowsToMigrateLookup && false === $this->fetchSelectsToMigrate()) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
48 | throw new SkipMigration('No data to migrate'); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | public function up(Schema $schema): void |
||
53 | { |
||
54 | if (false !== $this->rowsToMigrateLookup) { |
||
55 | $this->migrateLookups(); |
||
56 | } |
||
57 | |||
58 | if (false !== $this->rowsToMigrateSelectMultiselect) { |
||
59 | $this->migrateSelects(); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | private function fetchLookupsToMigrate() |
||
64 | { |
||
65 | $sql = <<<SQL |
||
66 | SELECT id, properties |
||
67 | FROM {$this->prefix}lead_fields |
||
68 | WHERE |
||
69 | type = 'lookup' AND |
||
70 | properties LIKE '%|%' |
||
71 | SQL; |
||
72 | |||
73 | $stmt = $this->connection->prepare($sql); |
||
74 | $stmt->execute(); |
||
75 | $this->rowsToMigrateLookup = $stmt->fetchAll(FetchMode::ASSOCIATIVE); |
||
76 | } |
||
77 | |||
78 | private function migrateLookups() |
||
79 | { |
||
80 | foreach ($this->rowsToMigrateLookup as $rowToMigrate) { |
||
81 | $properties = unserialize($rowToMigrate['properties']); |
||
82 | $properties['list'] = explode('|', $properties['list']); |
||
83 | |||
84 | $params = [ |
||
85 | 'id' => $rowToMigrate['id'], |
||
86 | 'properties' => serialize($properties), |
||
87 | ]; |
||
88 | |||
89 | $this->addSql($this->getUpdateSql(), $params); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | private function fetchSelectsToMigrate() |
||
94 | { |
||
95 | $sql = <<<SQL |
||
96 | SELECT id, properties |
||
97 | FROM {$this->prefix}lead_fields |
||
98 | WHERE |
||
99 | ( |
||
100 | type = 'select' OR |
||
101 | type = 'multiselect' |
||
102 | ) AND |
||
103 | properties LIKE '%|%' |
||
104 | SQL; |
||
105 | |||
106 | $stmt = $this->connection->prepare($sql); |
||
107 | $stmt->execute(); |
||
108 | $this->rowsToMigrateSelectMultiselect = $stmt->fetchAll(FetchMode::ASSOCIATIVE); |
||
109 | } |
||
110 | |||
111 | private function migrateSelects() |
||
112 | { |
||
113 | foreach ($this->rowsToMigrateSelectMultiselect as $rowToMigrate) { |
||
114 | $properties = unserialize($rowToMigrate['properties']); |
||
115 | $propertyList = explode('|', $properties['list']); |
||
116 | |||
117 | $convertedPropertyList = []; |
||
118 | |||
119 | foreach ($propertyList as $property) { |
||
120 | $convertedPropertyList[] = [ |
||
121 | 'label' => $property, |
||
122 | 'value' => $property, |
||
123 | ]; |
||
124 | } |
||
125 | |||
126 | $properties['list'] = $convertedPropertyList; |
||
127 | |||
128 | $params = [ |
||
129 | 'id' => $rowToMigrate['id'], |
||
130 | 'properties' => serialize($properties), |
||
131 | ]; |
||
132 | |||
133 | $this->addSql($this->getUpdateSql(), $params); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | private function getUpdateSql(): string |
||
138 | { |
||
139 | return <<<SQL |
||
140 | UPDATE {$this->prefix}lead_fields |
||
141 | SET properties = :properties |
||
142 | WHERE id = :id |
||
143 | SQL; |
||
144 | } |
||
145 | } |
||
146 |