Total Complexity | 47 |
Total Lines | 347 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like UpdatesForVersion50 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UpdatesForVersion50, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class UpdatesForVersion50 extends Migration |
||
17 | { |
||
18 | /** |
||
19 | * Database changes for version 5.0 |
||
20 | * |
||
21 | * @return void |
||
22 | */ |
||
23 | public function up() |
||
24 | { |
||
25 | // See the function itself for a description of the database changes |
||
26 | // DB Adds |
||
27 | $this->addSoftDeleteToUsers(); |
||
28 | $this->addSoftDeleteToCustomerSystems(); |
||
29 | $this->addSoftDeleteToTechTips(); |
||
30 | $this->addPasswordExpiresColumn(); |
||
31 | $this->addHiddenColumn(); |
||
32 | $this->addColumnsToFileLinksTable(); |
||
33 | $this->addNotesColumnToFileLinkFiles(); |
||
34 | |||
35 | // DB Modifications |
||
36 | $this->updatePhoneIcons(); |
||
37 | $this->modifySystemDataTableNames(); |
||
38 | $this->migrateSystemDocumentation(); |
||
39 | $this->migrateUserRoles(); |
||
40 | $this->updateCustomersTable(); |
||
41 | |||
42 | // Remove Unneeded Tables |
||
43 | $this->removeNavBarView(); |
||
44 | $this->removeUserRolesTables(); |
||
45 | $this->removeFileLinkInstructionsTable(); |
||
46 | $this->removeFileLinkNotesTable(); |
||
47 | $this->removeSystemFilesTables(); |
||
48 | $this->removeActiveFromCustomers(); |
||
49 | |||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Reverse the migrations. |
||
54 | * |
||
55 | * @return void |
||
56 | */ |
||
57 | public function down() |
||
58 | { |
||
59 | return 'Unable to Process. Please Downgrade app and load backup.'; |
||
60 | } |
||
61 | |||
62 | /* |
||
63 | * Database Modifications |
||
64 | */ |
||
65 | // Update the icon class to change from font awesome, to theymify icons |
||
66 | private function updatePhoneIcons() |
||
67 | { |
||
68 | $newIcons = [ |
||
69 | ['description' => 'Home', 'icon_class' => 'ti-home'], |
||
70 | ['description' => 'Work', 'icon_class' => 'ti-briefcase'], |
||
71 | ['description' => 'Mobile', 'icon_class' => 'ti-mobile'], |
||
72 | ]; |
||
73 | |||
74 | foreach($newIcons as $new) |
||
75 | { |
||
76 | PhoneNumberTypes::where('description', $new['description'])->update(['icon_class' => $new['icon_class']]); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | /* |
||
81 | * New Columns and Tables |
||
82 | */ |
||
83 | // Added the ability to set an expiration date for user passwords - will force them to change after this expires |
||
84 | private function addPasswordExpiresColumn() |
||
85 | { |
||
86 | if(!Schema::hasColumn('users', 'password_expires')) { |
||
87 | Schema::table('users', function(Blueprint $table) { |
||
88 | $table->timestamp('password_expires') |
||
89 | ->nullable() |
||
90 | ->after('active'); |
||
91 | }); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | // Add the is installer column to the users table |
||
96 | private function migrateUserRoles() |
||
97 | { |
||
98 | if(Schema::hasTable('user_role')) |
||
99 | { |
||
100 | if(!Schema::hasColumn('users', 'role_id')) |
||
101 | { |
||
102 | Schema::table('users', function (Blueprint $table) { |
||
103 | $table->integer('role_id')->after('user_id')->unsigned()->default(4); |
||
104 | $table->foreign('role_id')->references('role_id')->on('user_role_descriptions')->onUpdate('cascade'); |
||
105 | }); |
||
106 | } |
||
107 | |||
108 | $roleData = DB::select('SELECT * FROM `user_role`'); |
||
109 | |||
110 | foreach($roleData as $data) |
||
111 | { |
||
112 | User::where('user_id', $data->user_id)->update([ |
||
113 | 'role_id' => $data->role_id, |
||
114 | ]); |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
119 | // Added a 'hidden' attribute to system customer data types to allow passwords to not be viewed unless clicked or focus |
||
120 | private function addHiddenColumn() |
||
121 | { |
||
122 | if(!Schema::hasColumn('system_data_field_types', 'hidden')) { |
||
123 | Schema::table('system_cust_data_types', function(Blueprint $table) { |
||
124 | $table->boolean('hidden') |
||
125 | ->default(0) |
||
126 | ->after('name'); |
||
127 | }); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | // Update the File links table - add cust_id and note column |
||
132 | private function addColumnsToFileLinksTable() |
||
133 | { |
||
134 | if(!Schema::hasColumn('file_links', 'cust_id')) { |
||
135 | Schema::table('file_links', function(Blueprint $table) { |
||
136 | $table->integer('cust_id') |
||
137 | ->unsigned() |
||
138 | ->nullable() |
||
139 | ->after('user_id'); |
||
140 | $table->foreign('cust_id')->references('cust_id')->on('customers')->onUpdate('cascade')->onDelete('cascade'); |
||
141 | }); |
||
142 | } |
||
143 | if(!Schema::hasColumn('file_links', 'note')) { |
||
144 | Schema::table('file_links', function(Blueprint $table) { |
||
145 | $table->longText('note') |
||
146 | ->nullable() |
||
147 | ->after('link_name'); |
||
148 | }); |
||
149 | // Migrate the instructions from the old table to the new column |
||
150 | $instructions = DB::select('SELECT * FROM `file_link_instructions`'); |
||
151 | foreach($instructions as $ins) { |
||
152 | FileLinks::find($ins->link_id)->update([ |
||
153 | 'note' => $ins->instruction |
||
154 | ]); |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | // Add Notes column to the file links files table |
||
160 | private function addNotesColumnToFileLinkFiles() |
||
161 | { |
||
162 | if(!Schema::hasColumn('file_link_files', 'note')) { |
||
163 | Schema::table('file_link_files', function(Blueprint $table) { |
||
164 | $table->longText('note') |
||
165 | ->nullable() |
||
166 | ->after('upload'); |
||
167 | }); |
||
168 | // Migrate the existing notes to the new table |
||
169 | $notes = DB::select('SELECT * FROM `file_link_notes`'); |
||
170 | foreach($notes as $note) { |
||
171 | FileLinkFiles::where('file_id', $note->file_id)->update([ |
||
172 | 'note' => $note->note |
||
173 | ]); |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | |||
178 | // Add the documentation column to the tech tips table |
||
179 | private function migrateSystemDocumentation() |
||
180 | { |
||
181 | if(!Schema::hasColumn('tech_tips', 'tip_type_id')) { |
||
182 | Schema::table('tech_tips', function(Blueprint $table) { |
||
183 | $table->bigInteger('tip_type_id')->unsigned()->after('public')->default(1); |
||
184 | $table->foreign('tip_type_id')->references('tip_type_id')->on('tech_tip_types')->onUpdate('cascade'); |
||
185 | }); |
||
186 | |||
187 | // Move all of the system files over to the tech tips table |
||
188 | $sysFiles = DB::select('SELECT * FROM `system_files`'); |
||
189 | foreach($sysFiles as $sysFile) { |
||
190 | $newTip = TechTips::create([ |
||
191 | 'user_id' => $sysFile->user_id, |
||
192 | 'public' => 0, |
||
193 | 'tip_type_id' => 2, |
||
194 | 'subject' => $sysFile->name, |
||
195 | 'description' => empty($sysFile->description) ? $sysFile->name : $sysFile->description, |
||
196 | 'created_at' => $sysFile->created_at, |
||
197 | 'updated_at' => $sysFile->updated_at |
||
198 | ]); |
||
199 | |||
200 | $tipId = $newTip->tip_id; |
||
201 | TechTipFiles::create([ |
||
202 | 'tip_id' => $tipId, |
||
203 | 'file_id' => $sysFile->file_id |
||
204 | ]); |
||
205 | TechTipSystems::create([ |
||
206 | 'tip_id' => $tipId, |
||
207 | 'sys_id' => $sysFile->sys_id |
||
208 | ]); |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | |||
213 | /* |
||
214 | * Database Cleanup |
||
215 | */ |
||
216 | // Remove the NavBar view if it exists - We no longer need it to sort out the system types |
||
217 | private function removeNavBarView() |
||
218 | { |
||
219 | DB::statement('DROP VIEW IF EXISTS `navbar_view`'); |
||
220 | } |
||
221 | |||
222 | // Remove the active column from the customers table |
||
223 | private function removeActiveFromCustomers() |
||
224 | { |
||
225 | if(Schema::hasColumn('customers', 'active')) |
||
226 | { |
||
227 | // Migrate the existing disabled customers first |
||
228 | $deactivatedCustomers = Customers::where('active', 0)->get(); |
||
229 | foreach($deactivatedCustomers as $cust) |
||
230 | { |
||
231 | Customers::find($cust->cust_id)->delete(); |
||
232 | } |
||
233 | |||
234 | Schema::table('customers', function(Blueprint $table) { |
||
235 | $table->dropColumn('active'); |
||
236 | }); |
||
237 | } |
||
238 | } |
||
239 | |||
240 | // Remove the user roles tables |
||
241 | private function removeUserRolesTables() |
||
251 | } |
||
252 | } |
||
253 | |||
254 | // Remove the File Link Instructions Table |
||
255 | private function removeFileLinkInstructionsTable() |
||
256 | { |
||
257 | if(Schema::hasTable('file_link_instructions')) |
||
258 | { |
||
259 | // Remove the foreign key to allow for dropping the table |
||
260 | Schema::table('file_link_instructions', function(Blueprint $table) { |
||
261 | $table->dropForeign(['link_id']); |
||
262 | }); |
||
263 | Schema::dropIfExists('file_link_instructions'); |
||
264 | } |
||
265 | } |
||
266 | |||
267 | // Remove the file link files note table |
||
268 | private function removeFileLinkNotesTable() |
||
269 | { |
||
270 | if(Schema::hasTable('file_link_notes')) |
||
271 | { |
||
272 | Schema::table('file_link_notes', function(Blueprint $table) { |
||
273 | $table->dropForeign(['link_id']); |
||
274 | $table->dropForeign(['file_id']); |
||
275 | }); |
||
276 | Schema::dropIfExists('file_link_notes'); |
||
277 | } |
||
278 | } |
||
279 | |||
280 | // Remove the system files and system file types table |
||
281 | private function removeSystemFilesTables() |
||
282 | { |
||
283 | if(Schema::hasTable('system_files')) { |
||
284 | Schema::table('system_files', function(Blueprint $table) { |
||
285 | $table->dropForeign(['sys_id']); |
||
286 | $table->dropForeign(['type_id']); |
||
287 | $table->dropForeign(['file_id']); |
||
288 | $table->dropForeign(['user_id']); |
||
289 | }); |
||
290 | Schema::dropIfExists('system_files'); |
||
291 | Schema::dropIfExists('system_file_types'); |
||
292 | } |
||
293 | } |
||
294 | |||
295 | // Rename the tables that hold customer system information to make more sense |
||
296 | private function modifySystemDataTableNames() |
||
297 | { |
||
298 | if(Schema::hasTable('customer_system_fields')) |
||
299 | { |
||
300 | Schema::dropIfExists('customer_system_data'); |
||
301 | Schema::rename('customer_system_fields', 'customer_system_data'); |
||
302 | } |
||
303 | if(Schema::hasTable('system_cust_data_fields')) |
||
304 | { |
||
305 | Schema::dropIfExists('system_data_fields'); |
||
306 | Schema::rename('system_cust_data_fields', 'system_data_fields'); |
||
307 | } |
||
308 | if(Schema::hasTable('system_cust_data_types')) |
||
309 | { |
||
310 | Schema::dropIfExists('system_data_field_types'); |
||
311 | Schema::rename('system_cust_data_types', 'system_data_field_types'); |
||
312 | } |
||
313 | } |
||
314 | |||
315 | // Add soft deletes to customer systems table to prevent accidental deletes |
||
316 | private function addSoftDeleteToCustomerSystems() |
||
317 | { |
||
318 | if(!Schema::hasColumn('customer_systems', 'deleted_at')) |
||
319 | { |
||
320 | Schema::table('customer_systems', function(Blueprint $table) { |
||
321 | $table->softDeletes()->after('sys_id'); |
||
322 | }); |
||
323 | } |
||
324 | } |
||
325 | |||
326 | // Add soft deletes to tech tips table to prevent accidental deletes |
||
327 | private function addSoftDeleteToTechTips() |
||
328 | { |
||
329 | if (!Schema::hasColumn('tech_tips', 'deleted_at')) { |
||
330 | Schema::table('tech_tips', function (Blueprint $table) { |
||
331 | $table->softDeletes()->after('description'); |
||
332 | }); |
||
333 | } |
||
334 | } |
||
335 | |||
336 | // Swap out the active column for deleted at column on users table |
||
337 | public function addSoftDeleteToUsers() |
||
338 | { |
||
339 | if(!Schema::hasColumn('users', 'deleted_at')) |
||
340 | { |
||
341 | Schema::table('users', function(Blueprint $table) |
||
342 | { |
||
343 | $table->softDeletes()->after('password_expires'); |
||
344 | }); |
||
345 | // Migrate over all deactivated users |
||
346 | DB::update('UPDATE `users` SET `deleted_at` = "'.Carbon::now().'" WHERE `active` = 0'); |
||
347 | // Remove the Active column |
||
348 | Schema::table('users', function (Blueprint $table) { |
||
349 | $table->dropColumn('active'); |
||
350 | }); |
||
351 | } |
||
352 | } |
||
353 | |||
354 | // Add the Parent id column to the customers table |
||
355 | public function updateCustomersTable() |
||
363 | }); |
||
364 | } |
||
365 | } |
||
366 | } |
||
367 |