Skip to content

Commit 1fc7f8d

Browse files
authored
docs: restore reference content lost in docs split (#1073)
Follow-up to #1070. The writing-migrations split dropped several reference sections and left some RST-conversion artifacts. - Fix `bin/cake migration migrate` typo in integration-and-deployment - Fix missing space around `phinxlog` tables in upgrade guide - Replace leftover `<span class="title-ref">...</span>` with backticks in seeding and using-the-query-builder - Restore "Anonymous Migration Classes" section in creating-migrations - Restore full Table Partitioning examples (RANGE, LIST, HASH, KEY, expressions, modifying existing partitions) - Restore index examples for MySQL fulltext/length, SQL Server and PostgreSQL include columns, partial indexes, concurrent indexes, and GIN indexes - Restore foreign key details: composite keys, options table, and hasForeignKey/dropForeignKey usage - Restore check constraint details: fluent builder, auto-generated names, complex expressions, has/drop examples
1 parent 5843667 commit 1fc7f8d

File tree

7 files changed

+504
-52
lines changed

7 files changed

+504
-52
lines changed

docs/en/advanced/integration-and-deployment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ columns not existing when performing operations on those new columns. The
192192
CakePHP core includes a [Schema Cache Shell](https://book.cakephp.org/5/en/console-and-shells/schema-cache.html) that you can use:
193193

194194
```bash
195-
bin/cake migration migrate
195+
bin/cake migrations migrate
196196
bin/cake schema_cache clear
197197
```
198198

docs/en/getting-started/creating-migrations.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,54 @@ You can also use the `underscore_form` as the name for your migrations, such as
4545
> migrations if the class names are not unique. In that case, you may need to
4646
> rename the migration manually.
4747
48+
## Anonymous Migration Classes
49+
50+
Migrations also supports generating anonymous migration classes, which use PHP's
51+
anonymous class feature instead of named classes. This style is useful for:
52+
53+
- Avoiding namespace declarations
54+
- Better PHPCS compatibility (no class name to filename matching required)
55+
- Simpler file structure without named class constraints
56+
- More readable filenames like `2024_12_08_120000_CreateProducts.php`
57+
58+
To generate an anonymous migration class, use the `--style anonymous` option:
59+
60+
```bash
61+
bin/cake bake migration CreateProducts --style anonymous
62+
```
63+
64+
This generates a migration file using an anonymous class:
65+
66+
```php
67+
<?php
68+
declare(strict_types=1);
69+
70+
use Migrations\BaseMigration;
71+
72+
return new class extends BaseMigration
73+
{
74+
public function change(): void
75+
{
76+
}
77+
};
78+
```
79+
80+
Both traditional and anonymous migration classes work identically at runtime
81+
and can be used interchangeably within the same project.
82+
83+
You can set the default migration style globally in your application
84+
configuration:
85+
86+
```php
87+
// In config/app.php or config/app_local.php
88+
'Migrations' => [
89+
'style' => 'anonymous', // or 'traditional'
90+
],
91+
```
92+
93+
This configuration also applies to seeds, allowing you to use consistent
94+
styling across your entire project.
95+
4896
## Creating a Table
4997

5098
You can use `bake migration` to create a table:

docs/en/guides/seeding.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ The method takes three arguments:
509509
## Truncating Tables
510510

511511
In addition to inserting data Migrations makes it trivial to empty your tables using the
512-
SQL <span class="title-ref">TRUNCATE</span> command:
512+
SQL `TRUNCATE` command:
513513

514514
```php
515515
<?php
@@ -574,7 +574,7 @@ bin/cake seeds run User,Permission,Log
574574
bin/cake seeds run UserSeed,PermissionSeed,LogSeed
575575
```
576576

577-
You can also use the <span class="title-ref">-v</span> parameter for more output verbosity:
577+
You can also use the `-v` parameter for more output verbosity:
578578

579579
```bash
580580
bin/cake seeds run -v

docs/en/guides/using-the-query-builder.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Migrations provides access to a Query builder object, that you may use to execut
77

88
The Query builder is provided by the [cakephp/database](https://github.com/cakephp/database) project, and should
99
be easy to work with as it resembles very closely plain SQL. Accesing the query builder is done by calling the
10-
`getQueryBuilder(string $type)` function. The `string $type` options are <span class="title-ref">'select'</span>, <span class="title-ref">'insert'</span>, <span class="title-ref">'update'</span> and \`'delete'\`:
10+
`getQueryBuilder(string $type)` function. The `string $type` options are `'select'`, `'insert'`, `'update'` and `'delete'`:
1111

1212
```php
1313
<?php

docs/en/guides/writing-migrations/columns-and-table-operations.md

Lines changed: 169 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,177 @@ Migrations supports table partitioning for MySQL and PostgreSQL. Partitioning
201201
helps manage large tables by splitting them into smaller, more manageable
202202
pieces.
203203

204-
Supported strategies include:
204+
> [!NOTE]
205+
> Partition columns must be included in the primary key for MySQL. SQLite does
206+
> not support partitioning. MySQL's `RANGE` and `LIST` types only work with
207+
> integer columns - use `RANGE COLUMNS` and `LIST COLUMNS` for DATE/STRING
208+
> columns.
209+
210+
### RANGE Partitioning
211+
212+
RANGE partitioning is useful when you want to partition by numeric ranges. For
213+
MySQL, use `TYPE_RANGE` with integer columns or expressions, and
214+
`TYPE_RANGE_COLUMNS` for DATE/DATETIME/STRING columns:
215+
216+
```php
217+
<?php
218+
use Migrations\BaseMigration;
219+
use Migrations\Db\Table\Partition;
220+
221+
class CreatePartitionedOrders extends BaseMigration
222+
{
223+
public function change(): void
224+
{
225+
$table = $this->table('orders', [
226+
'id' => false,
227+
'primary_key' => ['id', 'order_date'],
228+
]);
229+
$table->addColumn('id', 'integer', ['identity' => true])
230+
->addColumn('order_date', 'date')
231+
->addColumn('amount', 'decimal', ['precision' => 10, 'scale' => 2])
232+
->partitionBy(Partition::TYPE_RANGE_COLUMNS, 'order_date')
233+
->addPartition('p2022', '2023-01-01')
234+
->addPartition('p2023', '2024-01-01')
235+
->addPartition('p2024', '2025-01-01')
236+
->addPartition('pmax', 'MAXVALUE')
237+
->create();
238+
}
239+
}
240+
```
241+
242+
### LIST Partitioning
243+
244+
LIST partitioning is useful when you want to partition by discrete values. For
245+
MySQL, use `TYPE_LIST` with integer columns and `TYPE_LIST_COLUMNS` for STRING
246+
columns:
247+
248+
```php
249+
<?php
250+
use Migrations\BaseMigration;
251+
use Migrations\Db\Table\Partition;
252+
253+
class CreatePartitionedCustomers extends BaseMigration
254+
{
255+
public function change(): void
256+
{
257+
$table = $this->table('customers', [
258+
'id' => false,
259+
'primary_key' => ['id', 'region'],
260+
]);
261+
$table->addColumn('id', 'integer', ['identity' => true])
262+
->addColumn('region', 'string', ['limit' => 20])
263+
->addColumn('name', 'string')
264+
->partitionBy(Partition::TYPE_LIST_COLUMNS, 'region')
265+
->addPartition('p_americas', ['US', 'CA', 'MX', 'BR'])
266+
->addPartition('p_europe', ['UK', 'DE', 'FR', 'IT'])
267+
->addPartition('p_asia', ['JP', 'CN', 'IN', 'KR'])
268+
->create();
269+
}
270+
}
271+
```
272+
273+
### HASH Partitioning
274+
275+
HASH partitioning distributes data evenly across a specified number of
276+
partitions:
277+
278+
```php
279+
<?php
280+
use Migrations\BaseMigration;
281+
use Migrations\Db\Table\Partition;
282+
283+
class CreatePartitionedSessions extends BaseMigration
284+
{
285+
public function change(): void
286+
{
287+
$table = $this->table('sessions');
288+
$table->addColumn('user_id', 'integer')
289+
->addColumn('data', 'text')
290+
->partitionBy(Partition::TYPE_HASH, 'user_id', ['count' => 8])
291+
->create();
292+
}
293+
}
294+
```
205295

206-
- `RANGE`
207-
- `RANGE COLUMNS`
208-
- `LIST`
209-
- `LIST COLUMNS`
210-
- `HASH`
211-
- `KEY` *(MySQL only)*
296+
### KEY Partitioning (MySQL only)
212297

213-
You can also partition by expressions using `Literal::from(...)`, and add or
214-
drop partitions on existing tables.
298+
KEY partitioning is similar to HASH but uses MySQL's internal hashing function:
299+
300+
```php
301+
<?php
302+
use Migrations\BaseMigration;
303+
use Migrations\Db\Table\Partition;
304+
305+
class CreatePartitionedCache extends BaseMigration
306+
{
307+
public function change(): void
308+
{
309+
$table = $this->table('cache', [
310+
'id' => false,
311+
'primary_key' => ['cache_key'],
312+
]);
313+
$table->addColumn('cache_key', 'string', ['limit' => 255])
314+
->addColumn('value', 'binary')
315+
->partitionBy(Partition::TYPE_KEY, 'cache_key', ['count' => 16])
316+
->create();
317+
}
318+
}
319+
```
320+
321+
### Partitioning with Expressions
322+
323+
You can partition by expressions using the `Literal` class:
324+
325+
```php
326+
<?php
327+
use Migrations\BaseMigration;
328+
use Migrations\Db\Literal;
329+
use Migrations\Db\Table\Partition;
330+
331+
class CreatePartitionedEvents extends BaseMigration
332+
{
333+
public function change(): void
334+
{
335+
$table = $this->table('events', [
336+
'id' => false,
337+
'primary_key' => ['id', 'created_at'],
338+
]);
339+
$table->addColumn('id', 'integer', ['identity' => true])
340+
->addColumn('created_at', 'datetime')
341+
->partitionBy(Partition::TYPE_RANGE, Literal::from('YEAR(created_at)'))
342+
->addPartition('p2022', 2023)
343+
->addPartition('p2023', 2024)
344+
->addPartition('pmax', 'MAXVALUE')
345+
->create();
346+
}
347+
}
348+
```
349+
350+
### Modifying Partitions on Existing Tables
351+
352+
You can add or drop partitions on existing partitioned tables:
353+
354+
```php
355+
<?php
356+
use Migrations\BaseMigration;
357+
358+
class ModifyOrdersPartitions extends BaseMigration
359+
{
360+
public function up(): void
361+
{
362+
$this->table('orders')
363+
->addPartitionToExisting('p2025', '2026-01-01')
364+
->update();
365+
}
366+
367+
public function down(): void
368+
{
369+
$this->table('orders')
370+
->dropPartition('p2025')
371+
->update();
372+
}
373+
}
374+
```
215375

216376
## Saving Changes
217377

0 commit comments

Comments
 (0)