Elgg  Version 4.3
20171021111059_denormalize_entity_subtypes.php
Go to the documentation of this file.
1 <?php
2 
5 
6 class DenormalizeEntitySubtypes extends AbstractMigration {
7 
11  public function validate() {
12 
13  if (!$this->hasTable('entity_subtypes')) {
14  return;
15  }
16 
17  $reserved_subtypes = [
18  'user' => [
19  'user',
20  ],
21  'group' => [
22  'group',
23  ],
24  'site' => [
25  'site',
26  ],
27  ];
28 
29  $prefix = $this->getAdapter()->getOption('table_prefix');
30 
31  foreach ($reserved_subtypes as $type => $subtypes) {
32  $subtypes_in = array_map(function ($e) {
33  return "'$e'";
34  }, $subtypes);
35 
36  $subtypes_in = implode(', ', $subtypes_in);
37 
38  $row = $this->fetchRow("
39  SELECT count(*) as count
40  FROM {$prefix}entity_subtypes
41  WHERE type='$type'
42  AND subtype IN ($subtypes_in)
43  ");
44 
45  if (!empty($row['count'])) {
46  $class = __CLASS__;
47  throw new InstallationException("
48  Unable to perform migration {$class}, because the database contains entities with a reserved subtype name.
49  Please ensure that you are not using one of the reserved subtypes [{$subtypes_in}]
50  for entities of '{$type}' type before running the migration,
51  otherwise you may loose important entity subtype bindings.
52  ");
53  }
54  }
55 
56  $row = $this->fetchRow("
57  SELECT count(*) as count
58  FROM {$prefix}entities
59  WHERE type='object'
60  AND subtype=0 OR subtype IS NULL
61  ");
62 
63  if (!empty($row['count'])) {
64  $class = __CLASS__;
65  throw new InstallationException("
66  Unable to perform migration {$class}, because the database contains objects without a subtype.
67  Please ensure that all object entities have a valid subtype associated with them.
68  There are {$row->count} object without a subtype in your entities table.
69  ");
70  }
71  }
72 
76  public function change() {
77 
78  $this->validate();
79 
80  $prefix = $this->getAdapter()->getOption('table_prefix');
81 
82  $table = $this->table('entities');
83 
84  $table->renameColumn('subtype', 'subtype_id');
85  $table->save();
86 
87  $table->addColumn('subtype', 'string', [
88  'null' => false,
89  'limit' => 50,
90  'after' => 'type',
91  ]);
92 
93  $table->save();
94 
95  $this->query("
96  UPDATE {$prefix}entities e
97  JOIN {$prefix}entity_subtypes es ON e.subtype_id = es.id
98  SET e.subtype = es.subtype
99  ");
100 
101  foreach (['user', 'group', 'site'] as $type) {
102  $this->query("
103  UPDATE {$prefix}entities e
104  SET e.subtype = '{$type}'
105  WHERE e.type = '{$type}' AND e.subtype_id = 0
106  ");
107  }
108 
109  $table->removeColumn('subtype_id');
110 
111  $table->save();
112 
113  $this->table('entity_subtypes')->drop()->save();
114  }
115 }
$type
Definition: delete.php:21
$class
Definition: summary.php:44
Updates the basic settings for the primary site object.
Thrown when there is a major problem with the installation.
validate()
Validate that migration is possible.
$subtypes
$table
Definition: cron.php:56