1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Migration; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Schema\ColumnSchemaBuilder; |
8
|
|
|
use Yiisoft\Db\Schema\Schema; |
9
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* AbstractMigrationBuilder contains shortcut methods to create instances of {@see ColumnSchemaBuilder}. |
13
|
|
|
* |
14
|
|
|
* These can be used in database migrations to define database schema types using a PHP interface. This is useful to |
15
|
|
|
* define a schema in a DBMS independent way so that the application may run on different DBMS the same way. |
16
|
|
|
* |
17
|
|
|
* For example, you may use the following code inside your migration files: |
18
|
|
|
* |
19
|
|
|
* ```php |
20
|
|
|
* $this->createTable( |
21
|
|
|
* 'example_table', |
22
|
|
|
* [ |
23
|
|
|
* 'id' => $this->primaryKey(), |
24
|
|
|
* 'name' => $this->string(64)->notNull(), |
25
|
|
|
* 'type' => $this->integer()->notNull()->defaultValue(10), |
26
|
|
|
* 'description' => $this->text(), |
27
|
|
|
* 'rule_name' => $this->string(64), |
28
|
|
|
* 'data' => $this->text(), |
29
|
|
|
* 'created_at' => $this->datetime()->notNull(), |
30
|
|
|
* 'updated_at' => $this->datetime(), |
31
|
|
|
* ], |
32
|
|
|
* ); |
33
|
|
|
* ``` |
34
|
|
|
*/ |
35
|
|
|
abstract class AbstractMigrationBuilder |
36
|
|
|
{ |
37
|
|
|
public function __construct(private SchemaInterface $schema) |
38
|
|
|
{ |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Creates a bigint column. |
43
|
|
|
* |
44
|
|
|
* @param int|null $length The column size or precision definition. |
45
|
|
|
* |
46
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
47
|
|
|
* |
48
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
49
|
|
|
*/ |
50
|
|
|
public function bigInteger(int $length = null): ColumnSchemaBuilder |
51
|
|
|
{ |
52
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_BIGINT, $length); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Creates a big primary key column. |
57
|
|
|
* |
58
|
|
|
* @param int|null $length The column size or precision definition. |
59
|
|
|
* |
60
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
61
|
|
|
* |
62
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
63
|
|
|
*/ |
64
|
|
|
public function bigPrimaryKey(int $length = null): ColumnSchemaBuilder |
65
|
|
|
{ |
66
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, $length); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Creates a binary column. |
71
|
|
|
* |
72
|
|
|
* @param int|null $length The column size or precision definition. |
73
|
|
|
* |
74
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
75
|
|
|
* |
76
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
77
|
|
|
*/ |
78
|
|
|
public function binary(int $length = null): ColumnSchemaBuilder |
79
|
|
|
{ |
80
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_BINARY, $length); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Creates a boolean column. |
85
|
|
|
* |
86
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
87
|
|
|
*/ |
88
|
|
|
public function boolean(): ColumnSchemaBuilder |
89
|
|
|
{ |
90
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Creates a char column. |
95
|
|
|
* |
96
|
|
|
* @param int|null $length the column size definition i.e. the maximum string length. |
97
|
|
|
* |
98
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
99
|
|
|
* |
100
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
101
|
|
|
*/ |
102
|
|
|
public function char(int $length = null): ColumnSchemaBuilder |
103
|
|
|
{ |
104
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_CHAR, $length); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Creates a date column. |
109
|
|
|
* |
110
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
111
|
|
|
*/ |
112
|
|
|
public function date(): ColumnSchemaBuilder |
113
|
|
|
{ |
114
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_DATE); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Creates a datetime column. |
119
|
|
|
* |
120
|
|
|
* @param int|null $precision The column value precision. First parameter passed to the column type, e.g. |
121
|
|
|
* DATETIME(precision). |
122
|
|
|
* |
123
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
124
|
|
|
* |
125
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
126
|
|
|
*/ |
127
|
|
|
public function dateTime(int $precision = null): ColumnSchemaBuilder |
128
|
|
|
{ |
129
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_DATETIME, $precision); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Creates a decimal column. |
134
|
|
|
* |
135
|
|
|
* @param int|null $precision The column value precision, which is usually the total number of digits. |
136
|
|
|
* First parameter passed to the column type, e.g. DECIMAL(precision, scale). |
137
|
|
|
* |
138
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
139
|
|
|
* @param int|null $scale The column value scale, which is usually the number of digits after the decimal point. |
140
|
|
|
* Second parameter passed to the column type, e.g. DECIMAL(precision, scale). |
141
|
|
|
* |
142
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
143
|
|
|
* |
144
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
145
|
|
|
*/ |
146
|
|
|
public function decimal(int $precision = null, int $scale = null): ColumnSchemaBuilder |
147
|
|
|
{ |
148
|
|
|
$length = []; |
149
|
|
|
|
150
|
|
|
if ($precision !== null) { |
151
|
|
|
$length[] = $precision; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if ($scale !== null) { |
155
|
|
|
$length[] = $scale; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_DECIMAL, $length); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Creates a double column. |
163
|
|
|
* |
164
|
|
|
* @param int|null $precision The column value precision. First parameter passed to the column type, e.g. |
165
|
|
|
* DOUBLE(precision). |
166
|
|
|
* |
167
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
168
|
|
|
* |
169
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
170
|
|
|
*/ |
171
|
|
|
public function double(int $precision = null): ColumnSchemaBuilder |
172
|
|
|
{ |
173
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_DOUBLE, $precision); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Creates a float column. |
178
|
|
|
* |
179
|
|
|
* @param int|null $precision The column value precision. First parameter passed to the column type, e.g. |
180
|
|
|
* FLOAT(precision). |
181
|
|
|
* |
182
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
183
|
|
|
* |
184
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
185
|
|
|
*/ |
186
|
|
|
public function float(int $precision = null): ColumnSchemaBuilder |
187
|
|
|
{ |
188
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_FLOAT, $precision); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Creates an integer column. |
193
|
|
|
* |
194
|
|
|
* @param int|null $length The column size or precision definition. |
195
|
|
|
* |
196
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
197
|
|
|
* |
198
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
199
|
|
|
*/ |
200
|
|
|
public function integer(int $length = null): ColumnSchemaBuilder |
201
|
|
|
{ |
202
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_INTEGER, $length); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Creates a JSON column. |
207
|
|
|
* |
208
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
209
|
|
|
*/ |
210
|
|
|
public function json(): ColumnSchemaBuilder |
211
|
|
|
{ |
212
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_JSON); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Creates a money column. |
217
|
|
|
* |
218
|
|
|
* @param int|null $precision The column value precision, which is usually the total number of digits. First |
219
|
|
|
* parameter passed to the column type, e.g. DECIMAL(precision, scale). |
220
|
|
|
* |
221
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
222
|
|
|
* @param int|null $scale The column value scale, which is usually the number of digits after the decimal point. |
223
|
|
|
* Second parameter passed to the column type, e.g. DECIMAL(precision, scale). |
224
|
|
|
* |
225
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
226
|
|
|
* |
227
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
228
|
|
|
*/ |
229
|
|
|
public function money(int $precision = null, int $scale = null): ColumnSchemaBuilder |
230
|
|
|
{ |
231
|
|
|
$length = []; |
232
|
|
|
|
233
|
|
|
if ($precision !== null) { |
234
|
|
|
$length[] = $precision; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if ($scale !== null) { |
238
|
|
|
$length[] = $scale; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_MONEY, $length); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Creates a primary key column. |
246
|
|
|
* |
247
|
|
|
* @param int|null $length The column size or precision definition. |
248
|
|
|
* |
249
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
250
|
|
|
* |
251
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
252
|
|
|
*/ |
253
|
|
|
public function primaryKey(int $length = null): ColumnSchemaBuilder |
254
|
|
|
{ |
255
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_PK, $length); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Creates a smallint column. |
260
|
|
|
* |
261
|
|
|
* @param int|null $length The column size or precision definition. |
262
|
|
|
* |
263
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
264
|
|
|
* |
265
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
266
|
|
|
*/ |
267
|
|
|
public function smallInteger(int $length = null): ColumnSchemaBuilder |
268
|
|
|
{ |
269
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_SMALLINT, $length); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Creates a string column. |
274
|
|
|
* |
275
|
|
|
* @param int|null $length The column size definition i.e. the maximum string length. |
276
|
|
|
* |
277
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
278
|
|
|
* |
279
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
280
|
|
|
*/ |
281
|
|
|
public function string(int $length = null): ColumnSchemaBuilder |
282
|
|
|
{ |
283
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_STRING, $length); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Creates a text column. |
288
|
|
|
* |
289
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
290
|
|
|
*/ |
291
|
|
|
public function text(): ColumnSchemaBuilder |
292
|
|
|
{ |
293
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_TEXT); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Creates a time column. |
298
|
|
|
* |
299
|
|
|
* @param int|null $precision The column value precision. First parameter passed to the column type, e.g. |
300
|
|
|
* TIME(precision). |
301
|
|
|
* |
302
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
303
|
|
|
* |
304
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
305
|
|
|
*/ |
306
|
|
|
public function time(int $precision = null): ColumnSchemaBuilder |
307
|
|
|
{ |
308
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_TIME, $precision); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Creates a timestamp column. |
313
|
|
|
* |
314
|
|
|
* @param int|null $precision The column value precision. First parameter passed to the column type, e.g. |
315
|
|
|
* TIMESTAMP(precision). |
316
|
|
|
* |
317
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
318
|
|
|
* |
319
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
320
|
|
|
*/ |
321
|
|
|
public function timestamp(int $precision = null): ColumnSchemaBuilder |
322
|
|
|
{ |
323
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_TIMESTAMP, $precision); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used. |
328
|
|
|
* |
329
|
|
|
* @param int|null $length The column size or precision definition. |
330
|
|
|
* |
331
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
332
|
|
|
* |
333
|
|
|
* @return ColumnSchemaBuilder The column instance which can be further customized. |
334
|
|
|
*/ |
335
|
|
|
public function tinyInteger(int $length = null): ColumnSchemaBuilder |
336
|
|
|
{ |
337
|
|
|
return $this->schema->createColumnSchemaBuilder(Schema::TYPE_TINYINT, $length); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|