cakephp3のmigrations
migrations
テーブルの設定などを行うらしい
まずは準備
新規インストール(すでにある場合は無視)
curl -s https://getcomposer.org/installer | php php composer.phar create-project --prefer-dist -s dev cakephp/app
app/composer.json
{ "name": "cakephp/app", "description": "CakePHP skeleton app", "homepage": "http://cakephp.org", "type": "project", "license": "MIT", "require": { "php": ">=5.4.16", "cakephp/cakephp": "3.0.*-dev", "mobiledetect/mobiledetectlib": "2.*", "cakephp/debug_kit": "3.0.*-dev". "cakephp/migrations": "dev-master" }, "require-dev": { "d11wtq/boris": "1.0.*" }, "suggest": { "phpunit/phpunit": "Allows automated tests to be run without system-wide install.", "cakephp/cakephp-codesniffer": "Allows to check the code against the coding standards used in CakePHP." }, "autoload": { "psr-4": { "App\\": "src" } }, "autoload-dev": { "psr-4": { "App\\Test\\": "tests", "Cake\\Test\\Fixture\\": "./vendor/cakephp/cakephp/tests/Fixture" } }, "scripts": { "post-install-cmd": "App\\Console\\Installer::postInstall" }, "config" : { "bin-dir" : "bin" }, "minimum-stability" : "dev", "prefer-stable": true }
※"cakephp/migrations"を追加
update
cd app php ../composer.phar update cakephp/migrations
確認
$ sh app/bin/cake migrations Welcome to CakePHP v3.0.0-beta2 Console --------------------------------------------------------------- App : src Path: /vagrant/projects/beta2/app/src/ --------------------------------------------------------------- Migrations plugin, based on Phinx by Rob Morgan. version 0.3.5 Usage: [options] command [arguments] Options: --help -h Display this help message. --quiet -q Do not output any message. --verbose -v|vv|vvv Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug --version -V Display this application version. --ansi Force ANSI output. --no-ansi Disable ANSI output. --no-interaction -n Do not ask any interactive question. Available commands: create Create a new migration help Displays help for a command list Lists commands migrate Migrate the database rollback Rollback the last or to a specific migration status Show migration status $
※動くかもしれない…
初期化
sh app/bin/cake migrations create Initial
↓
$ sh app/bin/cake migrations create Initial Welcome to CakePHP v3.0.0-beta2 Console --------------------------------------------------------------- App : src Path: /vagrant/projects/beta2/app/src/ --------------------------------------------------------------- using migration path /vagrant/projects/beta2/app/config/Migrations created ./app/config/Migrations/20141007152249_initial.php $
app/config/Migrations/20141007152249_initial.php
<?php use Phinx\Migration\AbstractMigration; class Initial extends AbstractMigration { /** * Change Method. * * More information on this method is available here: * http://docs.phinx.org/en/latest/migrations.html#the-change-method * * Uncomment this method if you would like to use it. * public function change() { } */ /** * Migrate Up. */ public function up() { } /** * Migrate Down. */ public function down() { } }
※初期データなのでこれだけ…
up時のメソッドを追加
<?php use Phinx\Migration\AbstractMigration; class Initial extends AbstractMigration { /** * Change Method. * * More information on this method is available here: * http://docs.phinx.org/en/latest/migrations.html#the-change-method * * Uncomment this method if you would like to use it. * public function change() { } */ /** * Migrate Up. */ public function up() { $_table = "sample1"; if($this->hasTable($_table)){ $this->dropTable("sample1"); } $sample1 = $this->table("sample1" , array( "id" => false, "primary_key" => array("id") ) ); $sample1 ->addColumn("id", "integer") ->addColumn("name", "string", array("limit" => 50)) ->addColumn("created", "datetime") ->addColumn("updated", "datetime") ->create(); } /** * Migrate Down. */ public function down() { } }
実行してみる
$ sh app/bin/cake migrations migrate Welcome to CakePHP v3.0.0-beta2 Console --------------------------------------------------------------- App : src Path: /vagrant/projects/beta2/app/src/ --------------------------------------------------------------- using migration path /vagrant/projects/beta2/app/config/Migrations using environment default using adapter mysql using database my_app All Done. Took 0.0147s $
できたもの
管理テーブルとして
phinxlog
というテーブルで更新情報を管理しているよう
所感
データベースの定義もソースで管理できるようになった。
SQLなどを別途設計書を用意するよりは多少楽かもしれない。
まだ、いろいろと制約があるか確認することはありそうですけど…
再作成などは便利になりそう。