|
| 1 | +# Using a custom migration template |
| 2 | + |
| 3 | +The `migrate:create` command from `yiisoft/db-migration` generates migration classes from PHP view templates. Use a |
| 4 | +custom template when your project wants a standard header, strict class shape, comments, helper calls, or team-specific |
| 5 | +placeholders in every new migration. |
| 6 | + |
| 7 | +This recipe assumes migrations are already configured as described in the |
| 8 | +[Database migrations](../guide/databases/db-migrations.md) guide. |
| 9 | + |
| 10 | +## Create a template |
| 11 | + |
| 12 | +Create `resources/migration-templates/migration.php`: |
| 13 | + |
| 14 | +```php |
| 15 | +<?php |
| 16 | + |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +/** |
| 20 | + * This view is used by {@see Yiisoft\Db\Migration\Command\CreateCommand}. |
| 21 | + * |
| 22 | + * @var \Yiisoft\Db\Migration\Service\Generate\PhpRenderer $this |
| 23 | + * @var string $className The new migration class name without namespace. |
| 24 | + * @var string $namespace The new migration class namespace. |
| 25 | + */ |
| 26 | + |
| 27 | +echo "<?php\n"; |
| 28 | +echo "\ndeclare(strict_types=1);\n"; |
| 29 | + |
| 30 | +if (!empty($namespace)) { |
| 31 | + echo "\nnamespace {$namespace};\n"; |
| 32 | +} |
| 33 | +?> |
| 34 | + |
| 35 | +use Yiisoft\Db\Migration\MigrationBuilder; |
| 36 | +use Yiisoft\Db\Migration\RevertibleMigrationInterface; |
| 37 | + |
| 38 | +final class <?= $className ?> implements RevertibleMigrationInterface |
| 39 | +{ |
| 40 | + public function up(MigrationBuilder $b): void |
| 41 | + { |
| 42 | + // Add forward migration code. |
| 43 | + } |
| 44 | + |
| 45 | + public function down(MigrationBuilder $b): void |
| 46 | + { |
| 47 | + // Add rollback migration code. |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +The template is a PHP file that prints the generated migration. For the default `create` command, the most useful |
| 53 | +variables are `$className` and `$namespace`. |
| 54 | + |
| 55 | +## Configure the generator |
| 56 | + |
| 57 | +Create `config/common/di/migration-generator.php`: |
| 58 | + |
| 59 | +```php |
| 60 | +<?php |
| 61 | + |
| 62 | +declare(strict_types=1); |
| 63 | + |
| 64 | +use Yiisoft\Db\Migration\Service\Generate\CreateService; |
| 65 | + |
| 66 | +return [ |
| 67 | + CreateService::class => [ |
| 68 | + 'setTemplate()' => [ |
| 69 | + 'create', |
| 70 | + dirname(__DIR__, 3) . '/resources/migration-templates/migration.php', |
| 71 | + ], |
| 72 | + ], |
| 73 | +]; |
| 74 | +``` |
| 75 | + |
| 76 | +The `create` key changes the template used by: |
| 77 | + |
| 78 | +```shell |
| 79 | +./yii migrate:create audit_log |
| 80 | +``` |
| 81 | + |
| 82 | +The command asks for confirmation and then writes a migration using your template. |
| 83 | + |
| 84 | +If the new DI file is not picked up, rebuild the config merge plan: |
| 85 | + |
| 86 | +```shell |
| 87 | +composer yii-config-rebuild |
| 88 | +``` |
| 89 | + |
| 90 | +## Configure multiple templates |
| 91 | + |
| 92 | +`yiisoft/db-migration` supports these template keys: |
| 93 | + |
| 94 | +- `create` |
| 95 | +- `table` |
| 96 | +- `dropTable` |
| 97 | +- `addColumn` |
| 98 | +- `dropColumn` |
| 99 | +- `junction` |
| 100 | + |
| 101 | +To replace multiple templates at once, use `setTemplates()`: |
| 102 | + |
| 103 | +```php |
| 104 | +use Yiisoft\Db\Migration\Service\Generate\CreateService; |
| 105 | + |
| 106 | +return [ |
| 107 | + CreateService::class => [ |
| 108 | + 'setTemplates()' => [[ |
| 109 | + 'create' => dirname(__DIR__, 3) . '/resources/migration-templates/migration.php', |
| 110 | + 'table' => dirname(__DIR__, 3) . '/resources/migration-templates/create-table.php', |
| 111 | + 'dropTable' => dirname(__DIR__, 3) . '/resources/migration-templates/drop-table.php', |
| 112 | + 'addColumn' => dirname(__DIR__, 3) . '/resources/migration-templates/add-column.php', |
| 113 | + 'dropColumn' => dirname(__DIR__, 3) . '/resources/migration-templates/drop-column.php', |
| 114 | + 'junction' => dirname(__DIR__, 3) . '/resources/migration-templates/create-junction.php', |
| 115 | + ]], |
| 116 | + ], |
| 117 | +]; |
| 118 | +``` |
| 119 | + |
| 120 | +For table-oriented templates, copy the corresponding file from |
| 121 | +`vendor/yiisoft/db-migration/resources/views/` and adjust it. Those templates receive additional variables such as |
| 122 | +`$table`, `$columns`, `$foreignKeys`, and `$tableComment`. |
| 123 | + |
| 124 | +## Keep templates maintainable |
| 125 | + |
| 126 | +Keep generated migrations independent from current application services. A migration may run years later, after service |
| 127 | +APIs have changed. Put stable schema operations in the generated class and leave business logic in console commands or |
| 128 | +application services. |
| 129 | + |
| 130 | +Do not edit templates under `vendor/`. Copy them into your project and configure `CreateService` to use the copies. |
0 commit comments