1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Cycle\Migrations\Operation\Table; |
||
6 | |||
7 | use Cycle\Database\Driver\HandlerInterface; |
||
8 | use Cycle\Migrations\CapsuleInterface; |
||
9 | use Cycle\Migrations\Exception\Operation\TableException; |
||
10 | use Cycle\Migrations\Operation\AbstractOperation; |
||
11 | |||
12 | final class Rename extends AbstractOperation |
||
13 | { |
||
14 | 24 | public function __construct(string $table, private string $newName) |
|
15 | { |
||
16 | 24 | parent::__construct($table); |
|
17 | } |
||
18 | |||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | 24 | public function execute(CapsuleInterface $capsule): void |
|
23 | { |
||
24 | 24 | $schema = $capsule->getSchema($this->getTable()); |
|
25 | 24 | $database = $this->database ?? '[default]'; |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
26 | |||
27 | 24 | if (!$schema->exists()) { |
|
28 | 8 | throw new TableException( |
|
29 | 8 | "Unable to rename table '{$database}'.'{$this->getTable()}', table does not exists" |
|
30 | ); |
||
31 | } |
||
32 | |||
33 | 16 | if ($capsule->getDatabase()->hasTable($this->newName)) { |
|
34 | 8 | throw new TableException( |
|
35 | 8 | "Unable to rename table '{$database}'.'{$this->getTable()}', table '{$this->newName}' already exists" |
|
36 | ); |
||
37 | } |
||
38 | |||
39 | 8 | $schema->setName($this->newName); |
|
40 | 8 | $schema->save(HandlerInterface::DO_ALL); |
|
41 | } |
||
42 | } |
||
43 |