Elgg  Version master
20170728020000_migrate_datalists_to_config.php
Go to the documentation of this file.
1 <?php
2 
6 
7 class MigrateDatalistsToConfig extends AbstractMigration {
8 
14  public function validate() {
15 
16  $prefix = $this->getAdapter()->getOption('table_prefix');
17 
18  $duplicates = $this->fetchAll("
19  SELECT name
20  FROM {$prefix}datalists
21  WHERE name IN (SELECT name FROM {$prefix}config)
22  AND name NOT IN ('processed_upgrades', 'version')
23  ");
24 
25 
26  if (!empty($duplicates)) {
27  $duplicates_array = [];
28  foreach ($duplicates as $duplicate) {
29  $duplicates_array[] = $duplicate['name'];
30  }
31  $duplicates = implode(', ', $duplicates_array);
32  throw new InstallationException("Found names ({$duplicates}) in datalist that also exist in config. Don't know how to merge.");
33  }
34 
35  }
36 
40  public function up() {
41 
42  if (!$this->hasTable('datalists') || !$this->hasTable('config')) {
43  return;
44  }
45 
46  $prefix = $this->getAdapter()->getOption('table_prefix');
47  $rows = $this->fetchAll("
48  SELECT * FROM {$prefix}datalists
49  WHERE name NOT IN ('version')
50  ");
51 
52  foreach ($rows as $row) {
53  $value = $row['value'];
54  if ($row['name'] !== 'processed_upgrades') {
55  $value = serialize($row['value']);
56  }
57 
58  $this->table('config')->insert([[
59  'name' => $row['name'],
60  'value' => $value,
61  ]])->saveData();
62  }
63 
64  // all data migrated, so drop the table
65  $this->table('datalists')->drop()->save();
66  }
67 
80  public function down() {
81 
82  if ($this->hasTable("datalists")) {
83  return;
84  }
85 
86  $table = $this->table("datalists", [
87  'id' => false,
88  'primary_key' => ["name"],
89  'engine' => "MyISAM",
90  'encoding' => "utf8",
91  'collation' => "utf8_general_ci",
92  ]);
93 
94  $table->addColumn('name', 'string', [
95  'null' => false,
96  'limit' => MysqlAdapter::TEXT_SMALL,
97  ]);
98 
99  $table->addColumn('value', 'text', [
100  'null' => false,
101  'limit' => MysqlAdapter::TEXT_REGULAR,
102  ]);
103 
104  $table->save();
105 
106  }
107 }
$rows
Definition: redis.php:25
$value
Definition: generic.php:51
Updates the basic settings for the primary site object.
Thrown when there is a major problem with the installation.
up()
Migrates legacy 2.x datalists values to config table.
$table
Definition: user.php:37
validate()
Validates that there are no duplicate names in datalist and config tables.