Migrations


Overview

WP Bones provides a simple implementation of the migration. Of course, this implementation is a little bit different from Laravel migration. First of all, you can use php bones migrate:create to create a migration file:

php bones migrate:create MyTable

You’ll see a new file in /database/migrations/ folder, resembling /database/migrations/2016_01_28_131206_create_mytable_table.php. This file contains your class migration:

use WPKirk\WPBones\Database\Migrations\Migration;
 
return new class extends Migration
{
 
  public function up()
  {
    // Create your table
  }
 
}

For example, if you want a products table, please use:

php bones migrate:create Products

Edit the migration file like this:

use WPKirk\WPBones\Database\Migrations\Migration;
 
return new class extends Migration {
 
  public function up()
  {
 
    $this->create('products', "(
              log_id bigint(20) unsigned NOT NULL auto_increment,
              user_id bigint(20) unsigned NOT NULL default '0',
              activity varchar(20) NOT NULL default 'updated',
              object_id bigint(20) unsigned NOT NULL default '0',
              object_type varchar(20) NOT NULL default 'post',
              activity_date datetime NOT NULL default '0000-00-00 00:00:00',
              PRIMARY KEY  (log_id),
              KEY user_id (user_id)
         ) {$this->charsetCollate};" );
 
  }
};

Remember that the migrations files will be executed when a plugin is activated. You’ll see in the database a table named wp_products.

WordPress prefix

As you know, the real name of the table products is {prefix}_products. Usually, {prefix} is wp_ for default. Anyway, you don’t need to use the WordPress prefix, WP Bones will do that for you.

Remove the WordPress prefix

Starting from version 1.7.0, you can remove the WordPress prefix. You may use the usePrefix property in the migration class:

use WPKirk\WPBones\Database\Migrations\Migration;
 
return new class extends Migration {
 
  protected $usePrefix = false;
 
  public function up()
  {
 
    $this->create('products', "(
              log_id bigint(20) unsigned NOT NULL auto_increment,
              user_id bigint(20) unsigned NOT NULL default '0',
              activity varchar(20) NOT NULL default 'updated',
              object_id bigint(20) unsigned NOT NULL default '0',
              object_type varchar(20) NOT NULL default 'post',
              activity_date datetime NOT NULL default '0000-00-00 00:00:00',
              PRIMARY KEY  (log_id),
              KEY user_id (user_id)
         ) {$this->charsetCollate};" );
 
  }
};

In this case the table will be named products.