The Kentico continuous integration solution handles serialization of data of most objects from the database into XML files on the file system. However, not all database changes are supported by it. Changes to database indexes, functions, stored procedures, custom views, and so on are all unsupported by continuous integration. To keep the main database consistent across your development instances, these changes need to be handled manually. We recommend using migration scripts to synchronize database changes not managed by the Kentico continuous integration solution.
A migration script allows you to alter a database by changing all or part of a database. This alteration can be as simple as adding or removing a column to a table, or a complex refactoring task such as splitting tables or changing column properties in a way that could affect the data it stores.
The rest of this page assumes you are using a development environment utilizing the Kentico continuous integration solution. See Setting up continuous integration for more information.
The Kentico continuous integration solution supports the use of migration scripts. The ~\CMS\App_Data\CIRepository folder contains, in addition to serialized database object data, the following files:
Additionally, the database of a Kentico instance contains a CI_Migration table, which stores the file names of migrations already executed on the database.
When you need to create or modify objects not supported by continuous integration (for example table indexes, functions, or stored procedures) and you want to share these changes with your team members or commit them to a source control system (for example Team Foundation Server (opens in a new tab) or Git (opens in a new tab)), you need to:
Write a migration script that applies the required change to the database. Refer to the Example - Creating migration scripts to synchronize database changes section for an example of the process.
Reference the migration script in either the Before.txt or After.txt file.
If you are not sure when a migration should be executed, see the Deciding when to execute a migration section for specific scenarios and examples.
Execute the migration script alongside the continuous integration repository restore on databases you want to keep synchronized.
You can download a sample PowerShell script automating the process here: RunRestore.ps1.
If you use the RunRestore.ps1 script, the database restore process consists of these steps:
The targeted project's database now contains the CIRepository data and modifications applied via migration scripts.
After creating a migration script, you need to decide whether it is going to be applied before or after the continuous integration files are restored.
The Before.txt file should only contain migrations that manipulate the database before the continuous integration files are restored. These migrations can, for example:
The After.txt file should only contain migrations that manipulate the restored database. These migrations can, for example:
Important: If you have multiple migrations, carefully consider the order in which they should be executed. The migration scripts in both the Before.txt and After.txt files are executed from the first to the last line.
If you need to create a new index on, for example, a column OfficeNamein a custom module class table CompanyOfficesand want this change to be reflected in the repository, you need to:
Create a new migration script (for example, AddOfficeNameIndex.sql):
CREATE INDEX IX_CompanyOffices_OfficeName ON CompanyOffices(OfficeName);Add the migration script to the @migrations folder.
Add the migration name to a new line in the After.txtfile (the index should only be created after the database is updated).
...
AddOfficeNameIndexCommit your changes to your source control repository.
When the repository containing your changes is restored on another database, the AddOfficeNameIndex.sql migration is automatically applied (creating the IX_CompanyOffices_OfficeName index) during the restore process (as part of the migrations executed after the CIRepository folder is restored). The migration is then added to the CI_Migration table, marked as applied, and will not be executed again on the target database.
To update your local solution and database to the latest version:
This restores the CIRepository folder to the targeted database, executing all unapplied migrations specified in the Before.txt and After.txt files.
When rolling back your changes, it is important to realize that migrations you have created and committed to source control may have already been applied to other databases. Therefore, removing your migrations from the @migrations folder and Before.txt or After.txt is not enough. You also need to a write a new migration that returns the structure of the database where your previous migration was already applied to their original state.
The rollback process is demonstrated here by extending the example above. To revert the changes made by the AddOfficeNameIndex.sql migration, you need to:
Delete the migration scriptAddOfficeNameIndex.sqlfrom the @migrations folder.
Remove the migration name AddOfficeNameIndex from the list of migrations in the After.txt file.
Create a new migration script (for example, RemoveOfficeNameIndex.sql) that removes the index (if it exists):
IF EXISTS(SELECT * FROM sys.indexes WHERE name = 'IX_CompanyOffices_OfficeName' AND object_id = OBJECT_ID('CompanyOffices'))
BEGIN
DROP INDEX IX_CompanyOffices_OfficeName ON CompanyOffices
ENDAdd the migration script to the*@migrations* folder.
Add the migration name to a new line in the Before.txt file (the index should be dropped before its table is deleted during the restore process):
...
RemoveOfficeNameIndexCommit your changes to your source control repository.
When the repository containing your changes is restored on another database, the RemoveOfficeNameIndex.sql migration is automatically applied (removing the IX_CompanyOffices_OfficeName index, if it exists) during the restore process (as part of the migrations executed before the CIRepository folder is restored). The migration is then added to the CI_Migration table, marked as applied, and will not be executed again on the target database.
If you need to revert your database to a specific backup, we recommend restoring the backup to a clean database to avoid possible structural database conflicts which may arise when restoring the database to an older version.
To perform a restore on a clean database:
After the restore finishes, you can continue working with the restored database backup.