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