AddOptionalUserToVersions   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 8 1
A down() 0 13 1
1
<?php
2
/**
3
 * No Namespace
4
 *
5
 * This file is part of the overtrue/laravel-versionable.
6
 * ------------------------------------------------------
7
 * (c) overtrue <[email protected]>
8
 * This source file is subject to the MIT license that is bundled.
9
 */
10
11
use CalamandreiLorenzo\LaravelVersionable\Version;
12
use Illuminate\Database\Migrations\Migration;
13
use Illuminate\Database\Schema\Blueprint;
14
use Illuminate\Support\Facades\Schema;
15
16
/**
17
 * Class AddDeletedAtToVersions
18
 * @author Lorenzo Calamandrei
19
 * @github https://github.com/CalamandreiLorenzo
20
 */
21
class AddOptionalUserToVersions extends Migration
22
{
23
    /**
24
     * Run the migrations.
25
     *
26
     * @return void
27
     */
28
    public function up(): void
29
    {
30
        Schema::table('versions', static function (Blueprint $table) {
31
            $table->{config('versionable.user_key_type', 'unsignedBigInteger')}(
32
                config('versionable.user_foreign_key', 'user_id')
33
            )->nullable()->change();
34
        });
35
    }
36
37
    /**
38
     * Reverse the migrations.
39
     *
40
     * @return void
41
     * @throws Exception
42
     */
43
    public function down(): void
44
    {
45
        // Delete all versions that doesn't have a user, because is mandatory before this transaction
46
        Version::whereDoesntHave(
47
            config('versionable.user_foreign_key', 'user_id')
48
        )->delete();
49
50
        Schema::table('versions', static function (Blueprint $table) {
51
            $table->{config('versionable.user_key_type', 'unsignedBigInteger')}(
52
                config('versionable.user_foreign_key', 'user_id')
53
            )->change();
54
        });
55
    }
56
}
57