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