Elgg  Version 5.1
20170728010000_remove_site_guid.php
Go to the documentation of this file.
1 <?php
2 
6 
10 class RemoveSiteGuid extends AbstractMigration {
11 
18  public function validate() {
19 
20  // validate if multiple sites are in the database
21  $tables = [
22  'access_collections',
23  'api_users',
24  'config',
25  'entities',
26  'users_apisessions'
27  ];
28 
29  foreach ($tables as $table) {
30  if (!$this->hasTable($table)) {
31  continue;
32  }
33 
34  $prefix = $this->getAdapter()->getOption('table_prefix');
35  $row = $this->fetchRow("
36  SELECT count(DISTINCT site_guid) as count
37  FROM {$prefix}{$table}
38  ");
39 
40  if ($row && $row['count'] > 1) {
41  throw new InstallationException("Multiple sites detected in table: '{$prefix}{$table}'. Can't upgrade the database.");
42  }
43  }
44  }
45 
49  public function up() {
50 
51  $this->validate();
52 
53  if ($this->hasTable('access_collections')) {
54  $table = $this->table('access_collections');
55 
56  if ($table->hasIndexByName('site_guid')) {
57  $table->removeIndexByName('site_guid')->save();
58  }
59 
60  if ($table->hasColumn('site_guid')) {
61  $table->removeColumn('site_guid')->save();
62  }
63  }
64 
65  if ($this->hasTable('api_users')) {
66  $table = $this->table('api_users');
67 
68  if ($table->hasColumn('site_guid')) {
69  $table->removeColumn('site_guid')->save();
70  }
71  }
72 
73  if ($this->hasTable('config')) {
74  $prefix = $this->getAdapter()->getOption('table_prefix');
75  $this->query("ALTER TABLE {$prefix}config DROP PRIMARY KEY, ADD PRIMARY KEY(name)");
76  $table = $this->table('config');
77 
78  if ($table->hasIndexByName('site_guid')) {
79  $table->removeIndexByName('site_guid')->save();
80  }
81 
82  if ($table->hasColumn('site_guid')) {
83  $table->removeColumn('site_guid')->save();
84  }
85  }
86 
87  if ($this->hasTable('entities')) {
88  $table = $this->table('entities');
89 
90  if ($table->hasIndexByName('site_guid')) {
91  $table->removeIndexByName('site_guid')->save();
92  }
93 
94  if ($table->hasColumn('site_guid')) {
95  $table->removeColumn('site_guid')->save();
96  }
97  }
98 
99  if ($this->hasTable('users_apisessions')) {
100  $table = $this->table('users_apisessions');
101 
102  if ($table->hasIndexByName('site_guid')) {
103  $table->removeIndexByName('site_guid')->save();
104  }
105 
106  $prefix = $this->getAdapter()->getOption('table_prefix');
107  $this->query("ALTER TABLE {$prefix}users_apisessions DROP KEY user_guid, ADD UNIQUE KEY user_guid(user_guid)");
108 
109  if ($table->hasColumn('site_guid')) {
110  $table->removeColumn('site_guid')->save();
111  }
112 
113  if ($table->hasIndexByName('user_guid')) {
114  $table->removeIndexByName('user_guid')->save();
115  }
116 
117  $table->addIndex(['user_guid'], [
118  'name' => "user_guid",
119  'unique' => false,
120  ])->save();
121  }
122 
123  if ($this->hasTable('entity_relationships')) {
124  // Remove member_of_site relaitonship following site_guid removal
125  $prefix = $this->getAdapter()->getOption('table_prefix');
126  $this->query("
127  DELETE FROM {$prefix}entity_relationships
128  WHERE relationship = 'member_of_site'
129  ");
130  }
131 
132  }
133 
137  public function down() {
138 
139  if ($this->hasTable('access_collections')) {
140  $table = $this->table('access_collections');
141 
142  if (!$table->hasColumn('site_guid')) {
143  $table->addColumn('site_guid', 'integer', [
144  'null' => false,
145  'limit' => MysqlAdapter::INT_BIG,
146  'precision' => 20,
147  'signed' => false,
148  ])->save();
149  }
150 
151  if (!$table->hasIndexByName('site_guid')) {
152  $table->addIndex(['site_guid'], [
153  'name' => 'site_guid',
154  'unique' => false,
155  ])->save();
156  }
157 
158  $prefix = $this->getAdapter()->getOption('table_prefix');
159  $this->query("
160  UPDATE {$prefix}access_collections
161  SET site_guid = 1
162  WHERE site_guid != 1
163  ");
164  }
165 
166  if ($this->hasTable('api_users')) {
167  $table = $this->table('api_users');
168 
169  if (!$table->hasColumn('site_guid')) {
170  $table->addColumn('site_guid', 'integer', [
171  'null' => false,
172  'limit' => MysqlAdapter::INT_BIG,
173  'precision' => 20,
174  'signed' => false,
175  ])->save();
176  }
177 
178  $prefix = $this->getAdapter()->getOption('table_prefix');
179  $this->query("
180  UPDATE {$prefix}api_users
181  SET site_guid = 1
182  WHERE site_guid != 1
183  ");
184  }
185 
186  if ($this->hasTable('config')) {
187  $table = $this->table('config', [
188  'primary_key' => [
189  "name",
190  "site_guid"
191  ],
192  ])->save();
193 
194  if (!$table->hasColumn('site_guid')) {
195  $table->addColumn('site_guid', 'integer', [
196  'null' => false,
197  'limit' => MysqlAdapter::INT_BIG,
198  'precision' => 20,
199  'signed' => false,
200  ])->save();
201  }
202 
203  if (!$table->hasIndexByName('site_guid')) {
204  $table->addIndex(['site_guid'], [
205  'name' => 'site_guid',
206  'unique' => false,
207  ])->save();
208  }
209 
210  $prefix = $this->getAdapter()->getOption('table_prefix');
211  $this->query("
212  UPDATE {$prefix}config
213  SET site_guid = 1
214  WHERE site_guid != 1
215  ");
216  }
217 
218  if ($this->hasTable('entities')) {
219  // remove site guid from entities
220  $table = $this->table('entities');
221 
222  if (!$table->hasColumn('site_guid')) {
223  $table->addColumn('site_guid', 'integer', [
224  'null' => false,
225  'limit' => MysqlAdapter::INT_BIG,
226  'precision' => 20,
227  'signed' => false,
228  ])->save();
229  }
230 
231  if (!$table->hasIndexByName('site_guid')) {
232  $table->addIndex(['site_guid'], [
233  'name' => 'site_guid',
234  'unique' => false,
235  ])->save();
236  }
237 
238  $prefix = $this->getAdapter()->getOption('table_prefix');
239  $this->query("
240  UPDATE {$prefix}entities
241  SET site_guid = 1
242  WHERE site_guid != 1
243  ");
244 
245  if ($this->hasTable('entity_relationships')) {
246  $rows = $this->fetchAll("
247  SELECT guid FROM {$prefix}entities
248  WHERE type = 'user'
249  ");
250 
251  foreach ($rows as $row) {
252  $this->table('entity_relationships')->insert([[
253  'guid_one' => $row['guid'],
254  'relationship' => 'member_of_site',
255  'guid_two' => 1,
256  'time_created' => time(),
257  ]])->saveData();
258  }
259  }
260  }
261 
262  if ($this->hasTable('users_apisessions')) {
263  // remove site guid from users_apisessions
264  $table = $this->table('users_apisessions');
265 
266  if ($table->hasIndexByName('site_guid')) {
267  $table->removeIndexByName('site_guid')->save();
268  }
269 
270  if (!$table->hasColumn('site_guid')) {
271  $table->addColumn('site_guid', 'integer', [
272  'null' => false,
273  'limit' => MysqlAdapter::INT_BIG,
274  'precision' => 20,
275  'signed' => false,
276  ])->save();
277  }
278 
279  if ($table->hasIndexByName('user_guid')) {
280  $table->removeIndexByName('user_guid')->save();
281  }
282 
283  $table->addIndex([
284  'user_guid',
285  'site_guid'
286  ], [
287  'name' => "user_guid",
288  'unique' => true,
289  ])->save();
290 
291  $prefix = $this->getAdapter()->getOption('table_prefix');
292  $this->query("
293  UPDATE {$prefix}users_apisessions
294  SET site_guid = 1
295  WHERE site_guid != 1
296  ");
297  }
298  }
299 }
$rows
Definition: redis.php:25
validate()
Ensure that legacy schema only has 1 site entity Refuse to upgrade if it doesn&#39;t. ...
down()
Add site_guid column and index.
Updates the basic settings for the primary site object.
Thrown when there is a major problem with the installation.
$table
Definition: user.php:37
$tables
Definition: database.php:6
up()
Removes site guid from legacy 2.x tables.
Removes multisite support from 2.x schema.