Skip to main content
Version: 5.x

Database Migrations

Introduction​

About migrations

Migrations are a way to version your database, which often evolves alongside changes to your application.

Migrations are stored in the migrations folder.

Adding a migration​

To add a migration, use php bow with the add:migration command followed by the name of the migration (e.g. create_todos_table). Bow will create a file of the same name prefixed with a creation date.

php bow add:migration create_todos_table

The --table and --create options can also be used to specify the table name and indicate whether the migration will create a new table:

# Create a new table
php bow add:migration create_todos_table --create=todos

# Modify an existing table
php bow add:migration add_status_to_todos_table --table=todos

Migration structure​

A migration class contains two methods: up and rollback. The up method is used to add new tables, columns, or indexes to your database, while the rollback method should reverse the operations performed by the up method.

use Bow\Database\Migration\Migration;
use Bow\Database\Migration\Table;

class Version20190929153939CreateTodosTable extends Migration
{
/**
* Run the migration
*/
public function up(): void
{
$this->create("todos", function (Table $table) {
$table->addIncrement('id');
$table->addString('title');
$table->addInteger('status', ['default' => 1]);
$table->addTimestamps();
});
}

/**
* Reverse the migration
*/
public function rollback(): void
{
$this->dropIfExists("todos");
}
}

Running migrations​

To run all your pending migrations:

php bow migration:migrate

# Shortcut
php bow migrate

Rolling back migrations​

To roll back the last migration operation:

php bow migration:rollback

To roll back all migrations:

php bow migration:reset

Table management​

Creating a table​

$this->create("users", function (Table $table) {
$table->addIncrement('id');
$table->addString('name');
$table->addString('email', ['unique' => true]);
$table->addTimestamps();
});

Creating a table if it does not exist​

$this->createIfNotExists("users", function (Table $table) {
$table->addIncrement('id');
$table->addString('name');
$table->addTimestamps();
});

Modifying a table​

$this->alter('users', function (Table $table) {
$table->addString('phone', ['nullable' => true]);
});

Renaming a table​

$this->renameTable('old_table', 'new_table');

// Rename if it exists
$this->renameTableIfExists('old_table', 'new_table');

Dropping a table​

$this->drop('users');

// Drop if it exists
$this->dropIfExists('users');

Custom connection​

$this->connection('mysql_secondary')->create("logs", function (Table $table) {
$table->addIncrement('id');
$table->addText('message');
$table->addTimestamps();
});

Table options​

$this->create("users", function (Table $table) {
$table->withEngine('InnoDB');
$table->withCharset('utf8mb4');
$table->withCollation('utf8mb4_unicode_ci');

$table->addIncrement('id');
$table->addString('name');
});

Raw SQL​

// Run raw SQL inside the migration
$this->sql('ALTER TABLE users ADD INDEX idx_email (email)');

// Alternative
$this->addSql('CREATE INDEX idx_name ON users (name)')