1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Phinx\Db\Adapter\MysqlAdapter; |
4
|
|
|
use Phinx\Migration\AbstractMigration; |
5
|
|
|
|
6
|
|
|
class MigrateDatalistsToConfig extends AbstractMigration { |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Validates that there are no duplicate names in datalist and config tables |
10
|
|
|
* |
11
|
|
|
* @throws InstallationException |
12
|
|
|
*/ |
13
|
|
|
public function validate() { |
14
|
|
|
|
15
|
|
|
$prefix = $this->getAdapter()->getOption('table_prefix'); |
16
|
|
|
|
17
|
|
|
$duplicates = $this->fetchAll(" |
18
|
|
|
SELECT name |
19
|
|
|
FROM {$prefix}datalists |
20
|
|
|
WHERE name IN (SELECT name FROM {$prefix}config) |
21
|
|
|
AND name NOT IN ('processed_upgrades', 'version') |
22
|
|
|
"); |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
if (!empty($duplicates)) { |
26
|
|
|
$duplicates_array = []; |
27
|
|
|
foreach ($duplicates as $duplicate) { |
28
|
|
|
$duplicates_array[] = $duplicate['name']; |
29
|
|
|
} |
30
|
|
|
$duplicates = implode(', ', $duplicates_array); |
31
|
|
|
throw new InstallationException("Found names ({$duplicates}) in datalist that also exist in config. Don't know how to merge."); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Migrates legacy 2.x datalists values to config table |
38
|
|
|
*/ |
39
|
|
|
public function up() { |
40
|
|
|
|
41
|
|
|
if (!$this->hasTable('datalists') || !$this->hasTable('config')) { |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$prefix = $this->getAdapter()->getOption('table_prefix'); |
46
|
|
|
$rows = $this->fetchAll(" |
47
|
|
|
SELECT * FROM {$prefix}datalists |
48
|
|
|
WHERE name NOT IN ('processed_upgrades', 'version') |
49
|
|
|
"); |
50
|
|
|
|
51
|
|
|
foreach ($rows as $row) { |
52
|
|
|
$this->insert('config', [ |
53
|
|
|
'name' => $row['name'], |
54
|
|
|
'value' => serialize($row['value']), |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// all data migrated, so drop the table |
59
|
|
|
$this->dropTable('datalists'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Recreate datalists table |
64
|
|
|
* |
65
|
|
|
* @warning Note that the datalists values will not be populated back into the table |
66
|
|
|
* There is no way of telling which config values have come from datalists table |
67
|
|
|
* |
68
|
|
|
* CREATE TABLE `prefix_datalists` ( |
69
|
|
|
* `name` varchar(255) NOT NULL, |
70
|
|
|
* `value` text NOT NULL, |
71
|
|
|
* PRIMARY KEY (`name`) |
72
|
|
|
* ) ENGINE=MyISAM DEFAULT CHARSET=utf8; |
73
|
|
|
*/ |
74
|
|
|
public function down() { |
75
|
|
|
|
76
|
|
|
if ($this->hasTable("datalists")) { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$table = $this->table("datalists", [ |
81
|
|
|
'id' => false, |
82
|
|
|
'primary_key' => ["name"], |
83
|
|
|
'engine' => "MyISAM", |
84
|
|
|
'encoding' => "utf8", |
85
|
|
|
'collation' => "utf8_general_ci", |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
$table->addColumn('name', 'string', [ |
89
|
|
|
'null' => false, |
90
|
|
|
'limit' => MysqlAdapter::TEXT_SMALL, |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
$table->addColumn('value', 'text', [ |
94
|
|
|
'null' => false, |
95
|
|
|
'limit' => MysqlAdapter::TEXT_REGULAR, |
96
|
|
|
]); |
97
|
|
|
|
98
|
|
|
$table->save(); |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|