Repository navigation
Add transaction mode to migration generation #369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aa20af8
Add migration generation transaction mode (#367)
samdark 1886394
Quote null in transaction mode PHPDoc
samdark b9fd33a
Simplify transactional migration documentation
samdark a09ea3a
Fix test dependency and file assertions (#367)
samdark d446022
Merge branch 'master' into issue-367-transaction-mode
samdark e2ae8b8
Merge branch 'master' into issue-367-transaction-mode
samdark 7841bfb
Update CHANGELOG.md
vjik b3229f6
Rename migration option to --transactional
samdark 1944c68
fix changelog
vjik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # Transactional migrations | ||
|
|
||
| A migration that implements `Yiisoft\Db\Migration\TransactionalMigrationInterface` runs in a transaction. | ||
| The migrator uses a separate transaction for each migration's `up()` or `down()` method, including the history update. | ||
|
|
||
| Some database operations commit automatically or cannot run in a transaction. | ||
| The interface does not change these limits. | ||
|
|
||
| ## Create a migration | ||
|
|
||
| To generate a transactional migration, use `--transactional`: | ||
|
|
||
| ```shell | ||
| ./yii migrate:create update_posts --transactional | ||
| ``` | ||
|
|
||
| To generate a migration without a transaction, use `--no-transactional`: | ||
|
|
||
| ```shell | ||
| ./yii migrate:create post --command=table --no-transactional | ||
| ``` | ||
|
|
||
| Both options work with all generator commands: | ||
| `create`, `table`, `dropTable`, `addColumn`, `dropColumn`, and `junction`. | ||
| For Symfony, replace `./yii` with `php bin/console`. For the standalone tool, use `./vendor/bin/yii-db-migration`. | ||
|
|
||
| ## Default transaction mode | ||
|
|
||
| Set `newMigrationTransactional` to choose the default mode for new migrations: | ||
|
|
||
| | Value | Behavior | | ||
| | --- | --- | | ||
| | `true` | Generate transactional migrations. | | ||
| | `false` | Generate migrations without transactions. | | ||
| | `null` | Use the template's default. | | ||
|
|
||
| By default, the built-in `table` and `junction` templates generate transactional migrations. | ||
| The other built-in templates generate migrations without transactions. | ||
|
|
||
| CLI options override `newMigrationTransactional`. | ||
| If no option is set and the setting is `null`, the generator uses the template's default. | ||
|
|
||
| For Yii Console, set the mode for the default database in `config/console/params.php`: | ||
|
|
||
| ```php | ||
| 'yiisoft/db-migration' => [ | ||
| // ... | ||
| 'newMigrationTransactional' => true, | ||
| ], | ||
| ``` | ||
|
|
||
| For the standalone tool, set `'newMigrationTransactional' => true` in the project's root `yii-db-migration.php` file. | ||
|
|
||
| For Symfony, configure the `CreateService` constructor argument in `config/services.yml`: | ||
|
|
||
| ```yaml | ||
| services: | ||
| Yiisoft\Db\Migration\Service\Generate\CreateService: | ||
| arguments: | ||
| $transactional: true | ||
| ``` | ||
|
|
||
| For direct PHP use, pass `transactional: true` to the `CreateService` constructor. | ||
| Pass the `transactional` argument to `CreateService::run()` to override the mode for one call. | ||
|
|
||
| ## Named databases | ||
|
|
||
| Each named database has its own `newMigrationTransactional` setting. It does not inherit the default database's setting. | ||
|
|
||
| For Yii Console, add the setting to the database's entry in `databases`. | ||
| For standalone or Symfony usage, pass it to the `DatabaseConfig` constructor: | ||
|
|
||
| ```php | ||
| new DatabaseConfig( | ||
| $mapsConnection, | ||
| newMigrationPath: __DIR__ . '/migrations/maps', | ||
| newMigrationTransactional: true, | ||
| ); | ||
| ``` | ||
|
|
||
| The CLI option overrides the setting for the selected database: | ||
|
|
||
| ```shell | ||
| ./yii migrate:create update_places --db=maps --no-transactional | ||
| ``` | ||
|
|
||
| See [Multiple databases](multiple-databases.md) to configure the connections. | ||
|
|
||
| ## Change an existing migration | ||
|
|
||
| Generation settings do not change existing migrations. | ||
|
|
||
| To enable transactions, add `TransactionalMigrationInterface` to the migration's `implements` list. | ||
| To disable transactions, remove it. Keep `RevertibleMigrationInterface` if the migration supports `down()`. | ||
|
|
||
| ## Custom templates | ||
|
|
||
| Templates set with `CreateService::setTemplate()` or `CreateService::setTemplates()` receive `$transactional`. | ||
| This variable contains the mode for the current call, or the configured mode if no override is set. | ||
| If the value is `null`, choose a template default, for example with `$transactional ??= false`. | ||
|
|
||
| Include the import and implementation of `TransactionalMigrationInterface` only when `$transactional` is `true`. | ||
| Templates that ignore this variable keep their existing behavior. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.