Content Migrations in Craft

What are Content Migrations?

Let's learn what content migrations are and when we can use them in Craft CMS.

What are Con­tent Migrations?

Con­tent migra­tions in Craft CMS are data migra­tions that are project-spe­cif­ic. You cre­ate them to be run on an instance of Craft CMS and to do some­thing like add con­tent, cre­ate users, sec­tions, fields, and more. 

Con­tent migra­tions are dif­fer­ent from Craft’s app migra­tions, which allow Craft to run data­base changes dur­ing updates and upgrade to the appli­ca­tion. Con­tent migra­tions are also dif­fer­ent from plu­g­in migra­tions, which is how the plu­g­ins you have installed alter your data­base dur­ing instal­la­tion and removal and change their data­base tables dur­ing an update.

Think about con­tent migra­tions as migra­tions that you ful­ly con­trol as part of your project. All of them are based on Yii’s migra­tions, but Craft has three dif­fer­ent types, so dif­fer­ent use cas­es can be met.

What are some exam­ples of what I’d use con­tent migra­tions for?

  1. Add a default set of users to every project (admin, con­tent admin, edi­tor, etc.)
  2. Adding a core set of field groups and fields that you use on every project (although project con­fig can han­dle that now).
  3. Any cus­tom, in-house set­up you’d like to have for projects.

What We’re Building

For this course we’re going to learn how to use Con­tent Migra­tions while build­ing a set of migra­tions to set up a con­tact form in our Craft installation. 

Our con­tact form will con­sist of:

  • a chan­nel sec­tion in Craft that will store the sub­mis­sions as entries
  • a field group
  • fields for the form that will allow users to enter their data and sub­mit it

When we are done we will have a sin­gle migra­tion file that will bun­dle up the entire con­tact form func­tion­al­i­ty (out­side of the front-end tem­plate imple­men­ta­tion) so it’s easy to install in a Craft project.

Let’s get start­ed by build­ing our first migra­tion file.

Content Migrations in Craft is made up of the following videos: