From cd2711942e102ba4548c0198198c0eb2e2d18a28 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Wed, 13 Mar 2019 11:24:04 +0800 Subject: [PATCH] Enforce to use `tid_skin` --- app/Console/Commands/MigratePlayersTable.php | 19 ++++++++- app/Models/Player.php | 12 ------ .../2016_11_18_133939_create_all_tables.php | 3 -- ...130617_add_verification_to_users_table.php | 10 +---- .../2019_03_01_131420_add_tid_skin.php | 10 ++++- .../CommandsTest/MigratePlayersTableTest.php | 40 ------------------- tests/ModelsTest/PlayerTest.php | 19 --------- 7 files changed, 27 insertions(+), 86 deletions(-) delete mode 100644 tests/CommandsTest/MigratePlayersTableTest.php diff --git a/app/Console/Commands/MigratePlayersTable.php b/app/Console/Commands/MigratePlayersTable.php index b3bd7a22..a1957493 100644 --- a/app/Console/Commands/MigratePlayersTable.php +++ b/app/Console/Commands/MigratePlayersTable.php @@ -2,8 +2,10 @@ namespace App\Console\Commands; +use Schema; use App\Models\Player; use Illuminate\Console\Command; +use Illuminate\Database\Schema\Blueprint; class MigratePlayersTable extends Command { @@ -38,12 +40,17 @@ class MigratePlayersTable extends Command */ public function handle() { + if (!Schema::hasColumn('players', 'tid_steve')) { + $this->info('No need to update.'); + return; + } + $players = Player::where('tid_skin', -1)->get(); $count = $players->count(); if ($count == 0) { + $this->dropColumn(); $this->info('No need to update.'); - return; } @@ -58,8 +65,18 @@ class MigratePlayersTable extends Command $bar->advance(); }); + + $this->dropColumn(); + $bar->finish(); $this->info("\nCongratulations! We've updated $count rows."); } + + private function dropColumn() + { + Schema::table('players', function (Blueprint $table) { + $table->dropColumn(['tid_steve', 'tid_alex', 'preference']); + }); + } } diff --git a/app/Models/Player.php b/app/Models/Player.php index b0d81202..f475f3b4 100644 --- a/app/Models/Player.php +++ b/app/Models/Player.php @@ -58,18 +58,6 @@ class Player extends Model return $this->belongsTo('App\Models\User', 'uid'); } - public function getTidSkinAttribute($value) - { - if ($value == -1) { - $this->tid_skin = $value = $this->preference == 'default' - ? $this->tid_steve - : $this->tid_alex; - $this->save(); - } - - return $value; - } - /** * Get specific texture of player. * diff --git a/database/migrations/2016_11_18_133939_create_all_tables.php b/database/migrations/2016_11_18_133939_create_all_tables.php index 1d97af25..2b04f03e 100644 --- a/database/migrations/2016_11_18_133939_create_all_tables.php +++ b/database/migrations/2016_11_18_133939_create_all_tables.php @@ -34,9 +34,6 @@ class CreateAllTables extends Migration $table->increments('pid'); $table->integer('uid'); $table->string('player_name', 50); - $table->string('preference', 10); - $table->integer('tid_steve')->default('0'); - $table->integer('tid_alex')->default('0'); $table->integer('tid_cape')->default('0'); $table->dateTime('last_modified'); }); diff --git a/database/migrations/2018_07_26_130617_add_verification_to_users_table.php b/database/migrations/2018_07_26_130617_add_verification_to_users_table.php index 2e79e318..9eabf4f8 100644 --- a/database/migrations/2018_07_26_130617_add_verification_to_users_table.php +++ b/database/migrations/2018_07_26_130617_add_verification_to_users_table.php @@ -25,16 +25,8 @@ class AddVerificationToUsersTable extends Migration */ public function down() { - if (config('database.default') == 'sqlite') { - // Dropping columns from a SQLite database requires `doctrine/dbal` dependency. - // However, we won't install it because it's too hard to specify the version of - // all the new dependencies exactly to make them support PHP ^5.5.9. Damn it. - return; - } - Schema::table('users', function (Blueprint $table) { - $table->dropColumn('verified'); - $table->dropColumn('verification_token'); + $table->dropColumn(['verified', 'verification_token']); }); } } diff --git a/database/migrations/2019_03_01_131420_add_tid_skin.php b/database/migrations/2019_03_01_131420_add_tid_skin.php index 913a9c3f..d8b4b2d0 100644 --- a/database/migrations/2019_03_01_131420_add_tid_skin.php +++ b/database/migrations/2019_03_01_131420_add_tid_skin.php @@ -15,7 +15,10 @@ class AddTidSkin extends Migration { Schema::table('players', function (Blueprint $table) { $table->integer('tid_skin')->default(-1); - $table->string('preference', 10)->nullable()->change(); + + if (Schema::hasColumn('players', 'preference')) { + $table->string('preference', 10)->nullable()->change(); + } }); } @@ -28,7 +31,10 @@ class AddTidSkin extends Migration { Schema::table('players', function (Blueprint $table) { $table->dropColumn('tid_skin'); - $table->string('preference', 10)->nullable(false)->change(); + + if (Schema::hasColumn('players', 'preference')) { + $table->string('preference', 10)->nullable(false)->change(); + } }); } } diff --git a/tests/CommandsTest/MigratePlayersTableTest.php b/tests/CommandsTest/MigratePlayersTableTest.php deleted file mode 100644 index 5a66a2be..00000000 --- a/tests/CommandsTest/MigratePlayersTableTest.php +++ /dev/null @@ -1,40 +0,0 @@ -create([ - 'tid_skin' => -1, - 'preference' => $faker->randomElement(['default', 'slim']), - 'tid_steve' => $faker->randomDigit(), - 'tid_alex' => $faker->randomDigit(), - ]); - } - - Artisan::call('bs:migrate-v4:players-table'); - - Player::all() - ->each(function (Player $player) { - if ($player->preference == 'default') { - $this->assertEquals($player->tid_steve, $player->tid_skin); - } else { - $this->assertEquals($player->tid_alex, $player->tid_skin); - } - }); - - $this->assertEquals(0, Artisan::call('bs:migrate-v4:players-table')); - } -} diff --git a/tests/ModelsTest/PlayerTest.php b/tests/ModelsTest/PlayerTest.php index 35025d14..c28f538c 100644 --- a/tests/ModelsTest/PlayerTest.php +++ b/tests/ModelsTest/PlayerTest.php @@ -29,23 +29,4 @@ class PlayerTest extends TestCase $player = factory(Player::class)->make(); $this->assertNull($player->getJsonProfile(-1)); } - - public function testGetTidSkinAttribute() - { - $player = factory(Player::class)->create([ - 'tid_skin' => -1, - 'preference' => 'default', - 'tid_steve' => 5, - ]); - $this->assertEquals(5, $player->tid_skin); - $this->assertEquals(5, Player::find($player->pid)->tid_skin); - - $player = factory(Player::class)->create([ - 'tid_skin' => -1, - 'preference' => 'slim', - 'tid_alex' => 6, - ]); - $this->assertEquals(6, $player->tid_skin); - $this->assertEquals(6, Player::find($player->pid)->tid_skin); - } }