AddPrefixToTables::up()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 8
c 1
b 0
f 1
nc 6
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
use Illuminate\Support\Facades\Log;
6
use Illuminate\Support\Facades\Schema;
7
use PhpTelegramBot\Laravel\Migration;
8
9
class AddPrefixToTables extends Migration
10
{
11
    /** @var string[] All PHP Telegram Bot database tables. */
12
    private $tables = [
13
        'botan_shortener',
14
        'callback_query',
15
        'chat',
16
        'chosen_inline_result',
17
        'conversation',
18
        'edited_message',
19
        'inline_query',
20
        'message',
21
        'user',
22
        'request_limiter',
23
        'telegram_update',
24
        'user_chat',
25
    ];
26
27
    public function up(): void
28
    {
29
        foreach ($this->tables as $table) {
30
            try {
31
                if (Schema::hasTable($this->prefix . $table)) {
32
                    Log::warning("Prefixed table '{$this->prefix}{$table}' already exists. Verify your migration status.");
33
                    continue; // Migration may be partly done already...
34
                }
35
36
                Schema::rename($table, $this->prefix . $table);
37
            } catch (Throwable $e) {
38
                Log::error($e->getMessage());
39
            }
40
        }
41
    }
42
43
    public function down(): void
44
    {
45
        foreach ($this->tables as $table) {
46
            try {
47
                if (Schema::hasTable($table)) {
48
                    Log::warning("Un-prefixed table '{$table}' already exists. Verify your migration status.");
49
                    continue; // Migration may be partly done already...
50
                }
51
52
                Schema::rename($this->prefix . $table, $table);
53
            } catch (Throwable $e) {
54
                Log::error($e->getMessage());
55
            }
56
        }
57
    }
58
}
59