UpdateSchema0611To0620::down()   A
last analyzed

Complexity

Conditions 2
Paths 5

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
cc 2
eloc 17
c 5
b 0
f 1
nc 5
nop 0
dl 0
loc 25
rs 9.7
1
<?php
2
3
declare(strict_types=1);
4
5
use Illuminate\Database\Schema\Blueprint;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Support\Facades\Schema;
8
use PhpTelegramBot\Laravel\Migration;
9
10
class UpdateSchema0611To0620 extends Migration
11
{
12
    public function up(): void
13
    {
14
        try {
15
            Schema::table($this->prefix . 'poll', static function (Blueprint $table) {
16
                $table->integer('total_voter_count')->unsigned()->nullable()->comment('Total number of users that voted in the poll')->after('options');
17
                $table->boolean('is_anonymous')->default(1)->comment('True, if the poll is anonymous')->after('is_closed');
18
                $table->char('type', 255)->comment('Poll type, currently can be "regular" or "quiz"')->after('is_anonymous');
19
                $table->boolean('allows_multiple_answers')->default(0)->comment('True, if the poll allows multiple answers')->after('type');
20
                $table->integer('correct_option_id')->unsigned()->nullable()->comment('0-based identifier of the correct answer option. Available only for polls in the quiz mode, which are closed, or was sent (not forwarded) by the bot or to the private chat with the bot.')->after('allows_multiple_answers');
21
            });
22
23
            Schema::table($this->prefix . 'message', static function (Blueprint $table) {
24
                $table->text('dice')->nullable()->comment('Message is a dice with random value from 1 to 6')->after('poll');
25
            });
26
27
            Schema::create($this->prefix . 'poll_answer', function (Blueprint $table) {
28
                $table->bigInteger('poll_id')->unsigned()->comment('Unique poll identifier');
29
                $table->bigInteger('user_id')->comment('The user, who changed the answer to the poll');
30
                $table->text('option_ids')->comment('0-based identifiers of answer options, chosen by the user. May be empty if the user retracted their vote.');
31
                $table->timestamp('created_at')->nullable()->comment('Entry date creation');
32
                $table->primary(['poll_id', 'user_id']);
33
                $table->foreign('poll_id', 'poll_answer_ibfk_1')->references('id')->on($this->prefix . 'poll')->onUpdate('RESTRICT')->onDelete('RESTRICT');
34
            });
35
36
            Schema::table($this->prefix . 'telegram_update', function (Blueprint $table) {
37
                $table->bigInteger('poll_answer_poll_id')->unsigned()->nullable()->comment('A user changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were sent by the bot itself.')->after('poll_id');
38
                $table->index('poll_answer_poll_id', 'poll_answer_poll_id');
39
                $table->foreign('poll_answer_poll_id', 'telegram_update_ibfk_11')->references('poll_id')->on($this->prefix . 'poll_answer')->onUpdate('RESTRICT')->onDelete('RESTRICT');
40
            });
41
        } catch (Throwable $e) {
42
            Log::error($e->getMessage());
43
            return; // Migration may be partly done already...
44
        }
45
    }
46
47
    public function down(): void
48
    {
49
        try {
50
            Schema::table($this->prefix . 'telegram_update', static function (Blueprint $table) {
51
                $table->dropForeign('telegram_update_ibfk_11');
52
                $table->dropIndex('poll_answer_poll_id');
53
                $table->dropColumn('poll_answer_poll_id');
54
            });
55
56
            Schema::dropIfExists($this->prefix . 'poll_answer');
57
58
            Schema::table($this->prefix . 'message', static function (Blueprint $table) {
59
                $table->dropColumn('dice');
60
            });
61
62
            Schema::table($this->prefix . 'poll', static function (Blueprint $table) {
63
                $table->dropColumn('correct_option_id');
64
                $table->dropColumn('allows_multiple_answers');
65
                $table->dropColumn('type');
66
                $table->dropColumn('is_anonymous');
67
                $table->dropColumn('total_voter_count');
68
            });
69
        } catch (Throwable $e) {
70
            Log::error($e->getMessage());
71
            return; // Migration may be partly done already...
72
        }
73
    }
74
}
75