|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* components |
|
4
|
|
|
* |
|
5
|
|
|
* @author Wolfy-J |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Spiral\Database\Drivers\Postgres; |
|
8
|
|
|
|
|
9
|
|
|
use Spiral\Database\Drivers\Postgres\Schemas\PostgresColumn; |
|
10
|
|
|
use Spiral\Database\Entities\AbstractHandler; |
|
11
|
|
|
use Spiral\Database\Exceptions\SchemaException; |
|
12
|
|
|
use Spiral\Database\Schemas\Prototypes\AbstractColumn; |
|
13
|
|
|
use Spiral\Database\Schemas\Prototypes\AbstractTable; |
|
14
|
|
|
|
|
15
|
|
|
class PostgresHandler extends AbstractHandler |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* {@inheritdoc} |
|
19
|
|
|
* |
|
20
|
|
|
* @throws SchemaException |
|
21
|
|
|
*/ |
|
22
|
|
|
public function alterColumn( |
|
23
|
|
|
AbstractTable $table, |
|
24
|
|
|
AbstractColumn $initial, |
|
25
|
|
|
AbstractColumn $column |
|
26
|
|
|
) { |
|
27
|
|
|
if (!$initial instanceof PostgresColumn || !$column instanceof PostgresColumn) { |
|
28
|
|
|
throw new SchemaException('Postgres handler can work only with Postgres columns'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
//Rename is separate operation |
|
32
|
|
|
if ($column->getName() != $initial->getName()) { |
|
33
|
|
|
$this->renameColumn($table, $initial, $column); |
|
34
|
|
|
|
|
35
|
|
|
//This call is required to correctly built set of alter operations |
|
36
|
|
|
$initial->setName($column->getName()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
//Postgres columns should be altered using set of operations |
|
40
|
|
|
$operations = $column->alterOperations($this->driver, $initial); |
|
41
|
|
|
if (empty($operations)) { |
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
//Postgres columns should be altered using set of operations |
|
46
|
|
|
$query = \Spiral\interpolate('ALTER TABLE {table} {operations}', [ |
|
47
|
|
|
'table' => $this->identify($table), |
|
48
|
|
|
'operations' => trim(implode(', ', $operations), ', '), |
|
49
|
|
|
]); |
|
50
|
|
|
|
|
51
|
|
|
$this->run($query); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param AbstractTable $table |
|
56
|
|
|
* @param AbstractColumn $initial |
|
57
|
|
|
* @param AbstractColumn $column |
|
58
|
|
|
*/ |
|
59
|
|
|
private function renameColumn( |
|
60
|
|
|
AbstractTable $table, |
|
61
|
|
|
AbstractColumn $initial, |
|
62
|
|
|
AbstractColumn $column |
|
63
|
|
|
) { |
|
64
|
|
|
$statement = \Spiral\interpolate('ALTER TABLE {table} RENAME COLUMN {column} TO {name}', [ |
|
65
|
|
|
'table' => $this->identify($table), |
|
66
|
|
|
'column' => $this->identify($initial), |
|
67
|
|
|
'name' => $this->identify($column) |
|
68
|
|
|
]); |
|
69
|
|
|
|
|
70
|
|
|
$this->run($statement); |
|
71
|
|
|
} |
|
72
|
|
|
} |