UpdateCommand.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Console\Commands;
  3. use Schema;
  4. use App\{Media, Status};
  5. use Illuminate\Console\Command;
  6. use App\Jobs\ImageOptimizePipeline\ImageThumbnail;
  7. class UpdateCommand extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'update';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Run pixelfed schema updates between versions.';
  21. protected $versions = [
  22. '0.1.0',
  23. '0.1.2',
  24. '0.1.3',
  25. '0.1.4',
  26. '0.1.5',
  27. '0.1.6',
  28. '0.1.7',
  29. '0.1.8',
  30. '0.1.9',
  31. ];
  32. protected $version;
  33. /**
  34. * Create a new command instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct()
  39. {
  40. parent::__construct();
  41. }
  42. /**
  43. * Execute the console command.
  44. *
  45. * @return mixed
  46. */
  47. public function handle()
  48. {
  49. $this->verifyVersion();
  50. }
  51. public function verifyVersion()
  52. {
  53. $this->callSilent('config:cache');
  54. $this->version = config('pixelfed.version');
  55. $known = in_array($this->version, $this->versions);
  56. if($known == false) {
  57. $this->error('Unknown version found, aborting...');
  58. exit;
  59. }
  60. $this->updateStrategy($this->version);
  61. }
  62. public function updateStrategy($version)
  63. {
  64. switch ($version) {
  65. case '0.1.8':
  66. $this->info('You are running an older version that doesn\'t require any updates!');
  67. break;
  68. case '0.1.9':
  69. $this->update019();
  70. break;
  71. default:
  72. # code...
  73. break;
  74. }
  75. }
  76. public function update019()
  77. {
  78. $this->buildVersionFile();
  79. $v = $this->getVersionFile();
  80. if($v['updated'] == true) {
  81. $this->info('Already up to date!');
  82. exit;
  83. }
  84. $exists = Schema::hasColumn('statuses','scope');
  85. if(!$exists) {
  86. $this->error('You need to run the migrations before you proceed with this update.');
  87. if($this->confirm('Do you want to run the migrations?')) {
  88. $this->callSilent('migrate');
  89. } else {
  90. exit;
  91. }
  92. }
  93. $statusCount = Status::count();
  94. $this->info('Running updates ...');
  95. $bar = $this->output->createProgressBar($statusCount);
  96. Status::chunk(200, function($statuses) use ($bar) {
  97. foreach($statuses as $status) {
  98. $ts = $status->updated_at;
  99. $status->scope = $status->visibility;
  100. $status->updated_at = $ts;
  101. $status->save();
  102. if($status->firstMedia()) {
  103. $media = $status->firstMedia();
  104. if(in_array($media->mime, ['image/jpeg', 'image/png'])) {
  105. ImageThumbnail::dispatch($media);
  106. }
  107. }
  108. $bar->advance();
  109. }
  110. });
  111. $this->updateVersionFile('0.1.9', true);
  112. $bar->finish();
  113. }
  114. protected function buildVersionFile()
  115. {
  116. $path = storage_path('app/version.json');
  117. if(is_file($path) == false) {
  118. $contents = json_encode([
  119. 'version' => $this->version,
  120. 'updated' => false,
  121. 'timestamp' => date('c')
  122. ], JSON_PRETTY_PRINT);
  123. file_put_contents($path, $contents);
  124. }
  125. }
  126. protected function getVersionFile()
  127. {
  128. $path = storage_path('app/version.json');
  129. if(is_file($path) == false) {
  130. $contents = [
  131. 'version' => $this->version,
  132. 'updated' => false,
  133. 'timestamp' => date('c')
  134. ];
  135. $json = json_encode($contents, JSON_PRETTY_PRINT);
  136. file_put_contents($path, $json);
  137. return $contents;
  138. } else {
  139. return json_decode(file_get_contents($path), true);
  140. }
  141. }
  142. protected function updateVersionFile($version, $updated = false) {
  143. $path = storage_path('app/version.json');
  144. if(is_file($path) == false) {
  145. return;
  146. }
  147. $contents = [
  148. 'version' => $version,
  149. 'updated' => $updated,
  150. 'timestamp' => date('c')
  151. ];
  152. $json = json_encode($contents, JSON_PRETTY_PRINT);
  153. file_put_contents($path, $json);
  154. }
  155. }