1
0

UpdateCommand.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Console\Commands;
  3. use Schema;
  4. use Illuminate\Console\Command;
  5. use App\Jobs\ImageOptimizePipeline\ImageThumbnail;
  6. class UpdateCommand extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'update';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Run pixelfed schema updates between versions.';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $this->update();
  37. }
  38. public function update()
  39. {
  40. $v = $this->getVersionFile();
  41. if($v && isset($v['commit_hash']) && $v['commit_hash'] == exec('git rev-parse HEAD') && \App\StatusHashtag::whereNull('profile_id')->count() == 0) {
  42. $this->info('No updates found.');
  43. return;
  44. }
  45. $bar = $this->output->createProgressBar(\App\StatusHashtag::whereNull('profile_id')->count());
  46. \App\StatusHashtag::whereNull('profile_id')->with('status')->chunk(50, function($sh) use ($bar) {
  47. foreach($sh as $status_hashtag) {
  48. if(!$status_hashtag->status) {
  49. $status_hashtag->delete();
  50. } else {
  51. $status_hashtag->profile_id = $status_hashtag->status->profile_id;
  52. $status_hashtag->save();
  53. }
  54. $bar->advance();
  55. }
  56. });
  57. $this->updateVersionFile();
  58. $bar->finish();
  59. }
  60. protected function getVersionFile()
  61. {
  62. $path = storage_path('app/version.json');
  63. return is_file($path) ?
  64. json_decode(file_get_contents($path), true) :
  65. false;
  66. }
  67. protected function updateVersionFile() {
  68. $path = storage_path('app/version.json');
  69. $contents = [
  70. 'commit_hash' => exec('git rev-parse HEAD'),
  71. 'version' => config('pixelfed.version'),
  72. 'timestamp' => date('c')
  73. ];
  74. $json = json_encode($contents, JSON_PRETTY_PRINT);
  75. file_put_contents($path, $json);
  76. }
  77. }