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