Completed
Push — master ( 19041e...6eacc5 )
by Abdelrahman
63:07 queued 59:28
created

CreateCategoriesTable::jsonable()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
use Kalnoy\Nestedset\NestedSet;
6
use Illuminate\Support\Facades\Schema;
7
use Illuminate\Database\Schema\Blueprint;
8
use Illuminate\Database\Migrations\Migration;
9
10
class CreateCategoriesTable extends Migration
11
{
12
    /**
13
     * Run the migrations.
14
     *
15
     * @return void
16
     */
17
    public function up(): void
18
    {
19
        Schema::create(config('rinvex.categories.tables.categories'), function (Blueprint $table) {
20
            // Columns
21
            $table->increments('id');
22
            $table->string('slug');
23
            $table->{$this->jsonable()}('name');
24
            $table->{$this->jsonable()}('description')->nullable();
25
            NestedSet::columns($table);
26
            $table->timestamps();
27
            $table->softDeletes();
28
29
            // Indexes
30
            $table->unique('slug');
31
        });
32
    }
33
34
    /**
35
     * Reverse the migrations.
36
     *
37
     * @return void
38
     */
39
    public function down(): void
40
    {
41
        Schema::dropIfExists(config('rinvex.categories.tables.categories'));
42
    }
43
44
    /**
45
     * Get jsonable column data type.
46
     *
47
     * @return string
48
     */
49
    protected function jsonable(): string
50
    {
51
        $driverName = DB::connection()->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME);
52
        $dbVersion = DB::connection()->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION);
53
        $isOldVersion = version_compare($dbVersion, '5.7.8', 'lt');
54
55
        return $driverName === 'mysql' && $isOldVersion ? 'text' : 'json';
56
    }
57
}
58