| Total Complexity | 60 | 
| Total Lines | 690 | 
| Duplicated Lines | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like MigrateDatabaseCommand 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 MigrateDatabaseCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 19 | class MigrateDatabaseCommand extends Command  | 
            ||
| 20 | { | 
            ||
| 21 | use FileTrait;  | 
            ||
| 22 | |||
| 23 |     protected $signature   = 'tb_database:migrate {--y|yes}'; | 
            ||
| 24 | protected $description = 'Because of the massive changes from version 5 to 6, this script is needed to properly update the database';  | 
            ||
| 25 | |||
| 26 | protected $database;  | 
            ||
| 27 | protected $latestVersion;  | 
            ||
| 28 | |||
| 29 | // All of the tables in the Version 5 database  | 
            ||
| 30 | protected $ver5Tables = [  | 
            ||
| 31 | 'customer_contact_phones', 'customer_contacts', 'customer_favs', 'customer_file_types', 'customer_files', 'customer_notes', 'customer_system_data', 'customer_systems', 'customers',  | 
            ||
| 32 | 'file_link_files', 'file_links', 'files',  | 
            ||
| 33 | 'phone_number_types',  | 
            ||
| 34 | 'settings',  | 
            ||
| 35 | 'system_categories', 'system_data_field_types', 'system_data_fields', 'system_types',  | 
            ||
| 36 | 'tech_tip_comments', 'tech_tip_favs', 'tech_tip_files', 'tech_tip_systems', 'tech_tip_types', 'tech_tips',  | 
            ||
| 37 | 'user_initializes', 'user_logins', 'user_role_permission_types', 'user_role_permissions', 'user_role_types', 'user_settings', 'users',  | 
            ||
| 38 | ];  | 
            ||
| 39 | |||
| 40 | /**  | 
            ||
| 41 | * Create a new command instance  | 
            ||
| 42 | */  | 
            ||
| 43 | public function __construct()  | 
            ||
| 48 | }  | 
            ||
| 49 | |||
| 50 | /**  | 
            ||
| 51 | * Execute the console command  | 
            ||
| 52 | */  | 
            ||
| 53 | public function handle()  | 
            ||
| 54 |     { | 
            ||
| 55 |         $this->line('---------------------------------------------------------------'); | 
            ||
| 56 |         $this->line('|                                                              |'); | 
            ||
| 57 |         $this->line('|               Database Migration Utility                     |'); | 
            ||
| 58 |         $this->line('|                                                              |'); | 
            ||
| 59 |         $this->line('---------------------------------------------------------------'); | 
            ||
| 60 | |||
| 61 | // Make sure that this process is actually needed  | 
            ||
| 62 | if(!$this->isMigrationNecessary())  | 
            ||
| 63 |         { | 
            ||
| 64 | $this->newLine();  | 
            ||
| 65 |             $this->info('Your Database is at the proper version'); | 
            ||
| 66 |             $this->info('Run `php artisan migrate` to update the tables'); | 
            ||
| 67 | $this->newLine();  | 
            ||
| 68 | return 0;  | 
            ||
| 69 | }  | 
            ||
| 70 | |||
| 71 | // Using the --yes option will bypass the confirmation message  | 
            ||
| 72 |         if(!$this->option('yes')) | 
            ||
| 73 |         { | 
            ||
| 74 |             $this->error('-------------------------------------------------------------'); | 
            ||
| 75 |             $this->error('| VERY IMPORTANT:  PLEASE BACKUP ALL DATA BEFORE PROCEEDING |'); | 
            ||
| 76 |             $this->error('|            POSSIBLE DATA LOSS MAY OCCUR                   |'); | 
            ||
| 77 |             $this->error('-------------------------------------------------------------'); | 
            ||
| 78 | |||
| 79 |             if(!$this->confirm('Are you sure you want to continue?')) | 
            ||
| 80 |             { | 
            ||
| 81 |                 $this->line('Exiting'); | 
            ||
| 82 | return 0;  | 
            ||
| 83 | }  | 
            ||
| 84 | }  | 
            ||
| 85 | |||
| 86 | // Check if the _old database already exists  | 
            ||
| 87 |         $database = DB::select('SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = "'.$this->database.'_old"'); | 
            ||
| 88 | if(count($database) > 0)  | 
            ||
| 89 |         { | 
            ||
| 90 |             $this->error('ERROR - The MySQL Database already has a database named '.$this->database.'_old'); | 
            ||
| 91 |             $this->error('You must manually delete this database in order to continue'); | 
            ||
| 92 | return 0;  | 
            ||
| 93 | }  | 
            ||
| 94 | |||
| 95 | // Check to make sure we can create a database and write to it  | 
            ||
| 96 | try  | 
            ||
| 97 |         { | 
            ||
| 98 |             DB::statement('CREATE SCHEMA `'.$this->database.'_old`'); | 
            ||
| 99 | }  | 
            ||
| 100 | catch(Exception $e)  | 
            ||
| 101 |         { | 
            ||
| 102 |             $this->error('Unable to create temporary database'); | 
            ||
| 103 |             $this->error('Please check that database user has permissions to create a new Schema'); | 
            ||
| 104 | $this->newLine();  | 
            ||
| 105 |             $this->error('Message - '.$e); | 
            ||
| 106 | }  | 
            ||
| 107 | |||
| 108 |         $this->info('Moving data to a temporary database'); | 
            ||
| 109 | $this->moveOldData();  | 
            ||
| 110 | |||
| 111 | // Create a default database  | 
            ||
| 112 |         $this->info('Building Default Database'); | 
            ||
| 113 |         Artisan::call('migrate:fresh'); | 
            ||
| 114 | $this->newLine();  | 
            ||
| 115 | |||
| 116 | // Migrate Data  | 
            ||
| 117 | $this->migrateAppSettings();  | 
            ||
| 118 | $this->migrateUserRoles();  | 
            ||
| 119 | $this->migrateUsers();  | 
            ||
| 120 | $this->migrateEquipment();  | 
            ||
| 121 | $this->migrateFiles();  | 
            ||
| 122 | $this->migrateCustomers();  | 
            ||
| 123 | $this->migrateTechTips();  | 
            ||
| 124 | $this->fileCleanup();  | 
            ||
| 125 | $this->checkFileLinkModule();  | 
            ||
| 126 | $this->cleanup();  | 
            ||
| 127 | |||
| 128 |         $this->line('---------------------------------------------------------------'); | 
            ||
| 129 |         $this->line('|                                                              |'); | 
            ||
| 130 |         $this->line('|               Database Migration Complete                    |'); | 
            ||
| 131 |         $this->line('|                                                              |'); | 
            ||
| 132 |         $this->line('---------------------------------------------------------------'); | 
            ||
| 133 | |||
| 134 | return 0;  | 
            ||
| 135 | }  | 
            ||
| 136 | |||
| 137 | /**  | 
            ||
| 138 | * Determine if the database needs to be migrated  | 
            ||
| 139 | */  | 
            ||
| 140 | protected function isMigrationNecessary()  | 
            ||
| 141 |     { | 
            ||
| 142 |         if(!Schema::hasTable('app_settings')) | 
            ||
| 143 |         { | 
            ||
| 144 | return true;  | 
            ||
| 145 | }  | 
            ||
| 146 | |||
| 147 | return false;  | 
            ||
| 148 | }  | 
            ||
| 149 | |||
| 150 | /**  | 
            ||
| 151 | * Move all existing information over to the temporary database  | 
            ||
| 152 | */  | 
            ||
| 153 | protected function moveOldData()  | 
            ||
| 154 |     { | 
            ||
| 155 | foreach($this->ver5Tables as $table)  | 
            ||
| 156 |         { | 
            ||
| 157 | try  | 
            ||
| 158 |             { | 
            ||
| 159 |                 $this->line('Moving '.$this->database.'.'.$table); | 
            ||
| 160 |                 DB::statement('RENAME TABLE `'.$this->database.'`.`'.$table.'` TO `'.$this->database.'_old`.`'.$table.'`'); | 
            ||
| 161 | }  | 
            ||
| 162 | catch(Exception $e)  | 
            ||
| 163 |             { | 
            ||
| 164 |                 $this->line('Table '.$this->database.'.'.$table.' does not exist'); | 
            ||
| 165 | }  | 
            ||
| 166 | }  | 
            ||
| 167 | |||
| 168 |         DB::statement('DROP VIEW IF EXISTS `customer_contacts_view`'); | 
            ||
| 169 |         DB::statement('DROP VIEW IF EXISTS `navbar_view`'); | 
            ||
| 170 | |||
| 171 | $this->newLine();  | 
            ||
| 172 | }  | 
            ||
| 173 | |||
| 174 | /**  | 
            ||
| 175 | * Migrate App Settings  | 
            ||
| 176 | */  | 
            ||
| 177 | protected function migrateAppSettings()  | 
            ||
| 178 |     { | 
            ||
| 179 |         $this->info('Migrating App Settings'); | 
            ||
| 180 | |||
| 181 |         $current = DB::select('SELECT * FROM `'.$this->database.'_old`.`settings`'); | 
            ||
| 182 | |||
| 183 | foreach($current as $cur)  | 
            ||
| 184 |         { | 
            ||
| 185 |             $this->line('Adding Key - '.$cur->key); | 
            ||
| 186 |             DB::table('app_settings')->insert([ | 
            ||
| 187 | 'key' => $cur->key,  | 
            ||
| 188 | 'value' => $cur->value,  | 
            ||
| 189 | 'created_at' => $cur->created_at,  | 
            ||
| 190 | 'updated_at' => NOW(),  | 
            ||
| 191 | ]);  | 
            ||
| 192 | }  | 
            ||
| 193 | |||
| 194 | $this->newLine();  | 
            ||
| 195 | }  | 
            ||
| 196 | |||
| 197 | /**  | 
            ||
| 198 | * Migrate User Roles  | 
            ||
| 199 | */  | 
            ||
| 200 | protected function migrateUserRoles()  | 
            ||
| 201 |     { | 
            ||
| 202 | // Determine if there are any roles other than the default roles  | 
            ||
| 203 |         $current = DB::select('SELECT * FROM `'.$this->database.'_old`.`user_role_types`'); | 
            ||
| 204 | if(count($current) == 4)  | 
            ||
| 205 |         { | 
            ||
| 206 | return false;  | 
            ||
| 207 | }  | 
            ||
| 208 | |||
| 209 |         $this->info('Migrating User Roles'); | 
            ||
| 210 |         $roles = DB::select('SELECT * FROM `'.$this->database.'_old`.`user_role_types` WHERE `role_id` > 4'); | 
            ||
| 211 | foreach($roles as $role)  | 
            ||
| 212 |         { | 
            ||
| 213 |             $this->line('Creating '.$role->name.' Role'); | 
            ||
| 214 | // Create the Role  | 
            ||
| 215 |             DB::table('user_roles')->insert([ | 
            ||
| 216 | 'role_id' => $role->role_id,  | 
            ||
| 217 | 'name' => $role->name,  | 
            ||
| 218 | 'description' => $role->description,  | 
            ||
| 219 | 'allow_edit' => 1,  | 
            ||
| 220 | 'created_at' => $role->created_at,  | 
            ||
| 221 | 'updated_at' => NOW()]  | 
            ||
| 222 | );  | 
            ||
| 223 | |||
| 224 | // Create the permissions  | 
            ||
| 225 |             $oldPermissions = DB::select('SELECT * FROM `'.$this->database.'_old`.`user_role_permissions` WHERE `role_id` ='.$role->role_id); | 
            ||
| 226 | $newPermissions = [  | 
            ||
| 227 | 1 => 0,  | 
            ||
| 228 | 2 => $this->getPermissionValue($oldPermissions, 1),  | 
            ||
| 229 | 3 => $this->getPermissionValue($oldPermissions, 2),  | 
            ||
| 230 | 4 => $this->getPermissionValue($oldPermissions, 3),  | 
            ||
| 231 | 5 => $this->getPermissionValue($oldPermissions, 11),  | 
            ||
| 232 | 6 => $this->getPermissionValue($oldPermissions, 5),  | 
            ||
| 233 | 7 => $this->getPermissionValue($oldPermissions, 4),  | 
            ||
| 234 | 8 => $this->getPermissionValue($oldPermissions, 4),  | 
            ||
| 235 | 9 => $this->getPermissionValue($oldPermissions, 6),  | 
            ||
| 236 | 10 => 0,  | 
            ||
| 237 | 11 => $this->getPermissionValue($oldPermissions, 4),  | 
            ||
| 238 | 12 => $this->getPermissionValue($oldPermissions, 4),  | 
            ||
| 239 | 13 => $this->getPermissionValue($oldPermissions, 4),  | 
            ||
| 240 | 14 => $this->getPermissionValue($oldPermissions, 4),  | 
            ||
| 241 | 15 => $this->getPermissionValue($oldPermissions, 4),  | 
            ||
| 242 | 16 => $this->getPermissionValue($oldPermissions, 4),  | 
            ||
| 243 | 17 => $this->getPermissionValue($oldPermissions, 4),  | 
            ||
| 244 | 18 => $this->getPermissionValue($oldPermissions, 4),  | 
            ||
| 245 | 19 => $this->getPermissionValue($oldPermissions, 4),  | 
            ||
| 246 | 20 => $this->getPermissionValue($oldPermissions, 4),  | 
            ||
| 247 | 21 => $this->getPermissionValue($oldPermissions, 4),  | 
            ||
| 248 | 22 => $this->getPermissionValue($oldPermissions, 4),  | 
            ||
| 249 | 23 => $this->getPermissionValue($oldPermissions, 8),  | 
            ||
| 250 | 24 => $this->getPermissionValue($oldPermissions, 9),  | 
            ||
| 251 | 25 => $this->getPermissionValue($oldPermissions, 10),  | 
            ||
| 252 | 26 => $this->getPermissionValue($oldPermissions, 9),  | 
            ||
| 253 | 27 => 1,  | 
            ||
| 254 | ];  | 
            ||
| 255 | |||
| 256 | foreach($newPermissions as $key => $value)  | 
            ||
| 257 |             { | 
            ||
| 258 |                 DB::table('user_role_permissions')->insert([ | 
            ||
| 259 | 'role_id' => $role->role_id,  | 
            ||
| 260 | 'perm_type_id' => $key,  | 
            ||
| 261 | 'allow' => $value,  | 
            ||
| 262 | 'created_at' => $oldPermissions[0]->created_at,  | 
            ||
| 263 | 'updated_at' => NOW(),  | 
            ||
| 264 | ]);  | 
            ||
| 265 | }  | 
            ||
| 266 | }  | 
            ||
| 267 | |||
| 268 | $this->newLine();  | 
            ||
| 269 | }  | 
            ||
| 270 | |||
| 271 | /**  | 
            ||
| 272 | * Get the allow or deny of a User Role Permission Type  | 
            ||
| 273 | */  | 
            ||
| 274 | protected function getPermissionValue($permArray, $id)  | 
            ||
| 275 |     { | 
            ||
| 276 | $obj = Arr::first($permArray, function($value, $key) use ($id)  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 277 |                 { | 
            ||
| 278 | return $value->perm_type_id == $id;  | 
            ||
| 279 | });  | 
            ||
| 280 | |||
| 281 | return $obj->allow;  | 
            ||
| 282 | }  | 
            ||
| 283 | |||
| 284 | /**  | 
            ||
| 285 | * Migrate all users from old database  | 
            ||
| 286 | */  | 
            ||
| 287 | protected function migrateUsers()  | 
            ||
| 332 | }  | 
            ||
| 333 | |||
| 334 | /**  | 
            ||
| 335 | * Migrate all equipment types  | 
            ||
| 336 | */  | 
            ||
| 337 | protected function migrateEquipment()  | 
            ||
| 393 | }  | 
            ||
| 394 | |||
| 395 | /**  | 
            ||
| 396 | * Migrate all files into the new database  | 
            ||
| 397 | */  | 
            ||
| 398 | protected function migrateFiles()  | 
            ||
| 417 | }  | 
            ||
| 418 | |||
| 419 | /**  | 
            ||
| 420 | * Migrate all customer information  | 
            ||
| 421 | */  | 
            ||
| 422 | protected function migrateCustomers()  | 
            ||
| 545 | }  | 
            ||
| 546 | |||
| 547 | /**  | 
            ||
| 548 | * Migrate all Tech Tip data  | 
            ||
| 549 | */  | 
            ||
| 550 | protected function migrateTechTips()  | 
            ||
| 551 |     { | 
            ||
| 552 |         $this->info('Migrating Tech Tips'); | 
            ||
| 553 | |||
| 554 | // Add the Tech Tip Types  | 
            ||
| 555 |         DB::statement('DELETE FROM `tech_tip_types` WHERE `description` IS NOT NULL'); | 
            ||
| 556 |         $oldTypes = DB::select('SELECT * FROM `'.$this->database.'_old`.`tech_tip_types`'); | 
            ||
| 557 | foreach($oldTypes as $type)  | 
            ||
| 558 |         { | 
            ||
| 559 |             DB::table('tech_tip_types')->insert((array) $type); | 
            ||
| 560 | }  | 
            ||
| 561 | |||
| 562 |         $oldTips = DB::select('SELECT * FROM `'.$this->database.'_old`.`tech_tips`'); | 
            ||
| 563 | foreach($oldTips as $tip)  | 
            ||
| 564 |         { | 
            ||
| 565 |             $this->line('Adding Tech Tip '.$tip->subject); | 
            ||
| 566 |             DB::table('tech_tips')->insert([ | 
            ||
| 567 | 'tip_id' => $tip->tip_id,  | 
            ||
| 568 | 'user_id' => $tip->user_id,  | 
            ||
| 569 | 'updated_id' => isset($tip->updated_id) ? $tip->updated_id : null,  | 
            ||
| 570 | 'tip_type_id' => $tip->tip_type_id,  | 
            ||
| 571 | 'sticky' => isset($tip->sticky) ? $tip->sticky : false,  | 
            ||
| 572 | 'subject' => $tip->subject,  | 
            ||
| 573 | 'slug' => Str::slug($tip->subject),  | 
            ||
| 574 |                 'details'     => str_replace('<img ', '<img class="img-fluid" ', $tip->description), | 
            ||
| 575 | 'deleted_at' => $tip->deleted_at,  | 
            ||
| 576 | 'created_at' => $tip->created_at,  | 
            ||
| 577 | 'updated_at' => $tip->updated_at,  | 
            ||
| 578 | ]);  | 
            ||
| 579 | }  | 
            ||
| 580 | |||
| 581 | // Add Tech Tip Equipment  | 
            ||
| 582 | $this->newLine();  | 
            ||
| 583 |         $this->line('Adding Tech Tip Equipment'); | 
            ||
| 584 |         $oldEquipment = DB::select('SELECT * FROM `'.$this->database.'_old`.`tech_tip_systems`'); | 
            ||
| 585 | foreach($oldEquipment as $equip)  | 
            ||
| 586 |         { | 
            ||
| 587 |             DB::table('tech_tip_equipment')->insert([ | 
            ||
| 588 | 'tip_equip_id' => $equip->tip_tag_id,  | 
            ||
| 589 | 'tip_id' => $equip->tip_id,  | 
            ||
| 590 | 'equip_id' => $equip->sys_id,  | 
            ||
| 591 | ]);  | 
            ||
| 592 | }  | 
            ||
| 593 | |||
| 594 | // Add Tech Tip Files  | 
            ||
| 595 |         $this->line('Adding Tech Tip Files'); | 
            ||
| 596 |         $oldFiles = DB::select('SELECT * FROM `'.$this->database.'_old`.`tech_tip_files`'); | 
            ||
| 597 | foreach($oldFiles as $file)  | 
            ||
| 598 |         { | 
            ||
| 599 |             DB::table('tech_tip_files')->insert((array) $file); | 
            ||
| 600 | }  | 
            ||
| 601 | |||
| 602 | // Add Tech Tip Comments  | 
            ||
| 603 |         $this->line('Adding Tech Tip Comments'); | 
            ||
| 604 |         $oldComments = DB::select('SELECT * FROM `'.$this->database.'_old`.`tech_tip_comments`'); | 
            ||
| 605 | foreach($oldComments as $com)  | 
            ||
| 606 |         { | 
            ||
| 607 |             DB::table('tech_tip_comments')->insert([ | 
            ||
| 608 | 'id' => $com->comment_id,  | 
            ||
| 609 | 'tip_id' => $com->tip_id,  | 
            ||
| 610 | 'user_id' => $com->user_id,  | 
            ||
| 611 | 'comment' => $com->comment,  | 
            ||
| 612 | 'created_at' => $com->created_at,  | 
            ||
| 613 | 'updated_at' => $com->updated_at,  | 
            ||
| 614 | ]);  | 
            ||
| 615 | }  | 
            ||
| 616 | |||
| 617 | // Add Tech Tip Bookmarks  | 
            ||
| 618 |         $this->line('Adding Tech Tip Bookmarks'); | 
            ||
| 619 |         $oldBookmarks = DB::select('SELECT * FROM `'.$this->database.'_old`.`tech_tip_favs`'); | 
            ||
| 620 | foreach($oldBookmarks as $b)  | 
            ||
| 621 |         { | 
            ||
| 622 |             DB::table('user_tech_tip_bookmarks')->insert((array) $b); | 
            ||
| 623 | }  | 
            ||
| 624 | |||
| 625 | $this->newLine();  | 
            ||
| 626 | }  | 
            ||
| 627 | |||
| 628 | /**  | 
            ||
| 629 | * Cleanup files and move them into the proper folders  | 
            ||
| 630 | */  | 
            ||
| 631 | protected function fileCleanup()  | 
            ||
| 632 |     { | 
            ||
| 633 |         $this->info('Cleaning up Filesystem'); | 
            ||
| 634 | |||
| 635 | $customerFiles = CustomerFile::all();  | 
            ||
| 636 | foreach($customerFiles as $file)  | 
            ||
| 637 |         { | 
            ||
| 638 | if(!$this->moveStoredFile($file->file_id, $file->cust_id, 'customers'))  | 
            ||
| 639 |             { | 
            ||
| 640 |                 $this->error('FILE MISSING ON DISK '.$file->disk.' - '.$file->FileUpload->folder.DIRECTORY_SEPARATOR.$file->FileUpload->file_name); | 
            ||
| 641 | }  | 
            ||
| 642 | }  | 
            ||
| 643 | |||
| 644 | $techTipFiles = TechTipFile::all();  | 
            ||
| 645 | foreach($techTipFiles as $file)  | 
            ||
| 646 |         { | 
            ||
| 647 | if(!$this->moveStoredFile($file->file_id, $file->cust_id, 'tips'))  | 
            ||
| 648 |             { | 
            ||
| 649 |                 $this->error('FILE MISSING ON DISK '.$file->disk.' - '.$file->FileUpload->folder.DIRECTORY_SEPARATOR.$file->FileUpload->file_name); | 
            ||
| 650 | }  | 
            ||
| 651 | }  | 
            ||
| 652 | |||
| 653 | $this->newLine();  | 
            ||
| 654 | }  | 
            ||
| 655 | |||
| 656 | /**  | 
            ||
| 657 | * Check if the File Link Add-on is installed,  | 
            ||
| 658 | * If it is, migrate those tables  | 
            ||
| 659 | */  | 
            ||
| 660 | protected function checkFileLinkModule()  | 
            ||
| 699 | }  | 
            ||
| 700 | |||
| 701 | /**  | 
            ||
| 702 | * Cleanup  | 
            ||
| 703 | */  | 
            ||
| 704 | protected function cleanup()  | 
            ||
| 709 | }  | 
            ||
| 710 | }  | 
            ||
| 711 | 
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.