Completed
Push — master ( 3e9fd3...553853 )
by Abdelrahman
01:29 queued 13s
created

CreateStatisticsGeoipsTable::up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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