1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Umbrellio\Postgres\Schema; |
||
6 | |||
7 | use Doctrine\DBAL\DriverManager; |
||
8 | use Illuminate\Database\Schema\Blueprint as BaseBlueprint; |
||
9 | use Illuminate\Database\Schema\ColumnDefinition; |
||
10 | use Illuminate\Support\Facades\Schema; |
||
11 | use Illuminate\Support\Fluent; |
||
12 | use Umbrellio\Postgres\Schema\Builders\Constraints\Check\CheckBuilder; |
||
13 | use Umbrellio\Postgres\Schema\Builders\Constraints\Exclude\ExcludeBuilder; |
||
14 | use Umbrellio\Postgres\Schema\Builders\Indexes\Unique\UniqueBuilder; |
||
15 | use Umbrellio\Postgres\Schema\Definitions\AttachPartitionDefinition; |
||
16 | use Umbrellio\Postgres\Schema\Definitions\CheckDefinition; |
||
17 | use Umbrellio\Postgres\Schema\Definitions\ExcludeDefinition; |
||
18 | use Umbrellio\Postgres\Schema\Definitions\LikeDefinition; |
||
19 | use Umbrellio\Postgres\Schema\Definitions\UniqueDefinition; |
||
20 | use Umbrellio\Postgres\Schema\Definitions\ViewDefinition; |
||
21 | use Umbrellio\Postgres\Schema\Types\DateRangeType; |
||
22 | use Umbrellio\Postgres\Schema\Types\TsRangeType; |
||
23 | use Umbrellio\Postgres\Schema\Types\TsTzRangeType; |
||
24 | |||
25 | class Blueprint extends BaseBlueprint |
||
26 | { |
||
27 | protected $commands = []; |
||
28 | |||
29 | /** |
||
30 | * @return AttachPartitionDefinition|Fluent |
||
31 | */ |
||
32 | 4 | public function attachPartition(string $partition): Fluent |
|
33 | { |
||
34 | 4 | return $this->addCommand('attachPartition', compact('partition')); |
|
35 | } |
||
36 | |||
37 | 1 | public function detachPartition(string $partition): void |
|
38 | { |
||
39 | 1 | $this->addCommand('detachPartition', compact('partition')); |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * @codeCoverageIgnore |
||
44 | * @return LikeDefinition|Fluent |
||
45 | */ |
||
46 | public function like(string $table): Fluent |
||
47 | { |
||
48 | return $this->addCommand('like', compact('table')); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @codeCoverageIgnore |
||
53 | */ |
||
54 | public function ifNotExists(): Fluent |
||
55 | { |
||
56 | return $this->addCommand('ifNotExists'); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param array|string $columns |
||
61 | * @return UniqueDefinition|UniqueBuilder |
||
62 | */ |
||
63 | 13 | public function uniquePartial($columns, ?string $index = null, ?string $algorithm = null): Fluent |
|
64 | { |
||
65 | 13 | $columns = (array) $columns; |
|
66 | |||
67 | 13 | $index = $index ?: $this->createIndexName('unique', $columns); |
|
68 | |||
69 | 13 | return $this->addExtendedCommand( |
|
70 | 13 | UniqueBuilder::class, |
|
71 | 13 | 'uniquePartial', |
|
72 | 13 | compact('columns', 'index', 'algorithm') |
|
73 | 13 | ); |
|
74 | } |
||
75 | |||
76 | 12 | public function dropUniquePartial($index): Fluent |
|
77 | { |
||
78 | 12 | return $this->dropIndexCommand('dropIndex', 'unique', $index); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param array|string $columns |
||
83 | * @return ExcludeDefinition|ExcludeBuilder |
||
84 | */ |
||
85 | 6 | public function exclude($columns, ?string $index = null): Fluent |
|
86 | { |
||
87 | 6 | $columns = (array) $columns; |
|
88 | |||
89 | 6 | $index = $index ?: $this->createIndexName('excl', $columns); |
|
90 | |||
91 | 6 | return $this->addExtendedCommand(ExcludeBuilder::class, 'exclude', compact('columns', 'index')); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param array|string $columns |
||
96 | * @return CheckDefinition|CheckBuilder |
||
97 | */ |
||
98 | 3 | public function check($columns, ?string $index = null): Fluent |
|
99 | { |
||
100 | 3 | $columns = (array) $columns; |
|
101 | |||
102 | 3 | $index = $index ?: $this->createIndexName('chk', $columns); |
|
103 | |||
104 | 3 | return $this->addExtendedCommand(CheckBuilder::class, 'check', compact('columns', 'index')); |
|
105 | } |
||
106 | |||
107 | 1 | public function dropExclude($index): Fluent |
|
108 | { |
||
109 | 1 | return $this->dropIndexCommand('dropUnique', 'excl', $index); |
|
110 | } |
||
111 | |||
112 | 1 | public function dropCheck($index): Fluent |
|
113 | { |
||
114 | 1 | return $this->dropIndexCommand('dropUnique', 'chk', $index); |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * @codeCoverageIgnore |
||
119 | */ |
||
120 | public function hasIndex($index, bool $unique = false): bool |
||
121 | { |
||
122 | if (is_array($index)) { |
||
123 | $index = $this->createIndexName($unique === false ? 'index' : 'unique', $index); |
||
124 | } |
||
125 | |||
126 | return array_key_exists($index, $this->getSchemaManager()->listTableIndexes($this->getTable())); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @codeCoverageIgnore |
||
131 | * @return ViewDefinition|Fluent |
||
132 | */ |
||
133 | public function createView(string $view, string $select, bool $materialize = false): Fluent |
||
134 | { |
||
135 | return $this->addCommand('createView', compact('view', 'select', 'materialize')); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @codeCoverageIgnore |
||
140 | */ |
||
141 | public function dropView(string $view): Fluent |
||
142 | { |
||
143 | return $this->addCommand('dropView', compact('view')); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Almost like 'decimal' type, but can be with variable precision (by default) |
||
148 | * |
||
149 | * @return Fluent|ColumnDefinition |
||
150 | */ |
||
151 | 3 | public function numeric(string $column, ?int $precision = null, ?int $scale = null): Fluent |
|
152 | { |
||
153 | 3 | return $this->addColumn('numeric', $column, compact('precision', 'scale')); |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return Fluent|ColumnDefinition |
||
158 | */ |
||
159 | 1 | public function tsrange(string $column): Fluent |
|
160 | { |
||
161 | 1 | return $this->addColumn(TsRangeType::TYPE_NAME, $column); |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * @return Fluent|ColumnDefinition |
||
166 | */ |
||
167 | 1 | public function tstzrange(string $column): Fluent |
|
168 | { |
||
169 | 1 | return $this->addColumn(TsTzRangeType::TYPE_NAME, $column); |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return Fluent|ColumnDefinition |
||
174 | */ |
||
175 | 1 | public function daterange(string $column): Fluent |
|
176 | { |
||
177 | 1 | return $this->addColumn(DateRangeType::TYPE_NAME, $column); |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * @codeCoverageIgnore |
||
182 | */ |
||
183 | protected function getSchemaManager() |
||
184 | { |
||
185 | /** @scrutinizer ignore-call */ |
||
186 | $connection = Schema::getConnection(); |
||
187 | $doctrineConnection = DriverManager::getConnection($connection->getConfig()); |
||
188 | return $doctrineConnection->getSchemaManager(); |
||
0 ignored issues
–
show
|
|||
189 | } |
||
190 | |||
191 | 22 | private function addExtendedCommand(string $fluent, string $name, array $parameters = []): Fluent |
|
192 | { |
||
193 | 22 | $command = new $fluent(array_merge(compact('name'), $parameters)); |
|
194 | 22 | $this->commands[] = $command; |
|
195 | 22 | return $command; |
|
196 | } |
||
197 | } |
||
198 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.