1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Umbrellio\Postgres\Schema; |
||
6 | |||
7 | use Illuminate\Database\Schema\Blueprint as BaseBlueprint; |
||
8 | use Illuminate\Database\Schema\ColumnDefinition; |
||
9 | use Illuminate\Support\Facades\Schema; |
||
10 | use Illuminate\Support\Fluent; |
||
11 | use Umbrellio\Postgres\Schema\Builders\Constraints\Check\CheckBuilder; |
||
12 | use Umbrellio\Postgres\Schema\Builders\Constraints\Exclude\ExcludeBuilder; |
||
13 | use Umbrellio\Postgres\Schema\Builders\Indexes\Unique\UniqueBuilder; |
||
14 | use Umbrellio\Postgres\Schema\Builders\Routines\CreateFunctionBuilder; |
||
15 | use Umbrellio\Postgres\Schema\Builders\Routines\CreateProcedureBuilder; |
||
16 | use Umbrellio\Postgres\Schema\Builders\Routines\CreateTriggerBuilder; |
||
17 | use Umbrellio\Postgres\Schema\Builders\Routines\DropFunctionBuilder; |
||
18 | use Umbrellio\Postgres\Schema\Builders\Routines\DropProcedureBuilder; |
||
19 | use Umbrellio\Postgres\Schema\Builders\Routines\DropTriggerBuilder; |
||
20 | use Umbrellio\Postgres\Schema\Definitions\Indexes\CheckDefinition; |
||
21 | use Umbrellio\Postgres\Schema\Definitions\Indexes\ExcludeDefinition; |
||
22 | use Umbrellio\Postgres\Schema\Definitions\Indexes\UniqueDefinition; |
||
23 | use Umbrellio\Postgres\Schema\Definitions\Routines\Functions\CreateFunctionDefinition; |
||
24 | use Umbrellio\Postgres\Schema\Definitions\Routines\Functions\DropFunctionDefinition; |
||
25 | use Umbrellio\Postgres\Schema\Definitions\Routines\Procedures\CreateProcedureDefinition; |
||
26 | use Umbrellio\Postgres\Schema\Definitions\Routines\Procedures\DropProcedureDefinition; |
||
27 | use Umbrellio\Postgres\Schema\Definitions\Routines\Triggers\CreateTriggerDefinition; |
||
28 | use Umbrellio\Postgres\Schema\Definitions\Routines\Triggers\DropTriggerDefinition; |
||
29 | use Umbrellio\Postgres\Schema\Definitions\Tables\AttachPartitionDefinition; |
||
30 | use Umbrellio\Postgres\Schema\Definitions\Tables\LikeDefinition; |
||
31 | use Umbrellio\Postgres\Schema\Definitions\Views\ViewDefinition; |
||
32 | |||
33 | class Blueprint extends BaseBlueprint |
||
34 | { |
||
35 | /** |
||
36 | * @return AttachPartitionDefinition |
||
37 | */ |
||
38 | public function attachPartition(string $partition) |
||
39 | { |
||
40 | return $this->addCommand('attachPartition', compact('partition')); |
||
41 | } |
||
42 | |||
43 | public function detachPartition(string $partition): void |
||
44 | { |
||
45 | $this->addCommand('detachPartition', compact('partition')); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @return LikeDefinition |
||
50 | */ |
||
51 | public function like(string $table) |
||
52 | { |
||
53 | return $this->addCommand('like', compact('table')); |
||
54 | } |
||
55 | |||
56 | public function ifNotExists(): Fluent |
||
57 | { |
||
58 | return $this->addCommand('ifNotExists'); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param array|string $columns |
||
63 | * @return UniqueDefinition |
||
64 | */ |
||
65 | public function uniquePartial($columns, ?string $index = null, ?string $algorithm = null) |
||
66 | { |
||
67 | $columns = (array) $columns; |
||
68 | |||
69 | $index = $index ?: $this->createIndexName('unique', $columns); |
||
70 | |||
71 | return $this->addExtendedCommand( |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
72 | UniqueBuilder::class, |
||
73 | 'uniquePartial', |
||
74 | compact('columns', 'index', 'algorithm') |
||
75 | ); |
||
76 | } |
||
77 | |||
78 | public function dropUniquePartial($index): Fluent |
||
79 | { |
||
80 | return $this->dropIndexCommand('dropIndex', 'unique', $index); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param array|string $columns |
||
85 | * @return ExcludeDefinition |
||
86 | */ |
||
87 | public function exclude($columns, ?string $index = null) |
||
88 | { |
||
89 | $columns = (array) $columns; |
||
90 | |||
91 | $index = $index ?: $this->createIndexName('excl', $columns); |
||
92 | |||
93 | return $this->addExtendedCommand(ExcludeBuilder::class, 'exclude', compact('columns', 'index')); |
||
0 ignored issues
–
show
|
|||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param array|string $columns |
||
98 | * @return CheckDefinition |
||
99 | */ |
||
100 | public function check($columns, ?string $index = null) |
||
101 | { |
||
102 | $columns = (array) $columns; |
||
103 | |||
104 | $index = $index ?: $this->createIndexName('chk', $columns); |
||
105 | |||
106 | return $this->addExtendedCommand(CheckBuilder::class, 'check', compact('columns', 'index')); |
||
0 ignored issues
–
show
|
|||
107 | } |
||
108 | |||
109 | public function dropExclude($index): Fluent |
||
110 | { |
||
111 | return $this->dropIndexCommand('dropUnique', 'excl', $index); |
||
112 | } |
||
113 | |||
114 | public function dropCheck($index): Fluent |
||
115 | { |
||
116 | return $this->dropIndexCommand('dropUnique', 'chk', $index); |
||
117 | } |
||
118 | |||
119 | public function hasIndex($index, bool $unique = false): bool |
||
120 | { |
||
121 | if (is_array($index)) { |
||
122 | $index = $this->createIndexName($unique === false ? 'index' : 'unique', $index); |
||
123 | } |
||
124 | |||
125 | return array_key_exists($index, $this->getSchemaManager()->listTableIndexes($this->getTable())); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @return ViewDefinition |
||
130 | */ |
||
131 | public function createView(string $view, string $select, bool $materialize = false) |
||
132 | { |
||
133 | return $this->addCommand('createView', compact('view', 'select', 'materialize')); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @return CreateTriggerDefinition |
||
138 | */ |
||
139 | public function createTrigger(string $name) |
||
140 | { |
||
141 | return $this->addExtendedCommand(CreateTriggerBuilder::class, 'createTrigger', compact('name')); |
||
0 ignored issues
–
show
|
|||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return CreateFunctionDefinition |
||
146 | */ |
||
147 | public function createFunction(string $name) |
||
148 | { |
||
149 | return $this->addExtendedCommand(CreateFunctionBuilder::class, 'createFunction', compact('name')); |
||
0 ignored issues
–
show
|
|||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @return CreateProcedureDefinition |
||
154 | */ |
||
155 | public function createProcedure(string $name) |
||
156 | { |
||
157 | return $this->addExtendedCommand(CreateProcedureBuilder::class, 'createProcedure', compact('name')); |
||
0 ignored issues
–
show
|
|||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @return DropFunctionDefinition |
||
162 | */ |
||
163 | public function dropFunction(string $name) |
||
164 | { |
||
165 | return $this->addExtendedCommand(DropFunctionBuilder::class, 'dropFunction', compact('name')); |
||
0 ignored issues
–
show
|
|||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @return DropProcedureDefinition |
||
170 | */ |
||
171 | public function dropProcedure(string $name) |
||
172 | { |
||
173 | return $this->addExtendedCommand(DropProcedureBuilder::class, 'dropProcedure', compact('name')); |
||
0 ignored issues
–
show
|
|||
174 | } |
||
175 | |||
176 | public function dropView(string $view): Fluent |
||
177 | { |
||
178 | return $this->addCommand('dropView', compact('view')); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return DropTriggerDefinition |
||
183 | */ |
||
184 | public function dropTrigger(string $name, bool $dropDepends = false) |
||
185 | { |
||
186 | return $this->addExtendedCommand(DropTriggerBuilder::class, 'dropTrigger', compact('name', 'dropDepends')); |
||
0 ignored issues
–
show
|
|||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Almost like 'decimal' type, but can be with variable precision (by default) |
||
191 | */ |
||
192 | public function numeric(string $column, ?int $precision = null, ?int $scale = null): ColumnDefinition |
||
193 | { |
||
194 | return $this->addColumn('numeric', $column, compact('precision', 'scale')); |
||
195 | } |
||
196 | |||
197 | public function tsrange(string $column): ColumnDefinition |
||
198 | { |
||
199 | return $this->addColumn('tsrange', $column); |
||
200 | } |
||
201 | |||
202 | protected function getSchemaManager() |
||
203 | { |
||
204 | return Schema::getConnection()->getDoctrineSchemaManager(); |
||
205 | } |
||
206 | |||
207 | private function addExtendedCommand(string $fluent, string $name, array $parameters = []) |
||
208 | { |
||
209 | $command = new $fluent(array_merge(compact('name'), $parameters)); |
||
210 | $this->commands[] = $command; |
||
211 | return $command; |
||
212 | } |
||
213 | } |
||
214 |