|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PhpTelegramBot/Laravel package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace PhpTelegramBot\Laravel; |
|
15
|
|
|
|
|
16
|
|
|
use Illuminate\Database\Migrations\Migration as LaravelMigration; |
|
17
|
|
|
use Illuminate\Support\Facades\DB; |
|
18
|
|
|
|
|
19
|
|
|
class Migration extends LaravelMigration |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $prefix = ''; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->prefix = (string) config('phptelegrambot.database.prefix', ''); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Change column type for passed table field(s). |
|
31
|
|
|
* |
|
32
|
|
|
* @param array $tableColumns |
|
33
|
|
|
* @param string $newType |
|
34
|
|
|
*/ |
|
35
|
|
|
public function changeColumnTypes(array $tableColumns, string $newType): void |
|
36
|
|
|
{ |
|
37
|
|
|
$database = DB::connection()->getDatabaseName(); |
|
38
|
|
|
$prefix = DB::connection()->getTablePrefix() . $this->prefix; |
|
39
|
|
|
|
|
40
|
|
|
foreach ($tableColumns as $table => $columns) { |
|
41
|
|
|
foreach ($columns as $column) { |
|
42
|
|
|
// Preserve column comment and nullable state. |
|
43
|
|
|
$props = DB::selectOne(' |
|
44
|
|
|
SELECT `COLUMN_COMMENT`, `IS_NULLABLE` |
|
45
|
|
|
FROM `information_schema`.`COLUMNS` |
|
46
|
|
|
WHERE `TABLE_SCHEMA` = ? |
|
47
|
|
|
AND `TABLE_NAME` = ? |
|
48
|
|
|
AND `COLUMN_NAME` = ? |
|
49
|
|
|
LIMIT 1 |
|
50
|
|
|
', [$database, $prefix . $table, $column]); |
|
51
|
|
|
|
|
52
|
|
|
$comment = $props->COLUMN_COMMENT; |
|
53
|
|
|
$nullable = $props->IS_NULLABLE === 'YES' ? 'NULL' : 'NOT NULL'; |
|
54
|
|
|
DB::statement("ALTER TABLE `{$prefix}{$table}` CHANGE `{$column}` `{$column}` {$newType} {$nullable} COMMENT '{$comment}'"); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|