/**
  * Execute the console command.
  *
  * @return mixed
  *
  * @dispatches UpdateGitMirror
  */
 public function handle()
 {
     $last_mirrored_since = Carbon::now()->subMinutes(self::UPDATE_FREQUENCY_MINUTES);
     $todo = self::UPDATES_TO_QUEUE;
     Project::where('last_mirrored', '<', $last_mirrored_since)->chunk($todo, function ($projects) {
         foreach ($projects as $project) {
             $this->dispatch(new UpdateGitMirror($project));
         }
     });
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $current_mirrors = [];
     Project::where('is_template', false)->chunk(10, function ($projects) use(&$current_mirrors) {
         foreach ($projects as $project) {
             $current_mirrors[] = $project->mirrorPath();
         }
     });
     $current_mirrors = collect($current_mirrors);
     $all_mirrors = collect(glob(storage_path('app/mirrors/') . '*.git'));
     // Compare the 2 collections get a list of mirrors which are no longer in use
     $orphan_mirrors = $all_mirrors->diff($current_mirrors);
     $this->info('Found ' . $orphan_mirrors->count() . ' orphaned mirrors');
     // Now loop through the mirrors and delete them from storage
     foreach ($orphan_mirrors as $mirror_dir) {
         $process = new Process('tools.RemoveMirrorDirectory', ['mirror_path' => $mirror_dir]);
         $process->run();
         if ($process->isSuccessful()) {
             $this->info('Deleted ' . basename($mirror_dir));
         } else {
             $this->info('Failed to delete ' . basename($mirror_dir));
         }
     }
 }
 /**
  * Run the migrations.
  *
  * @return void
  */
 public function up()
 {
     foreach ($this->relations as $relation) {
         $className = "REBELinBLUE\\Deployer\\{$relation}";
         $instance = new $className();
         $table = $instance->getTable();
         // Add the target fields to the tables
         Schema::table($table, function (Blueprint $table) {
             $table->integer('target_id')->nullable();
             $table->string('target_type')->nullable();
         });
         if (config('database.default') !== 'sqlite') {
             $drop = config('database.default') === 'mysql' ? 'FOREIGN KEY' : 'CONSTRAINT';
             DB::statement("ALTER TABLE {$table} DROP {$drop} {$table}_project_id_foreign");
         }
     }
     // Now find the existing templates in the project template and move them to templates table
     $templates = [];
     foreach (Project::where('is_template', true)->get() as $project) {
         $data = $project->toArray();
         unset($data['id']);
         $templates[$project->id] = Template::create($data);
     }
     // Now loop through the relations and set the target details
     foreach ($this->relations as $relation) {
         $className = "REBELinBLUE\\Deployer\\{$relation}";
         $instance = new $className();
         foreach ($instance->all() as $row) {
             $row->target_id = $row->project_id;
             $row->target_type = 'project';
             if (isset($templates[$row->project_id])) {
                 $row->target_id = $templates[$row->project_id]->id;
                 $row->target_type = 'template';
             }
             $row->save();
         }
     }
     // Remove any deleted non templates from group 1 to group 2
     $project = new Project();
     $project->where('is_template', false)->where('group_id', 1)->withTrashed()->update(['group_id' => 2]);
     // Remove the left over fake templates and the containing group
     Project::where('is_template', true)->forceDelete();
     Group::find(1)->forceDelete();
     // Remove the unneeded project ID column
     foreach ($this->relations as $relation) {
         $className = "REBELinBLUE\\Deployer\\{$relation}";
         $instance = new $className();
         $table = $instance->getTable();
         // You can't drop a column in SQLite
         if (config('database.default') !== 'sqlite') {
             DB::statement("ALTER TABLE {$table} DROP COLUMN project_id");
         }
         if (config('database.default') === 'mysql') {
             DB::statement("ALTER TABLE {$table} MODIFY target_id INT(11) NOT NULL");
             DB::statement("ALTER TABLE {$table} MODIFY target_type VARCHAR(255) NOT NULL");
         } elseif (config('database.default') === 'pgsql') {
             DB::statement("ALTER TABLE {$table} ALTER COLUMN target_id SET NOT NULL");
             DB::statement("ALTER TABLE {$table} ALTER COLUMN target_type SET NOT NULL");
         }
     }
 }