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