Schema migrations are JSON files gradually describing project schema changes - e.g. new entities or ACL updates.
Creating a diff using
After you update your schema, you need to create a migration for your change, otherwise Contember won't see it.
There is a command to rescue you:
npm run contember migrations:diff <migration name>
Example: creating a diff
npm run contember migrations:diff add-categories
Name of a migration can only contain alphanumeric letters and a dash
Contember will show you individual migration steps and ask you for confirmation.
You should check the steps with caution, because Contember cannot detect some changes correctly and it may result in a loss of your data. For example when you rename a field it drops the field and creates a new one.
If you have chosen to execute migration, you are done for now. If you haven't, you can check created
.json file and modify migration file manually describing the change more precisely.
Explaining a migration using
You can again verify individual migration steps using
migrations:describe. You can use
--no-sql to alter output of the command.
Example: explaining migrations steps
npm run contember migrations:describe
Executing migrations using
If you've pulled new migrations from upstream, or you want to execute a migration, you've created, you can apply all pending migrations using
Example: executing migrations
npm run contember migrations:execute
All the changes will be applied to both Contember schema and PostgreSQL database.
To execute migrations, you need appropriate permissions.
Contember includes constraints to prevent database inconsistencies. Namely:
- you can't change content of executed migration
- you can't execute a migration, which precedes already executed migration
Therefore, you should:
- never modify or delete a migration, which has been executed on live environment,
- ensure, that new migration is always last (e.g. when merging a branch).
Commands for development
During local development, there may be a need to bypass certain checks, even if the migration has already been executed locally. This section details several commands that provide flexibility and control over your local migration process.
Please be aware that these commands are available exclusively on your local Contember instance and are not meant for production environments.
Amending a Migration
While developing a new feature, you might find yourself needing to adjust an already created and applied schema migration. Instead of creating an entirely new diff, you can utilize the
migrations:amend command. This command allows you to update the most recent migration both on disk and in your local Contember instance. If you revert the schema changes and run
migrations:amend, the command effectively removes the migration.
Example: Amending Latest Migration
npm run contember migrations:amend
Example: Amending Specific Migration
You can target a specific migration to amend by providing an additional argument, as shown below:
npm run contember migrations:amend 2022-01-17-101806-test
If the migration has already been run by someone else or it's been deployed, it won't be possible to execute the amended migration.
Rebasing a Migration with
Before merging a branch with a new migration, you might find that a new migration has been added upstream. The
migrations:rebase command assists in resolving this by renaming the migrations both on disk and in your local Contember instance. Simply pass the names of the migrations you need to merge.
npm run contember migrations:rebase 2022-01-17-101806-test
Force Execution of Out-of-Order Migrations
When you pull code from upstream, there may be a new migration that precedes your local migrations. To bypass this, you can run the
migrations:execute command with the
Example: Force Executing
npm run contember migrations:execute --force
Engine 1.3+ Executing Migrations Until a Specific Point with
In your development process, you might need to run a series of migrations up to a certain point. The
migrations:execute command now allows you to use the
--until flag for this purpose. This option executes all migrations up to and including the specified migration.
Example: Executing Until a Specific Migration
npm run contember migrations:execute --until 2022-01-17-101806-test
Writing or fixing migrations manually
Typically, you won't need to write migrations from scratch, but there may be occasions when you need to fine-tune or rectify a generated migration. When you open a generated
.json migration file, you'll find a list of "modifications" that describe the changes made to your database schema. In such cases, you can manually adjust these modifications to tailor your migrations to specific requirements. Below are the available manual adjustments you can make to migrations.
In Contember, migrations can be manually adjusted or fixed when needed. When working with migrations, you may encounter two modifications,
updateColumnDefinition, which now support the
copyValue features. These options allow you to provide values during the migration process for added columns or modified columns that have been changed to disallow null values.
createColumn modification and
createColumn modification enables the addition of a new column to an entity. When creating a column that does not allow null values, you can utilize the following options:
fillValue: Specifies a value that will be used to fill the column during the migration run. This value is distinct from the default value used at runtime. If a new column with a default value is added, the default value will also be used as the fillValue in the generated JSON migration.
copyValue: Indicates the name of another column from which the value will be copied to the newly created column.
updateColumnDefinition modification and
updateColumnDefinition modification allows you to modify the definition of an existing column within an entity. When changing a column to disallow null values, you can make use of the following options:
fillValue: Specifies a value that will be used to fill the column during the migration run. This option proves useful in populating the modified column with meaningful data when the nullability constraint is enforced.
copyValue: Indicates the name of another column from which the value will be copied to the modified column.
In this example, the value from an existing column named "existingColumn" will be copied to the modified column "isPublished" during the migration run.
In Contember, renaming an entity involves creating a migration that drops the old entity and creates a new one. However, with the
updateEntityName modification, you can instruct Contember to simply rename an existing entity without recreating it.
- entityName: The current name of the entity.
- newEntityName: The desired new name for the entity.
- tableName: You can optionally also change the name of the database table.
In this example, the entity named "OldEntity" will be renamed to "NewEntity" using the
updateEntityName modification. Also, the table in database will be renamed to
Similar to the
updateEntityName modification, the
updateFieldName modification allows you to rename a field within an entity.
- entityName: The name of the entity containing the field.
- fieldName: The current name of the field.
- newFieldName: The desired new name for the field.
- columnName: You can optionally change the name of the field in a database.
In this example, the field named "OldField" within the entity "Entity" will be renamed to "NewField" using the
Engine 1.2+ Skipping validation errors
skippedErrors feature in Contember allows users to specify a list of errors that should be ignored during validation of a migration. This can be useful in cases where a migration became invalid due to improvements and new checks in validator, but cannot be changed, because it is already applied.
To skip errors, open a migration file producing errors and add
skippedErrors field. It is an array of objects, each of which contains a code and a path field. The code field specifies the error code, and the path field specifies the path to the element in the migration that caused the error. Path field is optional.
It is important to note that only individual migrations can have skipped errors, and the final migrated state must be valid. This means that any errors that are skipped in one migration must be fixed in a later migration in order for the migration process to be successful.
// Modifications here...
In this example, the
ACL_INVALID_CONDITION error will be ignored for the test predicate in the ContentReference entity for the reader role.
In each error object, you can specify a
skipUntil allowing to skip given validation until a specificed migration. This feature is useful when more migrations becomes invalid due to changes in the validator or data structure.
// Modifications here...
In the above example, the "ACL_INVALID_CONDITION" error is ignored for a specific predicate in the ContentReference entity for the reader role. Additionally, subsequent validations will be skipped until the migration