<?php

use App\User;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateDeveloperUserRole extends Migration {
    public function up() {
        // Shift moderator and higher role IDs up.
        User::where('role', '>=', 98)->update(['role' => DB::raw('role + 1')]);

        Schema::table('users', function (Blueprint $table) {
            $table
                ->boolean('is_developer')
                ->default(false)
                ->nullable(false);
        });
    }

    public function down() {
        // Remove role from developers to avoid implicitly promoting them to moderators.
        User::where('role', '=', 98)->update(['role' => 0]);

        // Shift moderator and higher role IDs down.
        User::where('role', '>=', 99)->update(['role' => DB::raw('role - 1')]);

        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('is_developer');
        });
    }
}
