1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use App\Models\CustomerFile; |
7
|
|
|
use App\Models\TechTipFile; |
8
|
|
|
use Illuminate\Console\Command; |
9
|
|
|
|
10
|
|
|
use Illuminate\Support\Arr; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
use Illuminate\Support\Facades\DB; |
13
|
|
|
use Illuminate\Support\Facades\Artisan; |
14
|
|
|
use Illuminate\Support\Facades\Schema; |
15
|
|
|
|
16
|
|
|
use App\Traits\FileTrait; |
17
|
|
|
use Illuminate\Support\Facades\Storage; |
18
|
|
|
|
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() |
44
|
|
|
{ |
45
|
|
|
parent::__construct(); |
46
|
|
|
|
47
|
|
|
$this->database = config('database.connections.mysql.database'); |
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() |
288
|
|
|
{ |
289
|
|
|
$this->info('Migrating Users'); |
290
|
|
|
|
291
|
|
|
$userList = DB::select('SELECT * FROM `'.$this->database.'_old`.`users`'); |
292
|
|
|
foreach($userList as $user) |
293
|
|
|
{ |
294
|
|
|
if($user->user_id === 1) |
295
|
|
|
{ |
296
|
|
|
DB::table('users')->where('user_id', 1)->update((array) $user); |
297
|
|
|
} |
298
|
|
|
else |
299
|
|
|
{ |
300
|
|
|
$this->line('Adding User '.$user->first_name.' '.$user->last_name); |
301
|
|
|
DB::table('users')->insert((array) $user); |
302
|
|
|
|
303
|
|
|
// Migrate the users settings |
304
|
|
|
$settings = DB::select('SELECT * FROM `'.$this->database.'_old`.`user_settings` WHERE `user_id` = '.$user->user_id); |
305
|
|
|
$newSettings = [ |
306
|
|
|
1 => $settings[0]->em_notification, |
307
|
|
|
2 => $settings[0]->em_notification, |
308
|
|
|
]; |
309
|
|
|
|
310
|
|
|
foreach($newSettings as $key => $value) |
311
|
|
|
{ |
312
|
|
|
DB::table('user_settings')->insert([ |
313
|
|
|
'user_id' => $user->user_id, |
314
|
|
|
'setting_type_id' => $key, |
315
|
|
|
'value' => $value, |
316
|
|
|
'created_at' => $user->created_at, |
317
|
|
|
'updated_at' => NOW(), |
318
|
|
|
]); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
// Migrate the User Logins table |
324
|
|
|
$this->line('Updated User Login Table'); |
325
|
|
|
$data = DB::select('SELECT * FROM `'.$this->database.'_old`.`user_logins`'); |
326
|
|
|
foreach($data as $d) |
327
|
|
|
{ |
328
|
|
|
DB::table('user_logins')->insert((array) $d); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$this->newLine(); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Migrate all equipment types |
336
|
|
|
*/ |
337
|
|
|
protected function migrateEquipment() |
338
|
|
|
{ |
339
|
|
|
$this->info('Migrating Equipment'); |
340
|
|
|
|
341
|
|
|
// Migrate Equipment Categories |
342
|
|
|
$cat = DB::select('SELECT * FROM `'.$this->database.'_old`.`system_categories`'); |
343
|
|
|
foreach($cat as $c) |
344
|
|
|
{ |
345
|
|
|
$this->line('Adding Category - '.$c->name); |
346
|
|
|
DB::table('equipment_categories')->insert((array) $c); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
// Migrate Equipment Types |
350
|
|
|
$equip = DB::select('SELECT * FROM `'.$this->database.'_old`.`system_types`'); |
351
|
|
|
foreach($equip as $e) |
352
|
|
|
{ |
353
|
|
|
$this->line('Adding Equipment - '.$e->name); |
354
|
|
|
DB::table('equipment_types')->insert([ |
355
|
|
|
'equip_id' => $e->sys_id, |
356
|
|
|
'cat_id' => $e->cat_id, |
357
|
|
|
'name' => $e->name, |
358
|
|
|
'created_at' => $e->created_at, |
359
|
|
|
'updated_at' => $e->updated_at, |
360
|
|
|
]); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
// Migrate Data Field Types |
364
|
|
|
DB::statement('DELETE FROM `data_field_types` WHERE `name` IS NOT NULL'); |
365
|
|
|
$types = DB::select('SELECT * FROM `'.$this->database.'_old`.`system_data_field_types`'); |
366
|
|
|
foreach($types as $t) |
367
|
|
|
{ |
368
|
|
|
$this->line('Adding Equipment Data Type - '.$t->name); |
369
|
|
|
DB::table('data_field_types')->insert([ |
370
|
|
|
'type_id' => $t->data_type_id, |
371
|
|
|
'name' => $t->name, |
372
|
|
|
'hidden' => $t->hidden, |
373
|
|
|
'created_at' => $t->created_at, |
374
|
|
|
'updated_at' => $t->updated_at |
375
|
|
|
]); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
$this->line('Adding Data Types to Equipment'); |
379
|
|
|
$fields = DB::select('SELECT * FROM `'.$this->database.'_old`.`system_data_fields`'); |
380
|
|
|
foreach($fields as $f) |
381
|
|
|
{ |
382
|
|
|
DB::table('data_fields')->insert([ |
383
|
|
|
'field_id' => $f->field_id, |
384
|
|
|
'equip_id' => $f->sys_id, |
385
|
|
|
'type_id' => $f->data_type_id, |
386
|
|
|
'order' => $f->order, |
387
|
|
|
'created_at' => $f->created_at, |
388
|
|
|
'updated_at' => $f->updated_at, |
389
|
|
|
]); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
$this->newLine(); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Migrate all files into the new database |
397
|
|
|
*/ |
398
|
|
|
protected function migrateFiles() |
399
|
|
|
{ |
400
|
|
|
$this->info('Migrating Files'); |
401
|
|
|
$fileList = DB::select('SELECT * FROM `'.$this->database.'_old`.`files`'); |
402
|
|
|
foreach($fileList as $file) |
403
|
|
|
{ |
404
|
|
|
$this->line('Adding File '.$file->file_name); |
405
|
|
|
DB::table('file_uploads')->insert([ |
406
|
|
|
'file_id' => $file->file_id, |
407
|
|
|
'disk' => 'local', |
408
|
|
|
'folder' => $file->file_link, |
409
|
|
|
'file_name' => $file->file_name, |
410
|
|
|
'public' => 0, |
411
|
|
|
'created_at' => $file->created_at, |
412
|
|
|
'updated_at' => $file->updated_at, |
413
|
|
|
]); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
$this->newLine(); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Migrate all customer information |
421
|
|
|
*/ |
422
|
|
|
protected function migrateCustomers() |
423
|
|
|
{ |
424
|
|
|
$this->info('Migrating Customers'); |
425
|
|
|
|
426
|
|
|
// Migrate the primary customer information |
427
|
|
|
$customers = DB::select('SELECT * FROM `'.$this->database.'_old`.`customers`'); |
428
|
|
|
|
429
|
|
|
// Temporarily disable all foreign keys |
430
|
|
|
DB::statement('SET FOREIGN_KEY_CHECKS=0'); |
431
|
|
|
|
432
|
|
|
foreach($customers as $cust) |
433
|
|
|
{ |
434
|
|
|
$this->line('Adding Customer '.$cust->name); |
435
|
|
|
DB::table('customers')->insert([ |
436
|
|
|
'cust_id' => $cust->cust_id, |
437
|
|
|
'parent_id' => $cust->parent_id, |
438
|
|
|
'name' => $cust->name, |
439
|
|
|
'dba_name' => $cust->dba_name, |
440
|
|
|
'slug' => STR::slug($cust->name), |
441
|
|
|
'address' => $cust->address, |
442
|
|
|
'city' => $cust->city, |
443
|
|
|
'state' => $cust->state, |
444
|
|
|
'zip' => $cust->zip, |
445
|
|
|
'deleted_at' => $cust->deleted_at, |
446
|
|
|
'created_at' => $cust->created_at, |
447
|
|
|
'updated_at' => $cust->updated_at, |
448
|
|
|
]); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
// Re-enable all foreign keys |
452
|
|
|
DB::statement('SET FOREIGN_KEY_CHECKS=1'); |
453
|
|
|
$this->newLine(); |
454
|
|
|
|
455
|
|
|
// Migrate customer equipment |
456
|
|
|
$this->line('Adding Customer Equipment'); |
457
|
|
|
$oldEquip = DB::select('SELECT * FROM `'.$this->database.'_old`.`customer_systems`'); |
458
|
|
|
foreach($oldEquip as $equip) |
459
|
|
|
{ |
460
|
|
|
DB::table('customer_equipment')->insert([ |
461
|
|
|
'cust_equip_id' => $equip->cust_sys_id, |
462
|
|
|
'cust_id' => $equip->cust_id, |
463
|
|
|
'equip_id' => $equip->sys_id, |
464
|
|
|
'shared' => $equip->shared, |
465
|
|
|
'deleted_at' => $equip->deleted_at, |
466
|
|
|
'created_at' => $equip->created_at, |
467
|
|
|
'updated_at' => $equip->updated_at, |
468
|
|
|
]); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
// Migrate customer equipment data |
472
|
|
|
$this->line('Adding Customer Equipment Data'); |
473
|
|
|
$oldData = DB::select('SELECT * FROM `'.$this->database.'_old`.`customer_system_data`'); |
474
|
|
|
foreach($oldData as $data) |
475
|
|
|
{ |
476
|
|
|
DB::table('customer_equipment_data')->insert([ |
477
|
|
|
'cust_equip_id' => $data->cust_sys_id, |
478
|
|
|
'field_id' => $data->field_id, |
479
|
|
|
'value' => $data->value, |
480
|
|
|
'created_at' => $data->created_at, |
481
|
|
|
'updated_at' => $data->updated_at, |
482
|
|
|
]); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
// Migrate Customer Contacts |
486
|
|
|
$this->line('Adding Customer Contacts'); |
487
|
|
|
$oldCont = DB::select('SELECT * FROM `'.$this->database.'_old`.`customer_contacts`'); |
488
|
|
|
foreach($oldCont as $cont) |
489
|
|
|
{ |
490
|
|
|
DB::table('customer_contacts')->insert((array) $cont); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
// Migrate Customer Contact Phone Numbers |
494
|
|
|
$oldPhone = DB::select('SELECT * FROM `'.$this->database.'_old`.`customer_contact_phones`'); |
495
|
|
|
foreach($oldPhone as $phone) |
496
|
|
|
{ |
497
|
|
|
DB::table('customer_contact_phones')->insert((array) $phone); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
// Migrate Customer File Types |
501
|
|
|
$this->line('Adding Customer Files'); |
502
|
|
|
DB::statement('DELETE FROM `customer_file_types` WHERE `description` IS NOT NULL'); |
503
|
|
|
$fileTypes = DB::select('SELECT * FROM `'.$this->database.'_old`.`customer_file_types`'); |
504
|
|
|
foreach($fileTypes as $type) |
505
|
|
|
{ |
506
|
|
|
DB::table('customer_file_types')->insert((array) $type); |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
// Migrate Customer Files |
510
|
|
|
$oldFiles = DB::select('SELECT * FROM `'.$this->database.'_old`.`customer_files`'); |
511
|
|
|
foreach($oldFiles as $file) |
512
|
|
|
{ |
513
|
|
|
DB::table('customer_files')->insert((array) $file); |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
// Migrate Customer Notes |
517
|
|
|
$this->line('Adding Customer Notes'); |
518
|
|
|
$oldNotes = DB::select('SELECT * FROM `'.$this->database.'_old`.`customer_notes`'); |
519
|
|
|
foreach($oldNotes as $note) |
520
|
|
|
{ |
521
|
|
|
DB::table('customer_notes')->insert([ |
522
|
|
|
'note_id' => $note->note_id, |
523
|
|
|
'cust_id' => $note->cust_id, |
524
|
|
|
'created_by' => $note->user_id, |
525
|
|
|
'updated_by' => null, |
526
|
|
|
'urgent' => $note->urgent, |
527
|
|
|
'shared' => $note->shared, |
528
|
|
|
'subject' => $note->subject, |
529
|
|
|
'details' => $note->description, |
530
|
|
|
'deleted_at' => null, |
531
|
|
|
'created_at' => $note->created_at, |
532
|
|
|
'updated_at' => $note->updated_at, |
533
|
|
|
]); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
// Migrate Customer Bookmarks |
537
|
|
|
$this->line('Adding Customer Bookmarks'); |
538
|
|
|
$oldBookmarks = DB::select('SELECT * FROM `'.$this->database.'_old`.`customer_favs`'); |
539
|
|
|
foreach($oldBookmarks as $b) |
540
|
|
|
{ |
541
|
|
|
DB::table('user_customer_bookmarks')->insert((array) $b); |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
$this->newLine(); |
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() |
661
|
|
|
{ |
662
|
|
|
$this->info('Checking for File Link Module'); |
663
|
|
|
|
664
|
|
|
if(!Storage::disk('modules')->exists('FileLinkModule/README.md')) |
665
|
|
|
{ |
666
|
|
|
$this->line('File Link module not installed'); |
667
|
|
|
$this->line('Continuing...'); |
668
|
|
|
$this->newLine(); |
669
|
|
|
|
670
|
|
|
return false; |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
$this->line('Found File Link Module'); |
674
|
|
|
Artisan::call('tb_module:activate FileLinkModule -q'); |
675
|
|
|
|
676
|
|
|
$oldFileLinks = DB::select('SELECT * FROM `'.$this->database.'_old`.`file_links`'); |
677
|
|
|
foreach($oldFileLinks as $link) |
678
|
|
|
{ |
679
|
|
|
DB::table('file_links')->insert([ |
680
|
|
|
'link_id' => $link->link_id, |
681
|
|
|
'user_id' => $link->user_id, |
682
|
|
|
'link_hash' => $link->link_hash, |
683
|
|
|
'link_name' => $link->link_name, |
684
|
|
|
'expire' => $link->expire, |
685
|
|
|
'instructions' => $link->note, |
686
|
|
|
'allow_upload' => $link->allow_upload, |
687
|
|
|
'created_at' => $link->created_at, |
688
|
|
|
'updated_at' => $link->updated_at, |
689
|
|
|
]); |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
$oldFiles = DB::select('SELECT * FROM `'.$this->database.'_old`.`file_link_files`'); |
693
|
|
|
foreach($oldFiles as $file) |
694
|
|
|
{ |
695
|
|
|
DB::table('file_link_files')->insert((array) $file); |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
$this->newLine(); |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
/** |
702
|
|
|
* Cleanup |
703
|
|
|
*/ |
704
|
|
|
protected function cleanup() |
705
|
|
|
{ |
706
|
|
|
$this->info('Cleaning Up'); |
707
|
|
|
|
708
|
|
|
DB::statement('DROP SCHEMA `'.$this->database.'_old`'); |
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.