Database Migrations

Custom database tables in WordPress are usually a hassle of checking if it exists already. Apart from that it’s not very database agnostic. Cuisine tries to fix this by, basically, implementing Laravel’s native migration system.


Getting started

Creating database migrations with the power of Cuisine is quite easy; you just create a class that extends

\Cuisine\Database\Migrations\Migration; And use WP Cli to run the command:

wp cuisine migrate That’s it! See our examples for more:


Creating and dropping custom tables

This actually looks exactly the same as Laravel’s database migrations, with the exception of having to extend a different class and create an instance of that class at the bottom.

namespace MyPlugin\Database;

use Cuisine\Wrappers\Schema;
use Cuisine\Database\Blueprint;
use Cuisine\Database\Migrations\Migration;

class ProductMetaMigration extends Migration{


    /**
    * Put the table up
    *
    * @return void
    */
    public function up()
    {
        Schema::create( 'product_meta', function( Blueprint $table ){
            $table->increments( 'id' )->unique();
            $table->integer( 'product_id' )->unsigned();
            $table->string( 'image_url' )->nullable();
            $table->integer( 'position' );
            $table->float( 'price' );
            $table->integer( 'stock' );
        });         
    }

    /**
    * Default down function
    *
    * @return void
    */
    public function down()
    {
        Schema::drop( 'product_meta' );
    }

}


\MyPlugin\Database\ProductMetaMigration::getInstance();

In this example a table named product_meta is created or destroyed, depending on which direction the migration is running.


Creating columns

You can create a lot of different column types with Cuisine. Here’s a list:

Command Description
$table->bigInteger(‘votes’); BIGINT equivalent for the database.
$table->binary(‘data’); BLOB equivalent for the database.
$table->boolean(‘confirmed’); BOOLEAN equivalent for the database.
$table->char(‘name’, 4); CHAR equivalent with a length.
$table->date(‘created_at’); DATE equivalent for the database.
$table->dateTime(‘created_at’); DATETIME equivalent for the database.
$table->decimal(‘amount’, 5, 2); DECIMAL equivalent with a precision and scale.
$table->double(‘column’, 15, 8); DOUBLE equivalent with precision, 15 digits in total and 8 after the decimal point.
$table->float(‘amount’, 8, 2); FLOAT equivalent for the database, 8 digits in total and 2 after the decimal point.
$table->increments(‘id’); } Incrementing ID (primary key) using a “UNSIGNED INTEGER” equivalent.
$table->integer(‘votes’); INTEGER equivalent for the database.
$table->longText(‘description’); LONGTEXT equivalent for the database.
$table->mediumInteger(‘numbers’); MEDIUMINT equivalent for the database.
$table->mediumText(‘description’); MEDIUMTEXT equivalent for the database.
$table->smallInteger(‘votes’); SMALLINT equivalent for the database.
$table->string(‘email’); VARCHAR equivalent column.
$table->string(‘name’, 100); VARCHAR equivalent with a length.
$table->text(‘description’); TEXT equivalent for the database.
$table->time(‘sunrise’); TIME equivalent for the database.
$table->tinyInteger(‘numbers’); TINYINT equivalent for the database.
$table->timestamp(‘added_on’); TIMESTAMP equivalent for the database.