blessing-skin-server/app/Console/Commands/MigratePlayersTable.php

85 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Console\Commands;
2019-03-13 11:24:04 +08:00
use Schema;
use App\Models\Player;
use Illuminate\Console\Command;
2019-03-13 11:24:04 +08:00
use Illuminate\Database\Schema\Blueprint;
class MigratePlayersTable extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bs:migrate-v4:players-table';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Migrate the players table for v4';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
2019-03-13 11:25:20 +08:00
if (! Schema::hasColumn('players', 'tid_steve')) {
2019-03-13 11:24:04 +08:00
$this->info('No need to update.');
2019-03-13 11:25:20 +08:00
2019-03-13 11:24:04 +08:00
return;
}
$players = Player::where('tid_skin', -1)->get();
$count = $players->count();
if ($count == 0) {
2019-03-13 11:24:04 +08:00
$this->dropColumn();
$this->info('No need to update.');
2019-03-13 11:25:20 +08:00
return;
}
$this->info('We are going to update your `players` table. Please wait...');
$bar = $this->output->createProgressBar($count);
$players->each(function ($player) use ($bar) {
$player->tid_skin = $player->preference == 'default'
? $player->tid_steve
: $player->tid_alex;
$player->save();
$bar->advance();
});
2019-03-13 11:24:04 +08:00
$this->dropColumn();
$bar->finish();
$this->info("\nCongratulations! We've updated $count rows.");
}
2019-03-13 11:24:04 +08:00
private function dropColumn()
{
Schema::table('players', function (Blueprint $table) {
$table->dropColumn(['tid_steve', 'tid_alex', 'preference']);
});
}
}