CakePHP Migrationプラグイン触ってみた

参考になる(なりそうな)サイト

本家

https://github.com/CakeDC/migrations 

CakePHPにMigrations Pluginを導入する

http://mawatari.jp/archives/the-introduction-of-the-cakephp-migrations-plugin

ビデオチュートリアル?

http://blip.tv/simonmales/migrations-plugin-for-cakephp-6214599

 

MySQL公式ページから練習用のダミーデータを拝借

http://dev.mysql.com/doc/index-other.html

 

DB作成

mysql > create database sakila character set utf8;

テーブル作成

mysql -u root -p --default-character-set=utf8 sakila < ~/Desktop/sakila-db/sakila-schema.sql

データ投入

mysql -u root -p --default-character-set=utf8 sakila < ~/Desktop/sakila-db/sakila-data.sql

 

cakeのappフォルダに移動して

./Console/cake Migrations.migration generate -f

-f が必要っぽそう。

 

READMEの以下の記述ですね。

If you want import all tables regardless if it has a model or not you can use -f (force) parameter while running the command:

Modelにクラスがあろうがなかろうが、すべてのテーブルを対象にしたい場合は-fを使ってね、と。 

ここいらの話は以下のエントリが詳しい

http://d.hatena.ne.jp/hiromi2424/20111220/1324387254

 

Cake Migration Shell

---------------------------------------------------------------

Strict Error: Only variables should be assigned by reference in [/usr/local/Cellar/httpd/2.2.22/share/apache2/htdocs/workspace/lab/experiment/test/play/cakephp-test/app/Plugin/Migrations/Lib/CakeMigration.php, line 510]

 

Strict Error: Only variables should be assigned by reference in [/usr/local/Cellar/httpd/2.2.22/share/apache2/htdocs/workspace/lab/experiment/test/play/cakephp-test/app/Plugin/Migrations/Lib/CakeMigration.php, line 510]

 

Strict Error: Only variables should be assigned by reference in [/usr/local/Cellar/httpd/2.2.22/share/apache2/htdocs/workspace/lab/experiment/test/play/cakephp-test/app/Plugin/Migrations/Lib/CakeMigration.php, line 510]

 

Do you want generate a dump from current database? (y/n) 

[y] > 

---------------------------------------------------------------

Generating dump from current database...

Notice Error: Schema generation error: invalid column type enum('G','PG','PG-13','R','NC-17') for Film.rating does not exist in DBO in [/usr/local/Cellar/httpd/2.2.22/share/apache2/htdocs/workspace/lab/experiment/test/play/cakephp-test/lib/Cake/Model/CakeSchema.php, line 606]

 

Notice Error: Schema generation error: invalid column type enum('G','PG','PG-13','R','NC-17') for FilmList.rating does not exist in DBO in [/usr/local/Cellar/httpd/2.2.22/share/apache2/htdocs/workspace/lab/experiment/test/play/cakephp-test/lib/Cake/Model/CakeSchema.php, line 606]

 

Notice Error: Schema generation error: invalid column type enum('G','PG','PG-13','R','NC-17') for NicerButSlowerFilmList.rating does not exist in DBO in [/usr/local/Cellar/httpd/2.2.22/share/apache2/htdocs/workspace/lab/experiment/test/play/cakephp-test/lib/Cake/Model/CakeSchema.php, line 606]

 

Do you want to preview the file before generation? (y/n) 

[y] > n

Please enter the descriptive name of the migration to generate:  

> initial

Generating Migration...

 

Done.

 
strict errorはでるね。

 

次にスキーマファイルを作成

./Console/cake schema generate -f 

 

今後、バージョン管理にはスキーマファイルを見て差分を確認するらしい。

この後、ふつーに進めてったら

Error: SQL Error: SQLSTATE[42000]: Syntax error or access violation: 1170 BLOB/TEXT column 'description' used in key specification without a key length

のエラーが出て、積んだ。

schema.php

'idx_title_description' => array('column' => array('title', 'description'), 'unique' => 0)

の行を削除してうまくいく。

 

 

バージョン管理を擬似体験するため、適当なテーブルのカラムを追加。

そのあとで再度、

./Console/cake Migrations.migration generate -f

-fをつけないでやるとデータベースは復元されなかったので今回もつける方向で。

 

* ./Console/cake Migrations.migration generate -f

 

Cake Migration Shell

---------------------------------------------------------------

Do you want compare the schema.php file to the database? (y/n) 

[y] > y

---------------------------------------------------------------

Comparing schema.php to the database...

Notice Error: Schema generation error: invalid column type enum('G','PG','PG-13','R','NC-17') for Film.rating does not exist in DBO in [/usr/local/Cellar/httpd/2.2.22/share/apache2/htdocs/workspace/lab/experiment/test/play/cakephp-test/lib/Cake/Model/CakeSchema.php, line 606]

 

Notice Error: Schema generation error: invalid column type enum('G','PG','PG-13','R','NC-17') for FilmList.rating does not exist in DBO in [/usr/local/Cellar/httpd/2.2.22/share/apache2/htdocs/workspace/lab/experiment/test/play/cakephp-test/lib/Cake/Model/CakeSchema.php, line 606]

 

Notice Error: Schema generation error: invalid column type enum('G','PG','PG-13','R','NC-17') for NicerButSlowerFilmList.rating does not exist in DBO in [/usr/local/Cellar/httpd/2.2.22/share/apache2/htdocs/workspace/lab/experiment/test/play/cakephp-test/lib/Cake/Model/CakeSchema.php, line 606]

 

Do you want to preview the file before generation? (y/n) 

[y] > n

Please enter the descriptive name of the migration to generate:  

> add experimental column 

Generating Migration...

 

Done.

Do you want update the schema.php file? (y/n) 

[y] > y

 

Welcome to CakePHP v2.2.2 Console

---------------------------------------------------------------

App : app

Path: /Users/**/htdocs/workspace/lab/experiment/test/play/cakephp-test/app/

---------------------------------------------------------------

Cake Schema Shell

---------------------------------------------------------------

Generating Schema...

Schema file exists.

 [O]verwrite

 [S]napshot

 [Q]uit

Would you like to do? (o/s/q) 

[s] > s

Notice Error: Schema generation error: invalid column type enum('G','PG','PG-13','R','NC-17') for Film.rating does not exist in DBO in [/usr/local/Cellar/httpd/2.2.22/share/apache2/htdocs/workspace/lab/experiment/test/play/cakephp-test/lib/Cake/Model/CakeSchema.php, line 606]

 

Notice Error: Schema generation error: invalid column type enum('G','PG','PG-13','R','NC-17') for FilmList.rating does not exist in DBO in [/usr/local/Cellar/httpd/2.2.22/share/apache2/htdocs/workspace/lab/experiment/test/play/cakephp-test/lib/Cake/Model/CakeSchema.php, line 606]

 

Notice Error: Schema generation error: invalid column type enum('G','PG','PG-13','R','NC-17') for NicerButSlowerFilmList.rating does not exist in DBO in [/usr/local/Cellar/httpd/2.2.22/share/apache2/htdocs/workspace/lab/experiment/test/play/cakephp-test/lib/Cake/Model/CakeSchema.php, line 606]

 

Schema file: schema_1.php generated

 

 

schema_1.phpというものをスナップショットという機能で作成したけど、Migrationsプラグインの機能ではたぶん使われないと思う。

常にschema.phpを見ていくのだと思う。もし上書きの機能を選ばないほうがいい気がするんだよな、どうなんだろう。

 

マイグレーションやら復元するときは

./Console/cake Migrations.migration run all

なんか失敗するときは

./Console/cake Migrations.migration run reset

を試すといいかも

 

所感としてはプロシージャとかスキーママイグレーション機構では保存してないし、ビューテーブルも普通のテーブルで復元されたし、上記の使い方だけではだめだな。

 

わかり次第共有します。