Completed
Push — master ( 52970f...c6d773 )
by Tristan
24:57 queued 10:40
created

ChangeJobPosterRemoteWorkColumnToBool::down()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 32
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 32
rs 8.8333
cc 7
nc 2
nop 0
1
<?php
2
3
use Illuminate\Support\Facades\Schema;
4
use Illuminate\Database\Schema\Blueprint;
5
use Illuminate\Database\Migrations\Migration;
6
7
use App\Models\JobPoster;
8
9
class ChangeJobPosterRemoteWorkColumnToBool extends Migration
10
{
11
    /**
12
     * Run the migrations.
13
     *
14
     * @return void
15
     */
16
    public function up()
17
    {
18
        // Since the ORM doesn't support a direct text to boolean migration we
19
        // have to get a little creative.
20
21
        // Add a new temp boolean column to the table
22
        Schema::table('job_posters', function (Blueprint $table) {
23
            $table->boolean('remote_work_allowed_new')->default(false);
24
        });
25
26
        // Loop through the existing entries and populate the new column
27
        $jobPosters = JobPoster::all();
28
        if ($jobPosters) {
0 ignored issues
show
introduced by
$jobPosters is of type Illuminate\Database\Eloquent\Collection, thus it always evaluated to true.
Loading history...
29
            foreach ($jobPosters as $jobPoster) {
30
                if ($jobPoster->remote_work_allowed === '1' || $jobPoster->remote_work_allowed === true) {
31
                    $jobPoster->remote_work_allowed_new = true;
0 ignored issues
show
Bug introduced by
The property remote_work_allowed_new does not exist on App\Models\JobPoster. Did you mean remote_work_allowed_new?
Loading history...
32
                } elseif ($jobPoster->remote_work_allowed === '0' || $jobPoster->remote_work_allowed === false) {
33
                    $jobPoster->remote_work_allowed_new = false;
34
                }
35
36
                $jobPoster->save();
37
            }
38
        }
39
40
        // Drop the old column
41
        Schema::table('job_posters', function (Blueprint $table) {
42
            $table->dropColumn('remote_work_allowed');
43
        });
44
45
        // Rename the new one
46
        Schema::table('job_posters', function (Blueprint $table) {
47
            $table->renameColumn('remote_work_allowed_new', 'remote_work_allowed');
48
        });
49
    }
50
51
    /**
52
     * Reverse the migrations.
53
     *
54
     * @return void
55
     */
56
    public function down()
57
    {
58
        // Since the ORM doesn't support a direct boolean to text migration we
59
        // have to get a little creative.
60
61
        // Add a new temp string column to the table
62
        Schema::table('job_posters', function (Blueprint $table) {
63
            $table->string('remote_work_allowed_new')->default('0');
64
        });
65
66
        // Loop through the existing entries and populate the new column
67
        $jobPosters = JobPoster::all();
68
        if ($jobPosters) {
0 ignored issues
show
introduced by
$jobPosters is of type Illuminate\Database\Eloquent\Collection, thus it always evaluated to true.
Loading history...
69
            foreach ($jobPosters as $jobPoster) {
70
                if ($jobPoster->remote_work_allowed === '1' || $jobPoster->remote_work_allowed === true) {
71
                    $jobPoster->remote_work_allowed_new = '1';
0 ignored issues
show
Bug introduced by
The property remote_work_allowed_new does not exist on App\Models\JobPoster. Did you mean remote_work_allowed_new?
Loading history...
72
                } elseif ($jobPoster->remote_work_allowed === '0' || $jobPoster->remote_work_allowed === false) {
73
                    $jobPoster->remote_work_allowed_new = '0';
74
                }
75
76
                $jobPoster->save();
77
            }
78
        }
79
80
        // Drop the old column
81
        Schema::table('job_posters', function (Blueprint $table) {
82
            $table->dropColumn('remote_work_allowed');
83
        });
84
85
        // Rename the new one
86
        Schema::table('job_posters', function (Blueprint $table) {
87
            $table->renameColumn('remote_work_allowed_new', 'remote_work_allowed');
88
        });
89
    }
90
}
91