<p>This is a plugin I'm working on to do exactly what you are after. </p>

<p><a href="https://m.youtube.com/watch?v=n6ko2PKUqAo&amp;feature=youtu.be" rel="noreferrer">https://m.youtube.com/watch?v=n6ko2PKUqAo&amp;feature=youtu.be</a></p>

<p>The syntax to figure out how to create sections, elements etc is determined by reading through the craft service and model classes. </p>

CC BY-SA 3.0
2017-07-18T11:35:21.657Z
This looks awesome. How is the progress on the plugin? Is this going to be an open-source kind of thing?
Roel
CC BY-SA 3.0
2017-07-19T12:49:17.987Z
Thanks. Hoping to have a beta release in the next month. It will be open source.
Derrick Grigg
CC BY-SA 3.0
2017-06-25T01:01:58.463Z
2017-06-25T01:01:58.463Z
Derrick Grigg
<blockquote>
  <p>"I'm just having a hard time figuring out what options there are."</p>
</blockquote>

<p>The options are virtually limitless. You can put almost anything you want into a <code>safeUp</code> method.</p>

<p>Craft 3 is still in beta, and probably will be for another six months or so. They are likely to expand on the current docs for migrations, but I wouldn't expect to see that until much closer to the official release.</p>

CC BY-SA 3.0
2017-04-04T12:15:17.993Z
Sorry, i should have been clearer there. I meant the options regarding the creation/modification/removal of fields, sections, entryTypes, globals etc.

Right now i'm using https://github.com/nerds-and-company/schematic to handle this in Craft 2. Renaming and removal is problematic, which should be no problem for migrations.
Roel
CC BY-SA 3.0
2017-04-04T18:12:54.107Z
It's not a fixed set of options... it's more like a blank canvas. You can do literally anything you want in a migration.
Lindsey D
CC BY-SA 3.0
2017-06-19T17:30:05.400Z
I think Roel is asking for an example of the syntax required to create a new section, with a set of custom fields. As in, what would the code look like to achieve this.
SteveEdson
CC BY-SA 3.0
2017-04-03T19:21:36.930Z
2017-04-03T19:21:36.930Z
Lindsey D
<p>I've written up an article with some basic examples of content migrations in Craft 3: <a href="https://medium.com/@mikethehud/craft-cms-3-content-migration-examples-3a377f6420c3" rel="nofollow noreferrer">https://medium.com/@mikethehud/craft-cms-3-content-migration-examples-3a377f6420c3</a></p>

<p>As others have stated I just read through the code base / api docs (<a href="https://docs.craftcms.com/api/v3/" rel="nofollow noreferrer">https://docs.craftcms.com/api/v3/</a>) to find out what methods and services to use to save specific things.</p>

<p>Hope that helps!</p>

CC BY-SA 4.0
2018-07-24T06:03:18.597Z
Thanks, i did see that article yesterday and it really helped! I think i saw it at Craft's twitter.
Roel
CC BY-SA 4.0
2018-07-23T03:28:41.023Z
2018-07-23T03:28:41.023Z
mikethehud
<p>I did some initial attempts, and this is what I cooked up (only the safeUp() method).</p>

<p>It is probably not the best way to do this, but the way I've so far found.</p>

<p>Currently it creates a field group <code>myFieldGroup</code> with a field <code>fieldName</code> of type Plaintext.</p>

<p>As others have said, you can do virtually anything with migrations, all it takes is figuring out how.</p>

<pre><code>public function safeUp()
{

    $fieldGroupName = 'myFieldGroup';
    $fieldService = Craft::$app-&gt;getFields();
    $fieldGroup = new FieldGroup();

    $fieldGroup-&gt;name = $fieldGroupName;

    $fieldGroupSuccess = $fieldService-&gt;saveGroup($fieldGroup);


    if ($fieldGroupSuccess) {
        $fieldGroupId = null;

        // FIXME: Couldn't find a method to search for a field group by name
        $fieldGroups = $fieldService-&gt;getAllGroups();
        foreach ($fieldGroups as $group) {
            if ($group-&gt;name == $fieldGroupName) {
                $fieldGroupId = $group-&gt;id;
            }
        }
        if (!$fieldGroupId) {
            echo "Couldn't find ID for field group, aborting";
            return false;
        }
        $field = $fieldService-&gt;createField([
            'type' =&gt; 'craft\fields\PlainText',
            'groupId' =&gt; $fieldGroupId,
            'name' =&gt; 'fieldName',
            'handle' =&gt; 'fieldNameHandle'
        ]);
    }
    return $fieldService-&gt;saveField($field);
}
</code></pre>

CC BY-SA 3.0
2018-01-09T07:29:21.960Z
You know Craft attaches the id to your `$fieldGroup` variable inside the save method? That said doing `$fieldGroupId = $fieldGroup->id` is totally enough. More important you should check if the `$field` variable actually exists, if someone copies your code and inserts something strange as a field name and the validation fails your `$field` variable is not initialized and it will trow an exception https://github.com/craftcms/cms/blob/develop/src/services/Fields.php#L281
Robin Schambach
CC BY-SA 3.0
2018-01-10T09:08:14.163Z
After some research, i figured out the migrations from the controllers. I still need to do a pull-request to the docs to help others. I did the same thing @user2874204 suggested.
Roel
CC BY-SA 3.0
2018-01-08T22:19:27.160Z
2018-01-08T22:19:27.160Z
Erik Weber
<p>For now (when the docs are not ready) you look into the craft source how things are saved for example:</p>

<p><a href="https://github.com/craftcms/cms/blob/develop/src/controllers/FieldsController.php#L250" rel="noreferrer">https://github.com/craftcms/cms/blob/develop/src/controllers/FieldsController.php#L250</a></p>

<p>Based on the source code I've created the following migration file:</p>

<p><a href="https://gist.github.com/jeroenlammerts/de0471dc210749f4afe8d2e69bab46d8" rel="noreferrer">https://gist.github.com/jeroenlammerts/de0471dc210749f4afe8d2e69bab46d8</a></p>

CC BY-SA 3.0
2017-09-05T08:13:27.077Z
Thanks, that's what i was trying to do for now. It just involves a lot of searching in the code. Seems like this would be a good addition to the docs.
Roel
CC BY-SA 3.0
2018-02-16T16:36:52.450Z
The migration docs are still not updated for craft3. I think https://github.com/pennebaker/craft-architect is a nice plugin to use for migrations, it saves a lot of time coding them yourself. If you want to write custom migrations the source of that plugin can be helpful.
Jeroen
CC BY-SA 3.0
2018-02-17T20:09:22.387Z
For Craft 2 i'm using Schematic, Architect looks similar to that. The major issue with having a complete layout of the structure is that changes are missing. For example, when you rename something, it won't rename on execution. The old field or section wil be removed and a new one created, which results in data loss. Be aware of this. This is why i'm starting to like the migrations written in separate php classes, you can rename things without much of an issue.
Roel
CC BY-SA 3.0
2018-03-17T19:10:23.000Z
Good point to be aware of. I think writing migrations by hand can get compicated with fields like matrix or something like super-table (plugin). I have done lots of sites with Wordpress ACF and love how they keep in sync of the custom fields with the json files. But you also have to migrate the content when renaming fields. Migrations by hand give you more possibilities to do this and you have a better history in git than json files although they cost more developer time.
Jeroen
CC BY-SA 3.0
2017-08-31T20:36:30.087Z
2017-08-31T20:36:30.087Z
Jeroen
<p>One of the big features for craft 3 is content migrations. Migrating fields and sections between environments (like deploying code to test or production).
I'm just having a hard time figuring out what options there are. I did find some simple examples, but they are a little limited...</p>

<p><a href="https://craftcms.com/news/craft-3-content-migrations" rel="nofollow noreferrer">https://craftcms.com/news/craft-3-content-migrations</a>
<a href="https://docs.craftcms.com/v3/content-migrations.html" rel="nofollow noreferrer">https://docs.craftcms.com/v3/content-migrations.html</a></p>

<p>Since there is a separate documentation page which seems incomplete i created an <a href="https://github.com/craftcms/docs/issues/3" rel="nofollow noreferrer">issue</a>. If anyone has some additional info i would like to make a pull-request to complete the docs.</p>

CC BY-SA 4.0
2018-05-18T06:43:37.400Z
See Github for more info: https://github.com/craftcms/docs/issues/3#issuecomment-373990306
Roel
<migration><content><craft3>
Has someone figured out Craft 3 content migrations?
# Has someone figured out Craft 3 content migrations?

(Asked by: username_0 on 2017-04-03)

One of the big features for craft 3 is content migrations. Migrating fields and sections between environments (like deploying code to test or production).
I'm just having a hard time figuring out what options there are. I did find some simple examples, but they are a little limited...

<https://craftcms.com/news/craft-3-content-migrations>
<https://docs.craftcms.com/v3/content-migrations.html>

Since there is a separate documentation page which seems incomplete i created an [issue](https://github.com/craftcms/docs/issues/3). If anyone has some additional info i would like to make a pull-request to complete the docs.

**Tags:** migration, content, craft3

> **Comments:**
> **username_0**: See Github for more info: https://github.com/craftcms/docs/issues/3#issuecomment-373990306  

---

## 5 answers

### Answer by username_7 on 2017-08-31. Score: 5 | Accepted answer

For now (when the docs are not ready) you look into the craft source how things are saved for example:

<https://github.com/craftcms/cms/blob/develop/src/controllers/FieldsController.php#L250>

Based on the source code I've created the following migration file:

<https://gist.github.com/jeroenlammerts/de0471dc210749f4afe8d2e69bab46d8>

> **Comments:**
> **username_0**: Thanks, that's what i was trying to do for now. It just involves a lot of searching in the code. Seems like this would be a good addition to the docs.  
> **username_7**: The migration docs are still not updated for craft3. I think https://github.com/pennebaker/craft-architect is a nice plugin to use for migrations, it saves a lot of time coding them yourself. If you want to write custom migrations the source of that plugin can be helpful.  
> **username_0**: For Craft 2 i'm using Schematic, Architect looks similar to that. The major issue with having a complete layout of the structure is that changes are missing. For example, when you rename something, it won't rename on execution. The old field or section wil be removed and a new one created, which results in data loss. Be aware of this. This is why i'm starting to like the migrations written in separate php classes, you can rename things without much of an issue.  
> **username_7**: Good point to be aware of. I think writing migrations by hand can get compicated with fields like matrix or something like super-table (plugin). I have done lots of sites with Wordpress ACF and love how they keep in sync of the custom fields with the json files. But you also have to migrate the content when renaming fields. Migrations by hand give you more possibilities to do this and you have a better history in git than json files although they cost more developer time.  

---

### Answer by username_1 on 2017-06-25. Score: 5

This is a plugin I'm working on to do exactly what you are after.

<https://m.youtube.com/watch?v=n6ko2PKUqAo&feature=youtu.be>

The syntax to figure out how to create sections, elements etc is determined by reading through the craft service and model classes.

> **Comments:**
> **username_0**: This looks awesome. How is the progress on the plugin? Is this going to be an open-source kind of thing?  
> **username_1**: Thanks. Hoping to have a beta release in the next month. It will be open source.  

---

### Answer by username_2 on 2017-04-03. Score: 4

> "I'm just having a hard time figuring out what options there are."

The options are virtually limitless. You can put almost anything you want into a `safeUp` method.

Craft 3 is still in beta, and probably will be for another six months or so. They are likely to expand on the current docs for migrations, but I wouldn't expect to see that until much closer to the official release.

> **Comments:**
> **username_0**: Sorry, i should have been clearer there. I meant the options regarding the creation/modification/removal of fields, sections, entryTypes, globals etc.  Right now i'm using https://github.com/nerds-and-company/schematic to handle this in Craft 2. Renaming and removal is problematic, which should be no problem for migrations.  
> **username_2**: It's not a fixed set of options... it's more like a blank canvas. You can do literally anything you want in a migration.  
> **username_3**: I think username_0 is asking for an example of the syntax required to create a new section, with a set of custom fields. As in, what would the code look like to achieve this.  

---

### Answer by username_4 on 2018-07-23. Score: 3

I've written up an article with some basic examples of content migrations in Craft 3: <https://medium.com/@username_4/craft-cms-3-content-migration-examples-3a377f6420c3>

As others have stated I just read through the code base / api docs (<https://docs.craftcms.com/api/v3/>) to find out what methods and services to use to save specific things.

Hope that helps!

> **Comments:**
> **username_0**: Thanks, i did see that article yesterday and it really helped! I think i saw it at Craft's twitter.  

---

### Answer by username_5 on 2018-01-08. Score: 2

I did some initial attempts, and this is what I cooked up (only the safeUp() method).
It is probably not the best way to do this, but the way I've so far found.
Currently it creates a field group myFieldGroup with a field fieldName of type Plaintext.
As others have said, you can do virtually anything with migrations, all it takes is figuring out how.
public function safeUp()
{

    $fieldGroupName = 'myFieldGroup';
    $fieldService = Craft::$app->getFields();
    $fieldGroup = new FieldGroup();

    $fieldGroup->name = $fieldGroupName;

    $fieldGroupSuccess = $fieldService->saveGroup($fieldGroup);


    if ($fieldGroupSuccess) {
        $fieldGroupId = null;

        // FIXME: Couldn't find a method to search for a field group by name
        $fieldGroups = $fieldService->getAllGroups();
        foreach ($fieldGroups as $group) {
            if ($group->name == $fieldGroupName) {
                $fieldGroupId = $group->id;
            }
        }
        if (!$fieldGroupId) {
            echo "Couldn't find ID for field group, aborting";
            return false;
        }
        $field = $fieldService->createField([
            'type' => 'craft\fields\PlainText',
            'groupId' => $fieldGroupId,
            'name' => 'fieldName',
            'handle' => 'fieldNameHandle'
        ]);
    }
    return $fieldService->saveField($field);
}

> **Comments:**
> **username_6**: You know Craft attaches the id to your `$fieldGroup` variable inside the save method? That said doing `$fieldGroupId = $fieldGroup->id` is totally enough. More important you should check if the `$field` variable actually exists, if someone copies your code and inserts something strange as a field name and the validation fails your `$field` variable is not initialized and it will trow an exception https://github.com/craftcms/cms/blob/develop/src/services/Fields.php#L281  
> **username_0**: After some research, i figured out the migrations from the controllers. I still need to do a pull-request to the docs to help others. I did the same thing @user2874204 suggested.  

---

