イントロダクションIntroduction
マイグレーションはデータベースをバージョンコントロールする一手法です。これはデータベーススキーマの修正と現状のスキーマの状態を最新状態に保つことを両立させてくれます。典型的なマイグレーションはアプリケーションのスキーマを簡単に管理するためのスキーマ・ビルダーと一緒に使用されます。Migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state. Migrations are typically paired with the Schema Builder[/docs/master/schema] to easily manage your application's schema.
マイグレーションの生成Creating Migrations
make:migration Artisan CLIを使い、マイグレーションを生成できます。To create a migration, you may use the make:migration command on the Artisan CLI:
php artisan make:migration create_users_table
マイグレーションはdatabase/migrationsフォルダーに設置され、マイグレーションの実行順をフレームワークに知らせるため、名前にタイムスタンプが含まれています。The migration will be placed in your database/migrations folder, and will contain a timestamp which allows the framework to determine the order of the migrations.
--tableと--createオプションもテーブル名、マイグレーションで新しいテーブルを生成するかを指定するために使用できます。The --table and --create options may also be used to indicate the name of the table, and whether the migration will be creating a new table:
php artisan make:migration add_votes_to_user_table --table=users
php artisan make:migration create_users_table --create=users
マイグレーションの実行Running Migrations
未実行の全マイグレーションを実行するRunning All Outstanding Migrations
php artisan migrate
注目:"class not found"エラーがマイグレーション実行時に発生する場合は、
composer dump-autoloadコマンドを実行してみてください。Note: If you receive a "class not found" error when running migrations, try running thecomposer dump-autoloadcommand.
実働環境でマイグレーションを強制するForcing Migrations In Production
いくつかのマイグレーション操作は破壊的です。つまり、データーを失う可能性があります。実働環境のデータベースでこうしたコマンドを実行してしまうことを守るために、コマンドが実行される前に、確認のプロンプトが表示されます。このようにコマンド実行時のプロンプトを出さないためには、--forceフラグを指定してください。Some migration operations are destructive, meaning they may cause you to lose data. In order to protect you from running these commands against your production database, you will prompted for confirmation before these commands are executed. To force the commands to run without a prompt, use the --force flag:
php artisan migrate --force
マイグレーションのロールバックRolling Back Migrations
最後のマイグレーション操作をロールバックするRollback The Last Migration Operation
php artisan migrate:rollback
全マイグレーションをロールバックするRollback all migrations
php artisan migrate:reset
全マイグレーションをロールバックし、全部実行し直すRollback all migrations and run them all again
php artisan migrate:refresh
php artisan migrate:refresh --seed
データベース初期値設定Database Seeding
更に初期値設定(seed)クラスを使用し、テストデーターをデーターベースに設定する、シンプルな方法もLaravelには備わっています。全部の初期値設定クラスはdatabase/seedsに保存されます。初期値設定クラスには好きな名前を付けられます。ですがUserTableSeederなどのような分かりやすい規則に従ったほうが良いでしょう。デフォルトとして、DatabaseSeederクラスが定義されています。このクラスからcallメソッドを使い、他の初期値設定クラスを呼び出すことで、値を設定する順番をコントロールできます。Laravel also includes a simple way to seed your database with test data using seed classes. All seed classes are stored in database/seeds. Seed classes may have any name you wish, but probably should follow some sensible convention, such as UserTableSeeder, etc. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.
データベース初期値設定の例Example Database Seed Class
class DatabaseSeeder extends Seeder {
	public function run()
	{
		$this->call('UserTableSeeder');
		$this->command->info('User table seeded!');
	}
}
class UserTableSeeder extends Seeder {
	public function run()
	{
		DB::table('users')->delete();
		User::create(array('email' => 'foo@bar.com'));
	}
}
データベースに初期データーを設置するには、Artisanコマンドラインツールでdb:seedコマンドを使ってください。To seed your database, you may use the db:seed command on the Artisan CLI:
php artisan db:seed
デフォルトでは、db:seedコマンドは、他の初期値設定クラスを呼び出しているDatabaseSeederクラスを実行します。個別に初期値設定クラスを呼び出したい場合は、--classオプションを指定して実行することができます。By default, the db:seed command runs the DatabaseSeeder class, which may be used to call other seed classes. However, you may use the --class option to specify a specific seeder class to run individually:
php artisan db:seed --class=UserTableSeeder
またmigrate:refreshコマンドを使用し、マイグレーションを全部ロールバックし、再度実行することでも、初期データーを設置できます。You may also seed your database using the migrate:refresh command, which will also rollback and re-run all of your migrations:
php artisan migrate:refresh --seed