PrintChanges::describeColumns()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 8
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Generator;
6
7
use Cycle\Database\Schema\ComparatorInterface;
8
use Cycle\Schema\GeneratorInterface;
9
use Cycle\Schema\Registry;
10
use Cycle\Database\Schema\AbstractTable;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
final class PrintChanges implements GeneratorInterface
14
{
15
    private array $changes = [];
16
17
    public function __construct(
18
        private OutputInterface $output,
19
    ) {}
20
21
    public function run(Registry $registry): Registry
22
    {
23
        $this->changes = [];
24
        foreach ($registry->getIterator() as $e) {
25
            if ($registry->hasTable($e)) {
26
                $table = $registry->getTableSchema($e);
27
28
                if ($this->hasTableChanges($table)) {
29
                    $key = $registry->getDatabase($e) . ':' . $registry->getTable($e);
30
                    $this->changes[$key] = [
31
                        'database' => $registry->getDatabase($e),
32
                        'table' => $registry->getTable($e),
33
                        'schema' => $table,
34
                    ];
35
                }
36
            }
37
        }
38
39
        if (!$this->hasChanges()) {
40
            $this->output->writeln('<fg=yellow>No database changes has been detected</fg=yellow>');
41
42
            return $registry;
43
        }
44
45
        $this->output->writeln('<info>Schema changes:</info>');
46
47
        foreach ($this->changes as $change) {
48
            $this->output->write(\sprintf('• <fg=cyan>%s.%s</fg=cyan>', $change['database'], $change['table']));
49
            $this->describeChanges($change['schema']);
50
        }
51
52
        return $registry;
53
    }
54
55
    public function hasChanges(): bool
56
    {
57
        return $this->changes !== [];
58
    }
59
60
    private function describeChanges(AbstractTable $table): void
61
    {
62
        if ($table->getStatus() === AbstractTable::STATUS_DECLARED_DROPPED) {
63
            $this->output->writeln('    - drop table');
64
            return;
65
        }
66
67
        if (!$this->output->isVerbose()) {
68
            $this->output->writeln(
69
                \sprintf(
70
                    ': <fg=green>%s</fg=green> change(s) detected',
71
                    $this->numChanges($table),
72
                ),
73
            );
74
75
            return;
76
        }
77
78
        $this->output->write("\n");
79
80
        if (!$table->exists()) {
81
            $this->output->writeln('    - create table');
82
        }
83
84
        $cmp = $table->getComparator();
85
86
        $this->describeColumns($cmp);
87
        $this->describeIndexes($cmp);
88
        $this->describeFKs($cmp);
89
    }
90
91
    private function describeColumns(ComparatorInterface $cmp): void
92
    {
93
        foreach ($cmp->addedColumns() as $column) {
94
            $this->output->writeln("    - add column <fg=yellow>[{$column->getName()}]</fg=yellow>");
95
        }
96
97
        foreach ($cmp->droppedColumns() as $column) {
98
            $this->output->writeln("    - drop column <fg=yellow>[{$column->getName()}]</fg=yellow>");
99
        }
100
101
        foreach ($cmp->alteredColumns() as $column) {
102
            $column = $column[0];
103
            $this->output->writeln("    - alter column <fg=yellow>[{$column->getName()}]</fg=yellow>");
104
        }
105
    }
106
107
    private function describeIndexes(ComparatorInterface $cmp): void
108
    {
109
        foreach ($cmp->addedIndexes() as $index) {
110
            $index = \implode(', ', $index->getColumns());
111
            $this->output->writeln("    - add index on <fg=yellow>[{$index}]</fg=yellow>");
112
        }
113
114
        foreach ($cmp->droppedIndexes() as $index) {
115
            $index = \implode(', ', $index->getColumns());
116
            $this->output->writeln("    - drop index on <fg=yellow>[{$index}]</fg=yellow>");
117
        }
118
119
        foreach ($cmp->alteredIndexes() as $index) {
120
            $index = $index[0];
121
            $index = \implode(', ', $index->getColumns());
122
            $this->output->writeln("    - alter index on <fg=yellow>[{$index}]</fg=yellow>");
123
        }
124
    }
125
126
    private function describeFKs(ComparatorInterface $cmp): void
127
    {
128
        foreach ($cmp->addedForeignKeys() as $fk) {
129
            $fkColumns = \implode(', ', $fk->getColumns());
130
            $this->output->writeln("    - add foreign key on <fg=yellow>[{$fkColumns}]</fg=yellow>");
131
        }
132
133
        foreach ($cmp->droppedForeignKeys() as $fk) {
134
            $fkColumns = \implode(', ', $fk->getColumns());
135
            $this->output->writeln("    - drop foreign key on <fg=yellow>[{$fkColumns}]</fg=yellow>");
136
        }
137
138
        foreach ($cmp->alteredForeignKeys() as $fk) {
139
            $fk = $fk[0];
140
            $fkColumns = \implode(', ', $fk->getColumns());
141
            $this->output->writeln("    - alter foreign key on <fg=yellow>[{$fkColumns}]</fg=yellow>");
142
        }
143
    }
144
145
    private function numChanges(AbstractTable $table): int
146
    {
147
        $cmp = $table->getComparator();
148
149
        return \count($cmp->addedColumns())
150
            + \count($cmp->droppedColumns())
151
            + \count($cmp->alteredColumns())
152
            + \count($cmp->addedIndexes())
153
            + \count($cmp->droppedIndexes())
154
            + \count($cmp->alteredIndexes())
155
            + \count($cmp->addedForeignKeys())
156
            + \count($cmp->droppedForeignKeys())
157
            + \count($cmp->alteredForeignKeys());
158
    }
159
160
    private function hasTableChanges(AbstractTable $table): bool
161
    {
162
        return $table->getComparator()->hasChanges()
163
            || !$table->exists()
164
            || $table->getStatus() === AbstractTable::STATUS_DECLARED_DROPPED;
165
    }
166
}
167