The symfony and Doctrine book

7章 - マイグレーション

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


About

You are currently reading "The symfony and Doctrine book" which is licensed under the GFDL license.

Master symfony

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Books on symfony

Learn more about symfony with the official guides.
books.sensiolabs.com

L'audit Qualité par SensioLabs

200 points de contrôle de votre applicatif web.
audit.sensiolabs.com

Chapter Content

利用可能なマイグレーションタスク

スキーマとデータフィクスチャの例

マイグレーションを生成する

データベースから

モデルから

スケルトン

symfony training
Be trained by symfony experts
May 29: Paris (Web Development with Symfony2 - Français)
May 31: Paris (Mastering Symfony2 - Français)
Jun 06: Paris (Introduction to Symfony2 - Français)
Jun 06: Paris (Introduction to Symfony2 - English)
Jun 06: Paris (Going Further with Symfony2 - English)
and more...

Search


powered by google
You are currently browsing "The symfony and Doctrine book" in Japanese for the 1.2 version - Switch to language:
This work is licensed under a GFDL license.
This version of symfony is not maintained anymore.
If some of your projects still use this version, consider upgrading as soon as possible.

ウェブ開発での共通の問題はモデルが進化してスキーマが変更されるときにデータベースへの変更を管理する方法です。 Doctrineのマイグレーションサポート機能はこの問題の効率的な解決方法を提供します。

既存のデータベースもしくはモデルの両方からマイグレーションクラスを生成するためにsfDoctrinePluginは追加のタスクを実装します。 マイグレーションクラスの空のスケルトンの生成にもDoctrineを利用できます。

利用可能なマイグレーションタスク

:generate-migration          マイグレーションクラスを生成する (doctrine-generate-migration)
:generate-migrations-db      既存のデータベース接続からマイグレーションクラスを生成する (doctrine-generate-migrations-db, doctrine-gen-migrations-from-db)
:generate-migrations-models  既存のモデルのセットからマイグレーションクラスを生成する (doctrine-generate-migrations-models, doctrine-gen-migrations-from-models)
:migrate                     データベースを現在の/指定されたバージョンにマイグレートする (doctrine-migrate)

この章の例では次のスキーマとデータフィクスチャに取り組むことを想定します。 ここで文章化されたすべての例の基本としてこれを使います。

スキーマとデータフィクスチャの例

project/config/doctrine/schema.yml

BlogPost:
  actAs:
    I18n:
      fields: [title, body]
    Timestampable:
  columns:
    author: string(255)
    title: string(255)
    body: clob
  relations:
    Tags:
      class: Tag
      refClass: BlogPostTag
      foreignAlias: BlogPosts
Tag:
  columns:
    name: string(255)
BlogPostTag:
  columns:
    blog_post_id:
      type: integer
      primary: true
    tag_id:
      type: integer
      primary: true

project/data/fixtures.yml

BlogPost:
  BlogPost_1:
    author: Jonathan H. Wage
    Translation:
      en:
        title: Test Blog Post
        body: This is the body of the test blog post
    Tags: [php, orm]
Tag:
  php:
    name: PHP
  orm:
    name: ORM

マイグレーションを生成する

Doctrineはスキーマを変更するためにコードで満たされる空白のマイグレーションクラスを生成する機能と同様に既存のデータベースもしくは既存のモデル用にマイグレーションクラスのセットを生成する機能も提供します。

データベースから

既存のデータベースがあれば次のコマンドを実行することでデータベースを再生成するマイグレーションクラスのセットをビルドできます。

$ ./symfony doctrine:generate-migrations-db frontend
>> doctrine  Generated migration classes successfully from database

モデルから

既存のモデルのセットがあれば次のコマンドを実行することでデータベースを作成するマイグレーションのセットをビルドできます。

$ ./symfony doctrine:generate-migrations-models frontend
>> doctrine  Generated migration classes successfully from models

マイグレーションクラスが生成された後でデータベースを再作成するにはデータベースをリセットしてmigradeコマンドを実行します。

$ ./symfony doctrine:drop-db frontend && ./symfony doctrine:build-db frontend && ./symfony doctrine:migrate frontend && ./symfony doctrine:data-load frontend

スケルトン

データベースが作成され最新バージョンにマイグレートされたので、モデルの進化にあわせてスキーマをマイグレートするために新しいマイグレーションクラスのスケルトンを生成できます。 excerptという名前の新しいカラムをBlogPostモデルに追加する必要がある場合を考えてみましょう。 下記のコードは更新され新しいカラムを格納するBlogPostスキーマです。

BlogPost:
  actAs:
    I18n:
      fields: [title, body]
    Timestampable:
  columns:
    author: string(255)
    title: string(255)
    body: clob
    excerpt: string(255)
  relations:
    Tags:
      class: Tag
      refClass: BlogPostTag
      foreignAlias: BlogPosts

excerptカラムをデータベースに追加するためには空白のマイグレーションクラスを生成する必要があります。 次のコマンドでマイグレーションクラスが生成されます。

$ ./symfony doctrine:generate-migration frontend AddBlogPostExcerptColumn
>> doctrine  Generated migration class: AddB...ymfony12/lib/migration/doctrine

project/lib/migration/doctrineにおいて 次のコードの内容を持つ006_add_blog_post_excerpt_column.class.phpという名前の新しいファイルが見つかります。

/**
 * This class has been auto-generated by the Doctrine ORM Framework
 */
class AddBlogPostExcerptColumn extends Doctrine_Migration
{
    public function up()
    {
 
    }
 
    public function down()
    {
 
    }
}

それぞれのマイグレーションはup()down()メソッドで構成されます。up()の内部ではカラムを追加したりテーブルを作成することが可能で、down()ではup()で行ったことを取り消す処理を行うだけです。 それぞれのクラスはデータベースのバージョンを必ず表現しup()down()メソッドによってデータベースのバージョン間を往来できます。

excerptカラムを追加するために新しいマイグレーションクラス用のコードを書いてみましょう。

/**
 * This class has been auto-generated by the Doctrine ORM Framework
 */
class AddBlogPostExcerptColumn extends Doctrine_Migration
{
    public function up()
    {
      $this->addColumn('blog_post', 'excerpt', 'string', array('length' => '255'));
    }
 
    public function down()
    {
      $this->removeColumn('blog_post', 'excerpt');
    }
}

次のコマンドを実行するとデータベースは先ほど書いたマイグレーションクラスを使用する最新バージョンにアップグレードされexcerptカラムはデータベースに追加されます。

$ ./symfony doctrine:migrate frontend
>> doctrine  migrated successfully to version #6

マイグレーションに関する詳細な情報はDoctrineのマニュアルをご覧ください。

« 6章 - データを扱う

Questions & Feedback

If you find a typo or an error, please register and open a ticket.

If you need support or have a technical question, please post to the official user mailing-list.