Elgg  Version 3.0
20170728020000_migrate_datalists_to_config.php
Go to the documentation of this file.
1 <?php
2 
5 
6 class MigrateDatalistsToConfig extends AbstractMigration {
7 
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 
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 ('version')
49  ");
50 
51  foreach ($rows as $row) {
52  $value = $row['value'];
53  if ($row['name'] !== 'processed_upgrades') {
54  $value = serialize($row['value']);
55  }
56 
57  $this->insert('config', [
58  'name' => $row['name'],
59  'value' => $value,
60  ]);
61  }
62 
63  // all data migrated, so drop the table
64  $this->dropTable('datalists');
65  }
66 
79  public function down() {
80 
81  if ($this->hasTable("datalists")) {
82  return;
83  }
84 
85  $table = $this->table("datalists", [
86  'id' => false,
87  'primary_key' => ["name"],
88  'engine' => "MyISAM",
89  'encoding' => "utf8",
90  'collation' => "utf8_general_ci",
91  ]);
92 
93  $table->addColumn('name', 'string', [
94  'null' => false,
95  'limit' => MysqlAdapter::TEXT_SMALL,
96  ]);
97 
98  $table->addColumn('value', 'text', [
99  'null' => false,
100  'limit' => MysqlAdapter::TEXT_REGULAR,
101  ]);
102 
103  $table->save();
104 
105  }
106 }
$rows
Definition: redis.php:20
up()
Migrates legacy 2.x datalists values to config table.
$value
Definition: debugging.php:7
elgg ElggPriorityList prototype insert
Inserts an element into the priority list at the priority specified.
$table
Definition: cron.php:57
validate()
Validates that there are no duplicate names in datalist and config tables.