Database Migrations
Introductionβ
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)')