Migrations

In general migrations in PyroCMS work just like migrations in Laravel. Except now you can leverage using the streams, fields, and assignments repositories to scaffold your database automatically.

Generating Migrations

You can generate migrations just like you normally would using make:migration with the addition of a few options.

**Pro Tip:** Migrations in the **database/migrations** directory are ran last during system installation.
Field Migrations

You can create fields via repositories but you may prefer using the field migration to simplify things. You can create a field migration by including the --addon option and --fields option flag.

php artisan make:migration create_more_fields --addon=example.module.test --fields

Example

<?php

use Anomaly\Streams\Platform\Database\Migration\Migration;

class AnomalyModuleRedirectsCreateRedirectsFields extends Migration
{

    protected $fields = [
        'from'   => 'anomaly.field_type.text',
        'to'     => 'anomaly.field_type.text',
        'status' => [
            'type'   => 'anomaly.field_type.select',
            'config' => [
                'default_value' => '301',
                'options'       => [
                    '301' => 'anomaly.module.redirects::field.status.option.301',
                    '302' => 'anomaly.module.redirects::field.status.option.302',
                ],
            ],
        ],
        'secure' => 'anomaly.field_type.boolean',
    ];

}
Defining the namespace

When creating field you must provide a namespace to create the fields in.

By default the slug of the addon the migration resides in will be used as the namespace.

You can also define the $namespace property if the addon slug does not suffice:

protected $namespace = 'example';

Lastly you can define the namespace inline with the stream and and assignment definitions:

protected $stream = [
    'slug'         => 'widgets',
    'namespaces'   => 'example',
    'title_column' => 'name',
    'translatable' => true,
    'trashable'    => true,
];

protected $assignments = [
    'name'        => [
        'namespace'    => 'example',
        'required'     => true,
        'translatable' => true,
    ],
];
**Pro Tip:** Being able to set the namespace like this means you can create streams and assign fields for any namespace from anywhere without modifying core code.
Stream Migrations

You can create streams via repositories but you may prefer using the stream migration to simplify things. You can create a stream migration by including the --addon and --stream=stream_slug options.

php artisan make:migration create_example_stream --addon=example.module.test --stream=widgets

Example

<?php

use Anomaly\Streams\Platform\Database\Migration\Migration;

class AnomalyModuleUsersCreateRolesStream extends Migration
{

    protected $stream = [
        'slug'         => 'roles',
        'title_column' => 'name',
        'translatable' => true,
        'trashable'    => true,
    ];

    protected $assignments = [
        'name'        => [
            'required'     => true,
            'translatable' => true,
        ],
        'slug'        => [
            'required' => true,
            'unique'   => true,
        ],
        'description' => [
            'translatable' => true,
        ],
        'permissions',
    ];

}
Defining the namespace

Similar as to when creating fields, when creating streams and the stream assignments you must provide a namespace to create them in.

By default the slug of the addon the migration resides in will be used as the namespace.

You can also define the $namespace property if the addon slug does not suffice:

protected $namespace = 'example';

Lastly you can define the namespace inline with the field definitions:

'status' => [
    'type'      => 'anomaly.field_type.select',
    'namespace' => 'example_namespace',
    'config'    => [
        'default_value' => '301',
        'options'       => [
            '301' => 'anomaly.module.redirects::field.status.option.301',
            '302' => 'anomaly.module.redirects::field.status.option.302',
        ],
    ],
],
**Pro Tip:** Being able to set the namespace like this means you can create fields for any namespace from anywhere without modifying core code.
Default Migrations

You can also create "normal" migrations by omitting the --fields and --stream options. This will create a migration very similar to Laravel migrations with the exception that the fields, streams, and assignments repositories are all available to use.

Example

<?php

use Anomaly\Streams\Platform\Database\Migration\Migration;

class AnomalyModulePagesMakePagesSearchable extends Migration
{

    public function up()
    {
        $stream = $this->streams()->findBySlugAndNamespace('pages', 'pages');

        $stream
            ->setAttribute('searchable', true)
            ->save();

        $field = $this->fields()
            ->create(
                [
                    'slug'      => 'content',
                    'namespace' => 'example',
                    'type'      => 'anomaly.field_type.wysiwyg',
                ]
            );

        $this->assignments()
            ->create(
                [
                    'field'    => $field,
                    'stream'   => $stream,
                    'required' => true,
                ]
            );
    }

    public function down()
    {
        $stream = $this->streams()->findBySlugAndNamespace('pages', 'pages');

        $stream
            ->setAttribute('searchable', false)
            ->save();

        if ($field = $this->fields()->findBySlugAndNamespace('content', 'example')) {
            $field->delete(); // Will clean up abandoned assignments.
        }
    }
}
Appending Streams

Often times you will need to append streams information by adding fields, assignments, or updating streams. You can use the properties show in the examples above to add fields and/or assignments and update streams.

Example

<?php

use Anomaly\Streams\Platform\Database\Migration\Migration;

class AnomalyModuleUsersAddExampleField extends Migration
{

	/**
     * Prevent deleting on rollback.
     */
	protected $delete = false;

	/**
     * Any additional information will
	 * be updated. Slug helps find
     * the stream to work with.
     */
    protected $stream = [
        'slug' => 'roles',
    ];

    /**
     * This field will be added.
     */
	protected $fields = [
        'example' => 'anomaly.field_type.boolean',
    ];

    /**
     * These assignments will be
	 * created for the stream above.
     */
    protected $assignments = [
        'example'        => [
            'required'     => true,
            'translatable' => true,
        ],
    ];

}