本記事では、cakePHPのMigrationsコマンドを利用した、新規テーブルのスキーマファイルの作成を説明しています。
また、Migrationsコマンドで、既存テーブルのカラムの削除も説明しています。
新規テーブルの作成
Migrationsコマンドで、新規テーブルのスキーマファイルの作成
1 2 |
php bin/cake.php bake migration CreateUsers name:string pass:string status:string description:text logined_at created modified |
実行結果
1 2 3 4 5 6 7 8 9 10 11 12 |
[hogehoge@sv7108 app]$ php bin/cake.php bake migration CreateUsers name:string pass:string status:string description:text logined_at created modified Welcome to CakePHP v3.1.14 Console --------------------------------------------------------------- App : src Path: /home/hogehoge/example.com/public_html/sandbox/app/src/ PHP : 5.4.16 --------------------------------------------------------------- Creating file /home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/20190122234620_CreateUsers.php Wrote `/home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/20190122234620_CreateUsers.php` |
生成したファイルの内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
[hogehoge@sv7108 app]$ cat /home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/20190122234620_CreateUsers.php <?php use Migrations\AbstractMigration; class CreateUsers 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('users'); $table->addColumn('name', 'string', [ 'default' => null, 'limit' => 255, 'null' => false, ]); $table->addColumn('pass', 'string', [ 'default' => null, 'limit' => 255, 'null' => false, ]); $table->addColumn('status', 'string', [ 'default' => null, 'limit' => 255, 'null' => false, ]); $table->addColumn('description', 'text', [ 'default' => null, 'null' => false, ]); $table->addColumn('logined_at', 'datetime', [ 'default' => null, 'null' => false, ]); $table->addColumn('created', 'datetime', [ 'default' => null, 'null' => false, ]); $table->addColumn('modified', 'datetime', [ 'default' => null, 'null' => false, ]); $table->create(); } } |
新規テーブルのスキーマファイルをデータベースに反映
スキーマファイルからデータベースにテーブルを新規作成する。
migratonsコマンドにオプション migrateを指定してテーブルを作成します。
使い方
1 2 |
php bin/cake.php migrations migrate |
コマンド例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[hogehoge@sv7108 app]$ php bin/cake.php migrations migrate Welcome to CakePHP v3.1.14 Console --------------------------------------------------------------- App : src Path: /home/hogehoge/example.com/public_html/sandbox/app/src/ PHP : 5.4.16 --------------------------------------------------------------- using migration path /home/hogehoge/example.com/public_html/sandbox/app/config/Migrations using seed path /home/hogehoge/example.com/public_html/sandbox/app/config/Seeds using environment default using adapter mysql using database hogehoge_cakephp == 20190122234620 CreateUsers: migrating == 20190122234620 CreateUsers: migrated 0.0064s All Done. Took 0.0086s using migration path /home/hogehoge/example.com/public_html/sandbox/app/config/Migrations using seed path /home/hogehoge/example.com/public_html/sandbox/app/config/Seeds Writing dump file `/home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/schema-dump-default.lock`... Dump file `/home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/schema-dump-default.lock` was successfully written |
データベースの確認
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
MySQL [hogehoge_cakephp]> show tables; +-------------------------------+ | Tables_in_hogehoge_cakephp | +-------------------------------+ | phinxlog | | users | +-------------------------------+ 2 rows in set (0.00 sec) MySQL [hogehoge_cakephp]> desc users; +-------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | | NULL | | | pass | varchar(255) | NO | | NULL | | | status | varchar(255) | NO | | NULL | | | description | text | NO | | NULL | | | logined_at | datetime | NO | | NULL | | | created | datetime | NO | | NULL | | | modified | datetime | NO | | NULL | | +-------------+--------------+------+-----+---------+----------------+ 8 rows in set (0.00 sec) |
既存テーブルの変更
migrationsコマンドによるカラムの削除
ここでは、例としてdescriptionカラムを削除するスキーマファイルを作成します。
使い方
1 2 3 |
$bin/cake bake migration RemoveDescriptionFromUsers description Remove[削除するカラム名]From[テーブル名] [カラム名(小文字)] |
コマンド例
ここではUsersテーブルのdescriptionカラムを削除してみます。
1 2 |
php bin/cake.php bake migration RemoveDescriptionFromUsers description |
実行画面
1 2 3 4 5 6 7 8 9 10 11 |
Welcome to CakePHP v3.1.14 Console --------------------------------------------------------------- App : src Path: /home/hogehoge/example.com/public_html/sandbox/app/src/ PHP : 5.4.16 --------------------------------------------------------------- Creating file /home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/20190122235840_RemoveDescriptionFromUsers.php Wrote `/home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/20190122235840_RemoveDescriptionFromUsers.php` [hogehoge@sv7108 app]$ |
カラム削除のスキーマファイルをデータベースに反映
スキーマファイルから、データベース項目の削除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
php bin/cake.php migrations migrate [hogehoge@sv7108 app]$ php bin/cake.php migrations migrate Welcome to CakePHP v3.1.14 Console --------------------------------------------------------------- App : src Path: /home/hogehoge/example.com/public_html/sandbox/app/src/ PHP : 5.4.16 --------------------------------------------------------------- using migration path /home/hogehoge/example.com/public_html/sandbox/app/config/Migrations using seed path /home/hogehoge/example.com/public_html/sandbox/app/config/Seeds using environment default using adapter mysql using database hogehoge_cakephp == 20190122235840 RemoveDescriptionFromUsers: migrating == 20190122235840 RemoveDescriptionFromUsers: migrated 0.0104s All Done. Took 0.0121s using migration path /home/hogehoge/example.com/public_html/sandbox/app/config/Migrations using seed path /home/hogehoge/example.com/public_html/sandbox/app/config/Seeds Writing dump file `/home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/schema-dump-default.lock`... Dump file `/home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/schema-dump-default.lock` was successfully written |
反映後のテーブル確認
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
MySQL [hogehoge_cakephp]> desc users; +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | | NULL | | | pass | varchar(255) | NO | | NULL | | | status | varchar(255) | NO | | NULL | | | logined_at | datetime | NO | | NULL | | | created | datetime | NO | | NULL | | | modified | datetime | NO | | NULL | | +------------+--------------+------+-----+---------+----------------+ 7 rows in set (0.01 sec) |