1. Home
  2. PHP
  3. CakePHP
  4. CakePHP3のMigration(マイグレーション)でカラム追加・変更・削除を行う

CakePHP3のMigration(マイグレーション)でカラム追加・変更・削除を行う

  • 公開日
  • 更新日
  • カテゴリ:CakePHP
  • タグ:PHP,migration,Beginner,CakePHP
CakePHP3のMigration(マイグレーション)でカラム追加・変更・削除を行う

前回の記事 では、マイグレーションでデータベース(テーブル)の構築(作成)を行いました。

今回は、マイグレーションにて作成済みのテーブルへのカラム追加、およびカラムの変更・削除を行います。

[公式]Migrations
https://book.cakephp.org/3.0/ja/migrations.html

Contents

  1. 開発環境
  2. マイグレーションでテーブルを構築する
    1. マイグレーションファイルの作成
    2. マイグレーション実行
  3. マイグレーションでカラムを追加する
    1. マイグレーションファイルの作成
    2. マイグレーション実行
  4. マイグレーションでカラムを変更する
    1. マイグレーションファイルの作成
    2. マイグレーション実行
  5. マイグレーションでカラムを削除する
    1. マイグレーションファイルの作成
    2. マイグレーション実行

開発環境

  • linux CentOS 7
  • Apache 2.4
  • PHP 7.1
  • MySQL 5.7
  • CakePHP 3.5

CakePHP のルートディレクトリを「cakephp/」とします。

マイグレーションでテーブルを構築する

まずは検証用のテーブルをマイグレーションで構築します。

マイグレーションファイルの作成

まずは簡単に、id と name カラムのみのテーブルを作成する為のマイグレーションファイルを生成します。

# CakePHP のルートディレクトリへ移動する
cd /path/to/cakephp

# bake コマンドで samples テーブル構築のマイグレーションファイルを生成する
bin/cake bake migration CreateSamples name:string

# 実行結果
[demo@localhost cakephp]# bin/cake bake migration CreateSamples name:string

Creating file /var/www/html/cakephp/config/Migrations/20180121014718_CreateSamples.php
Wrote `/var/www/html/cakephp/config/Migrations/20180121014718_CreateSamples.php`

XXXX_CreateSamples.php が cakephp/config/Migrations 配下に生成されます。

cakephp
├─ config
│   ├─ Migrations
│   │   ├─ 20180121014718_CreateSamples.php
cakephp/config/Migrations/XXXX_CreateSamples.php
<?php
use Migrations\AbstractMigration;

class CreateSamples extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-change-method
     * @return void
     */
    public function change()
    {
        $table = $this->table('samples');
        $table->addColumn('name', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => false,
        ]);
        $table->create();
    }
}

マイグレーション実行

マイグレーションを実行し、samples テーブルを構築します。

cakephp ルートディレクトリへ移動し、以下の bake コマンドを叩きます。

# CakePHP のルートディレクトリへ移動する
cd /path/to/cakephp

# マイグレーションを実行する
bin/cake migrations migrate

# 実行結果
[demo@localhost cakephp]# bin/cake migrations migrate
using migration paths 
 - /var/www/html/cakephp/config/Migrations
using seed paths 
 - /var/www/html/cakephp/config/Seeds
using environment default
using adapter mysql
using database cakephp

 == 20180121014718 CreateSamples: migrating
 == 20180121014718 CreateSamples: migrated 0.3168s

All Done. Took 0.3186s
using migration paths 
 - /var/www/html/cakephp/config/Migrations
using seed paths 
 - /var/www/html/cakephp/config/Seeds
Writing dump file `/var/www/html/cakephp/config/Migrations/schema-dump-default.lock`...
Dump file `/var/www/html/cakephp/config/Migrations/schema-dump-default.lock` was successfully written

MySQL へログインし、テーブルが作成されているか確認します。

mysql> tables;
+-------------------+
| Tables_in_cakephp |
+-------------------+
| phinxlog          |
| samples           |
+------------------+

mysql> columns from samples;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

samples テーブルが作成され、id と name のみのテーブルが構築されました。

マイグレーションでカラムを追加する

ここから、マイグレーションでカラム操作を行っていきます。まずはカラム追加からです。

マイグレーションファイルの作成

最初にマイグレーションファイルを生成しますが、一定の書式があり、それは以下の通りです。

bin/cake bake migration AddAgeToSamples age:integer?[3]

クラス名を「Add[カラム名]To[テーブル名]」とし、その後に追加カラム情報を記述する事で、カラム追加のマイグレーションファイルが生成されます。

AddAgeToSamplesage カラムを samples テーブルに追加する。 age:integer?[3]age カラムは int 型で、NUll を許容し、最大文字長は 3 文字とする。それではこのコマンドを叩きマイグレーションファイルを生成します。 cakephp ルートディレクトリへ移動し、以下の bake コマンドを叩きます。

# CakePHP のルートディレクトリへ移動する
cd /path/to/cakephp

# bake コマンドで age カラムを samples テーブルに追加するマイグレーションファイルを生成する
bin/cake bake migration AddAgeToSamples age:integer?[3]

# 実行結果
[demo@localhost cakephp]# bin/cake bake migration AddAgeToSamples age:integer?[3]

Creating file /var/www/html/cakephp/config/Migrations/20180121021350_AddAgeToSamples.php
Wrote `/var/www/html/cakephp/config/Migrations/20180121021350_AddAgeToSamples.php`

XXXX_AddAgeToSamples.php が cakephp/config/Migrations 配下に生成されます。

cakephp
├─ config
│   ├─ Migrations
│   │   ├─ 20180121014718_CreateSamples.php
│   │   ├─ 20180121021350_AddAgeToSamples.php
│   │   └─ schema-dump-default.lock
cakephp/config/Migrations/XXXX_AddAgeToSamples.php
<?php
use Migrations\AbstractMigration;

class AddAgeToSamples extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-change-method
     * @return void
     */
    public function change()
    {
        $table = $this->table('samples');
        $table->addColumn('age', 'integer', [
            'default' => null,
            'limit' => 3,
            'null' => true,
        ]);
        $table->update();
    }
}

samples テーブルに age カラムを addColumn() するクラスが生成されたことが確認できます。

しかしながらここで一点問題があります。自動生成されたメソッドの場合、change() メソッドで実装されており、このままではロールバック時にコケてロールバックできなくなるので、up()/down() メソッドに実装し直します。

<?php
use Migrations\AbstractMigration;

class AddAgeToSamples extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-change-method
     * @return void
     */
    public function up()
    {
        $table = $this->table('samples');
        $table->addColumn('age', 'integer', [
            'default' => null,
            'limit' => 3,
            'null' => true,
        ]);
        $table->update();
    }

    public function down()
    {
      $table = $this->table('samples');
      $table->removeColumn('age');
      $table->update();
    }
}

up() メソッドはマイグレーション時に実行されるメソッドで、down() メソッドはロールバック時に実行されるメソッドです。この場合では、マイグレーション時には age カラムの追加を行いますが、down() メソッドでは age カラムを削除します。

マイグレーション実行

マイグレーションを実行し、samples テーブルに add カラムを追加します。

cakephp ルートディレクトリへ移動し、以下の bake コマンドを叩きます。

# CakePHP のルートディレクトリへ移動する
cd /path/to/cakephp

# マイグレーションを実行する
bin/cake migrations migrate

# 実行結果
[demo@localhost cakephp]# bin/cake migrations migrate
using migration paths 
 - /var/www/html/cakephp/config/Migrations
using seed paths 
 - /var/www/html/cakephp/config/Seeds
using environment default
using adapter mysql
using database cakephp

 == 20180121021350 AddAgeToSamples: migrating
 == 20180121021350 AddAgeToSamples: migrated 0.2027s

All Done. Took 0.2473s
using migration paths 
 - /var/www/html/cakephp/config/Migrations
using seed paths 
 - /var/www/html/cakephp/config/Seeds
Writing dump file `/var/www/html/cakephp/config/Migrations/schema-dump-default.lock`...
Dump file `/var/www/html/cakephp/config/Migrations/schema-dump-default.lock` was successfully written

MySQL へログインし、samples テーブルに age カラムが追加されているか確認します。

mysql> show columns from samples;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | NO   |     | NULL    |                |
| age   | int(3)       | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

age カラムが指定した型や最大文字長などで作成されている事が確認できました。

マイグレーションでカラムを変更する

次に、カラム変更を行う際の一連の流れを紹介します。今回は、先ほど追加した age カラムの最大文字長を3文字から2文字に変更します。

マイグレーションファイルの作成

bin/cake bake migration ChangeAgeToSamples

今回はファイル名だけで OK です。追加した時と同じような命名規則にしておくとわかりやすいですね。

それではこのコマンドを叩きマイグレーションファイルを生成します。 cakephp ルートディレクトリへ移動し、以下の bake コマンドを叩きます。

# CakePHP のルートディレクトリへ移動する
cd /path/to/cakephp

# bake コマンドでマイグレーションファイルを生成する
bin/cake bake migration ChangeAgeToSamples

# 実行結果
[demo@localhost cakephp]# bin/cake bake migration ChangeAgeToSamples

Creating file /var/www/html/cakephp/config/Migrations/20180121024247_ChangeAgeToSamples.php
Wrote `/var/www/html/cakephp/config/Migrations/20180121024247_ChangeAgeToSamples.php`
cakephp/config/Migrations/XXXX_ChangeAgeToSamples.php
<?php
use Migrations\AbstractMigration;

class ChangeAgeToSamples extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-change-method
     * @return void
     */
    public function change()
    {
    }
}

ここに、カラム変更の記述を行っていきます。

<?php
use Migrations\AbstractMigration;

class ChangeAgeToSamples extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-change-method
     * @return void
     */
    public function change()
    {
      $table = $this->table('samples');
      $table->changeColumn('age', 'integer', [
        'limit' => 2,
      ]);
      $table->update();
    }
}

changeColumn() メソッドを用いているところがポイントです。

そしてここも先ほどと同様、chage() メソッドを up()/down() メソッドに変更します。

<?php
use Migrations\AbstractMigration;

class ChangeAgeToSamples extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-change-method
     * @return void
     */
    public function up()
    {
      $table = $this->table('samples');
      $table->changeColumn('age', 'integer', [
        'limit' => 2,
      ]);
      $table->update();
    }

    public function down()
    {
      $table = $this->table('samples');
      $table->changeColumn('age', 'integer', [
        'limit' => 3,
      ]);
      $table->update();
    }
}

マイグレーション実行

それではマイグレーションを実行し、add カラムの最大文字長を変更します。

cakephp ルートディレクトリへ移動し、以下の bake コマンドを叩きます。

# CakePHP のルートディレクトリへ移動する
cd /path/to/cakephp

# マイグレーションを実行する
bin/cake migrations migrate

# 実行結果
[demo@localhost cakephp]# bin/cake migrations migrate
using migration paths 
 - /var/www/html/cakephp/config/Migrations
using seed paths 
 - /var/www/html/cakephp/config/Seeds
using environment default
using adapter mysql
using database cakephp

 == 20180121024247 ChangeAgeToSamples: migrating
 == 20180121024247 ChangeAgeToSamples: migrated 0.0608s

All Done. Took 0.0631s
using migration paths 
 - /var/www/html/cakephp/config/Migrations
using seed paths 
 - /var/www/html/cakephp/config/Seeds
Writing dump file `/var/www/html/cakephp/config/Migrations/schema-dump-default.lock`...
Dump file `/var/www/html/cakephp/config/Migrations/schema-dump-default.lock` was successfully written

MySQL へログインし、age カラムが変更されているか確認します。

mysql> show columns from samples;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | NO   |     | NULL    |                |
| age   | int(2)       | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

正常にカラム変更が行われている事が確認できました。

マイグレーションでカラムを削除する

最後に、カラムの削除をマイグレーションで行います。

マイグレーションファイルの作成

削除を行うマイグレーションファイルの作成コマンドに関しても、一定の書式があります。

bin/cake bake migration RemoveAgeFromSamples age

クラス名を「Remove[カラム名]To[テーブル名]」とし、その後に削除対象のカラム名を記述する事で、カラム削除のマイグレーションファイルが生成されます。

RemoveAgeFromSamplessamples テーブルの age カラムを削除する age 削除対象のカラム名それではこのコマンドを叩きマイグレーションファイルを生成します。 cakephp ルートディレクトリへ移動し、以下の bake コマンドを叩きます。

# CakePHP のルートディレクトリへ移動する
cd /path/to/cakephp

# bake コマンドでマイグレーションファイルを生成する
bin/cake bake migration RemoveAgeFromSamples age

# 実行結果
[demo@localhost cakephp]# bin/cake bake migration RemoveAgeFromSamples age

Creating file /var/www/html/cakephp/config/Migrations/20180121030108_RemoveAgeFromSamples.php
Wrote `/var/www/html/cakephp/config/Migrations/20180121030108_RemoveAgeFromSamples.php`

XXXX_RemoveAgeFromSamples.php が cakephp/config/Migrations 配下に生成されます。

cakephp
├─ config
│   ├─ Migrations
│   │   ├─ 20180121014718_CreateSamples.php
│   │   ├─ 20180121021350_AddAgeToSamples.php
│   │   ├─ 20180121024247_ChangeAgeToSamples.php
│   │   ├─ 20180121030108_RemoveAgeFromSamples.php
│   │   └── schema-dump-default.lock
cakephp/config/Migrations/XXXX_RemoveAgeFromSamples.php
<?php
use Migrations\AbstractMigration;

class RemoveAgeFromSamples extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-change-method
     * @return void
     */
    public function change()
    {
        $table = $this->table('samples');
        $table->removeColumn('age');
        $table->update();
    }
}

そしてここも、up()/down() メソッドに書き換えます。

<?php
use Migrations\AbstractMigration;

class RemoveAgeFromSamples extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-change-method
     * @return void
     */
    public function up()
    {
        $table = $this->table('samples');
        $table->removeColumn('age');
        $table->update();
    }

    public function down()
    {
      $table = $this->table('samples');
      $table->addColumn('age', 'integer', [
        'default' => null,
        'limit' => 2,
        'null' => true,
      ]);
      $table->update();
    }
}

マイグレーション実行

マイグレーションを実行し、add カラムの削除を行います。

cakephp ルートディレクトリへ移動し、以下の bake コマンドを叩きます。

# CakePHP のルートディレクトリへ移動する
cd /path/to/cakephp

# マイグレーションを実行する
bin/cake migrations migrate

# 実行結果
[demo@localhost cakephp]# bin/cake migrations migrate
using migration paths
 - /var/www/html/cakephp/config/Migrations
using seed paths
 - /var/www/html/cakephp/config/Seeds
using environment default
using adapter mysql
using database cakephp

 == 20180121030108 RemoveAgeFromSamples: migrating
 == 20180121030108 RemoveAgeFromSamples: migrated 0.0427s

All Done. Took 0.0455s
using migration paths
 - /var/www/html/cakephp/config/Migrations
using seed paths
 - /var/www/html/cakephp/config/Seeds
Writing dump file `/var/www/html/cakephp/config/Migrations/schema-dump-default.lock`...
Dump file `/var/www/html/cakephp/config/Migrations/schema-dump-default.lock` was successfully written

MySQL へログインし、age カラムが削除されているか確認します。

mysql> show columns from samples;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

正常にカラム削除が行われている事が確認できました。

まとめ

作業は以上となります。
マイグレーションでデータベース管理を行っていくと、テーブルやカラム追加の際に、チーム全体で即座にその内容を把握でき、追加・変更も容易に行えます。

また、いちいち MySQL にログインしなくても CakePHP のソース上でテーブル内容などを確認できるのでそれも結構便利なので、是非試してみてください。

next:CakePHP3 の Seeding(シーディング)で任意の初期データ投入& faker でダミーデータ挿入もしてみる

Author

rito

  • Backend Engineer
  • Tokyo, Japan
  • PHP 5 技術者認定上級試験 認定者
  • 統計検定 3 級