Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/en/advanced/integration-and-deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ columns not existing when performing operations on those new columns. The
CakePHP core includes a [Schema Cache Shell](https://book.cakephp.org/5/en/console-and-shells/schema-cache.html) that you can use:

```bash
bin/cake migration migrate
bin/cake migrations migrate
bin/cake schema_cache clear
```

Expand Down
48 changes: 48 additions & 0 deletions docs/en/getting-started/creating-migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,54 @@ You can also use the `underscore_form` as the name for your migrations, such as
> migrations if the class names are not unique. In that case, you may need to
> rename the migration manually.

## Anonymous Migration Classes

Migrations also supports generating anonymous migration classes, which use PHP's
anonymous class feature instead of named classes. This style is useful for:

- Avoiding namespace declarations
- Better PHPCS compatibility (no class name to filename matching required)
- Simpler file structure without named class constraints
- More readable filenames like `2024_12_08_120000_CreateProducts.php`

To generate an anonymous migration class, use the `--style anonymous` option:

```bash
bin/cake bake migration CreateProducts --style anonymous
```

This generates a migration file using an anonymous class:

```php
<?php
declare(strict_types=1);

use Migrations\BaseMigration;

return new class extends BaseMigration
{
public function change(): void
{
}
};
```

Both traditional and anonymous migration classes work identically at runtime
and can be used interchangeably within the same project.

You can set the default migration style globally in your application
configuration:

```php
// In config/app.php or config/app_local.php
'Migrations' => [
'style' => 'anonymous', // or 'traditional'
],
```

This configuration also applies to seeds, allowing you to use consistent
styling across your entire project.

## Creating a Table

You can use `bake migration` to create a table:
Expand Down
4 changes: 2 additions & 2 deletions docs/en/guides/seeding.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ The method takes three arguments:
## Truncating Tables

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

```php
<?php
Expand Down Expand Up @@ -574,7 +574,7 @@ bin/cake seeds run User,Permission,Log
bin/cake seeds run UserSeed,PermissionSeed,LogSeed
```

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

```bash
bin/cake seeds run -v
Expand Down
2 changes: 1 addition & 1 deletion docs/en/guides/using-the-query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Migrations provides access to a Query builder object, that you may use to execut

The Query builder is provided by the [cakephp/database](https://github.com/cakephp/database) project, and should
be easy to work with as it resembles very closely plain SQL. Accesing the query builder is done by calling the
`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'\`:
`getQueryBuilder(string $type)` function. The `string $type` options are `'select'`, `'insert'`, `'update'` and `'delete'`:

```php
<?php
Expand Down
178 changes: 169 additions & 9 deletions docs/en/guides/writing-migrations/columns-and-table-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,177 @@ Migrations supports table partitioning for MySQL and PostgreSQL. Partitioning
helps manage large tables by splitting them into smaller, more manageable
pieces.

Supported strategies include:
> [!NOTE]
> Partition columns must be included in the primary key for MySQL. SQLite does
> not support partitioning. MySQL's `RANGE` and `LIST` types only work with
> integer columns - use `RANGE COLUMNS` and `LIST COLUMNS` for DATE/STRING
> columns.

### RANGE Partitioning

RANGE partitioning is useful when you want to partition by numeric ranges. For
MySQL, use `TYPE_RANGE` with integer columns or expressions, and
`TYPE_RANGE_COLUMNS` for DATE/DATETIME/STRING columns:

```php
<?php
use Migrations\BaseMigration;
use Migrations\Db\Table\Partition;

class CreatePartitionedOrders extends BaseMigration
{
public function change(): void
{
$table = $this->table('orders', [
'id' => false,
'primary_key' => ['id', 'order_date'],
]);
$table->addColumn('id', 'integer', ['identity' => true])
->addColumn('order_date', 'date')
->addColumn('amount', 'decimal', ['precision' => 10, 'scale' => 2])
->partitionBy(Partition::TYPE_RANGE_COLUMNS, 'order_date')
->addPartition('p2022', '2023-01-01')
->addPartition('p2023', '2024-01-01')
->addPartition('p2024', '2025-01-01')
->addPartition('pmax', 'MAXVALUE')
->create();
}
}
```

### LIST Partitioning

LIST partitioning is useful when you want to partition by discrete values. For
MySQL, use `TYPE_LIST` with integer columns and `TYPE_LIST_COLUMNS` for STRING
columns:

```php
<?php
use Migrations\BaseMigration;
use Migrations\Db\Table\Partition;

class CreatePartitionedCustomers extends BaseMigration
{
public function change(): void
{
$table = $this->table('customers', [
'id' => false,
'primary_key' => ['id', 'region'],
]);
$table->addColumn('id', 'integer', ['identity' => true])
->addColumn('region', 'string', ['limit' => 20])
->addColumn('name', 'string')
->partitionBy(Partition::TYPE_LIST_COLUMNS, 'region')
->addPartition('p_americas', ['US', 'CA', 'MX', 'BR'])
->addPartition('p_europe', ['UK', 'DE', 'FR', 'IT'])
->addPartition('p_asia', ['JP', 'CN', 'IN', 'KR'])
->create();
}
}
```

### HASH Partitioning

HASH partitioning distributes data evenly across a specified number of
partitions:

```php
<?php
use Migrations\BaseMigration;
use Migrations\Db\Table\Partition;

class CreatePartitionedSessions extends BaseMigration
{
public function change(): void
{
$table = $this->table('sessions');
$table->addColumn('user_id', 'integer')
->addColumn('data', 'text')
->partitionBy(Partition::TYPE_HASH, 'user_id', ['count' => 8])
->create();
}
}
```

- `RANGE`
- `RANGE COLUMNS`
- `LIST`
- `LIST COLUMNS`
- `HASH`
- `KEY` *(MySQL only)*
### KEY Partitioning (MySQL only)

You can also partition by expressions using `Literal::from(...)`, and add or
drop partitions on existing tables.
KEY partitioning is similar to HASH but uses MySQL's internal hashing function:

```php
<?php
use Migrations\BaseMigration;
use Migrations\Db\Table\Partition;

class CreatePartitionedCache extends BaseMigration
{
public function change(): void
{
$table = $this->table('cache', [
'id' => false,
'primary_key' => ['cache_key'],
]);
$table->addColumn('cache_key', 'string', ['limit' => 255])
->addColumn('value', 'binary')
->partitionBy(Partition::TYPE_KEY, 'cache_key', ['count' => 16])
->create();
}
}
```

### Partitioning with Expressions

You can partition by expressions using the `Literal` class:

```php
<?php
use Migrations\BaseMigration;
use Migrations\Db\Literal;
use Migrations\Db\Table\Partition;

class CreatePartitionedEvents extends BaseMigration
{
public function change(): void
{
$table = $this->table('events', [
'id' => false,
'primary_key' => ['id', 'created_at'],
]);
$table->addColumn('id', 'integer', ['identity' => true])
->addColumn('created_at', 'datetime')
->partitionBy(Partition::TYPE_RANGE, Literal::from('YEAR(created_at)'))
->addPartition('p2022', 2023)
->addPartition('p2023', 2024)
->addPartition('pmax', 'MAXVALUE')
->create();
}
}
```

### Modifying Partitions on Existing Tables

You can add or drop partitions on existing partitioned tables:

```php
<?php
use Migrations\BaseMigration;

class ModifyOrdersPartitions extends BaseMigration
{
public function up(): void
{
$this->table('orders')
->addPartitionToExisting('p2025', '2026-01-01')
->update();
}

public function down(): void
{
$this->table('orders')
->dropPartition('p2025')
->update();
}
}
```

## Saving Changes

Expand Down
Loading