Ei kuvausta

ProgramUserTableSeeder.php 4.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. class ProgramUserTableSeeder extends Seeder {
  3. public function run()
  4. {
  5. // Disable query logging to avoid memory exhaustion
  6. // SELECT DISTINCT `program_id` , `user_id` FROM `courses`
  7. DB::disableQueryLog();
  8. $this->command->info('Program_Users table seeding started...');
  9. // Initiates time
  10. $time_start = microtime(true);
  11. // Open file
  12. if($file = fopen('app/database/csv/programs_users.csv', 'r'))
  13. {
  14. // Initialize count variables
  15. $read = 0;
  16. $added = 0;
  17. $deleted = 0;
  18. $error = 0;
  19. // Read each row
  20. while($data = fgetcsv($file, 5000, ","))
  21. {
  22. // Add read count
  23. $read++;
  24. try
  25. {
  26. // Get row info
  27. $program_id = trim($data[0]);
  28. $user_id = trim($data[1]);
  29. // Missing data trow exception
  30. if(!$program_id || !$user_id){
  31. throw new Exception('NON-FATAL ERROR(line '.$read.'): Missing information, read \''.implode(",", $data).'\'');
  32. }
  33. // If user has default 999 program, delete and insert rows
  34. if(DB::table('program_user')->where('program_id', '=', 999)->where('user_id', '=', $user_id)->get()){
  35. $this->command->info('Would had deleted default program if not needed verify down for new program insert or inserts: user_id = '.$user_id);
  36. // Delete from table program_user default program
  37. DB::table('program_user')->where('program_id', '=', 999)->where('user_id', '=', $user_id)->delete();
  38. $deleted++;
  39. // Insert new program_user row if it doesnt exist
  40. if(!(DB::table('program_user')->where('program_id', '=', $program_id)->where('user_id', '=', $user_id)->get())){
  41. DB::table('program_user')->insert(array('program_id' => $program_id, 'user_id' => $user_id));
  42. $this->command->info('Insert after deleting default program for user_id = '.$user_id);
  43. $added++;
  44. }
  45. }else{
  46. // Insert new program_user row if it doesnt exist
  47. if(!(DB::table('program_user')->where('program_id', '=', $program_id)->where('user_id', '=', $user_id)->get())){
  48. DB::table('program_user')->insert(array('program_id' => $program_id, 'user_id' => $user_id));
  49. $this->command->info('Insert new line, no default program for user_id = '.$user_id);
  50. $added++;
  51. }
  52. }
  53. }
  54. // If an exception is raised, show the message and add to error
  55. catch(Exception $e)
  56. {
  57. $this->command->info($e->getMessage());
  58. $error++;
  59. };
  60. }
  61. // Stop time
  62. $time_end = microtime(true);
  63. // Display results
  64. $this->command->info('------------------------------------------------------------');
  65. $this->command->info('Results on '.date('M d, Y, h:i:s a'));
  66. $this->command->info('- Runtime: '.(round($time_end - $time_start, 3)).' seconds');
  67. $this->command->info('- Read: '.$read);
  68. $this->command->info('- Deleted default programs from users (999): '.$deleted);
  69. $this->command->info('- Added (new active): '.$added);
  70. $this->command->info('- Not added/updated (errors or missing information): '.($error));
  71. // Close file
  72. fclose($file);
  73. }
  74. // File cannot be opened, display error and exit
  75. else
  76. {
  77. $this->command->info('File '.$filename.' could not be opened. Make sure it is located in the app/database/csv directory of this project.');
  78. }
  79. }
  80. }