-
Notifications
You must be signed in to change notification settings - Fork 82
Documented try_catch (and sql: execute) in migrations #3060
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
base: 5.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| - | ||
| type: sql | ||
| mode: execute | ||
| query: | ||
| - | ||
| driver: mysql | ||
| sql: 'INSERT INTO test_table (test_value) VALUES ("foo");' | ||
| - | ||
| driver: sqlite | ||
| sql: 'INSERT INTO test_table (test_value) VALUES ("foo");' | ||
| - | ||
| driver: postgresql | ||
| sql: "INSERT INTO test_table (test_value) VALUES ('foo');" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| - | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tested locally: |
||
| type: try_catch | ||
| mode: execute | ||
| allowed_exceptions: | ||
| - Throwable | ||
| stop_after_first_exception: true | ||
| steps: | ||
| - | ||
| type: language | ||
| mode: create | ||
| metadata: | ||
| languageCode: ger-DE | ||
| name: German | ||
| enabled: true | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,6 +74,14 @@ | |
| | `user` | ✔ | ✔ | | | | | ||
| | `user_group` | ✔ | ✔ | ✔ | | | | ||
|
|
||
| Additionally, the following special migration types are available: | ||
|
|
||
| | `type` | `execute` | | ||
| |------------------------|:---------:| | ||
| | `repeatable` | ✔ | | ||
| | `sql` | ✔ | | ||
| | `try_catch` | ✔ | | ||
|
|
||
| ### Repeatable steps | ||
|
|
||
| You can run a set of one or more similar migration steps multiple times by using the special `repeatable` migration type. | ||
|
|
@@ -122,6 +130,58 @@ | |
|
|
||
| This step generates field values with fake personal names. | ||
|
|
||
| ### SQL migrations | ||
|
Check notice on line 133 in docs/content_management/data_migration/importing_data.md
|
||
|
|
||
| You can execute raw SQL queries directly in migrations by using the `sql` migration type. | ||
| Use it for custom database operations that don't fit into standard entity migrations, such as creating custom tables or performing bulk updates. | ||
|
|
||
| Each query requires a `driver` property that specifies which database system the query is for. | ||
| The migration system automatically filters queries and executes only those matching your current database driver. | ||
|
|
||
| ```yaml | ||
| [[= include_file('code_samples/data_migration/examples/sql_execute.yaml') =]] | ||
| ``` | ||
|
|
||
| The supported database drivers are: | ||
|
|
||
| - `mysql` - MySQL/MariaDB | ||
| - `postgresql` - PostgreSQL | ||
| - `sqlite` - SQLite | ||
|
|
||
| You can define queries for multiple database drivers in a single migration step. | ||
| The system executes only the queries that match your configured database platform. | ||
| If no matching queries are found, the migration throws an error. | ||
|
Check notice on line 153 in docs/content_management/data_migration/importing_data.md
|
||
|
|
||
| !!! caution | ||
|
|
||
| SQL migrations bypass the content model abstraction layer and directly modify the database. | ||
| Use them with caution and ensure your queries are compatible with your target database system. | ||
|
|
||
| ### Error handling with try-catch | ||
|
|
||
| You can wrap one or more migration steps with a `try_catch` step to handle exceptions gracefully. | ||
|
|
||
| Use it for migrations that may fail under specific conditions but should not halt the entire migration process. | ||
|
|
||
| For example, you can ensure a language creation migration succeeds even if the language already exists. | ||
| If the migration fails for this reason, the exception is suppressed, allowing the remaining migrations to proceed without interruption. | ||
|
Check notice on line 167 in docs/content_management/data_migration/importing_data.md
|
||
|
|
||
| A `try_catch` migration requires the `steps` property and accepts optional `allowed_exceptions` and `stop_after_first_exception` settings. | ||
|
|
||
| Default values are: | ||
|
|
||
| - `allowed_exceptions`: empty list | ||
| - `stop_after_first_exception`: `true` | ||
|
|
||
| ```yaml | ||
| [[= include_file('code_samples/data_migration/examples/try_catch_step.yaml') =]] | ||
| ``` | ||
|
|
||
| When an exception is thrown within a try-catch step, it's compared against the list of `allowed_exceptions`. | ||
|
Check notice on line 180 in docs/content_management/data_migration/importing_data.md
|
||
| If the exception matches, it's caught and the migration continues or stops depending on the `stop_after_first_exception` configuration setting. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In both cases migration is treated as if it succeeded. Non-matching exceptions throw immediately just like before.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Added in af4288e |
||
| The migration is marked as successful. | ||
|
Check notice on line 182 in docs/content_management/data_migration/importing_data.md
|
||
| Non-matching exceptions throw immediately, halting the migration process and returning an error. | ||
|
|
||
| ### Expression syntax | ||
|
|
||
| You can use [Symfony expression syntax]([[= symfony_doc =]]/reference/formats/expression_language.html) in data migrations, like in [repeatable steps](#repeatable-steps), where you can use it to generate varied content in migration steps. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stolen from https://github.com/ibexa/migrations/blob/main/tests/bundle/Command/MigrateCommand/migrate-command-fixtures/sql-execute.yaml