What?
Why?
- そもそもなぜDBマイグレーションが必要なのか?
- MySQL Workbenchとかを使う
- 問題:
- .mwbファイルとかSQLファイルとかの管理対象が増える
- DBのバージョンとソースコードのバージョンがずれたりして死ぬことがある(死んだことがある)
Migration
$ php artisan migrate
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
$ mysql --user=homestead --password=secret homestead
mysql> show tables;
+---------------------+
| Tables_in_homestead |
+---------------------+
| migrations |
| password_resets |
| users |
+---------------------+
3 rows in set (0.00 sec)
mysql> desc migrations;
+-----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| migration | varchar(255) | NO | | NULL | |
| batch | int(11) | NO | | NULL | |
+-----------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> desc users;
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | UNI | NULL | |
| password | varchar(255) | NO | | NULL | |
| remember_token | varchar(100) | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+----------------+------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
mysql> desc password_resets;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| email | varchar(255) | NO | MUL | NULL | |
| token | varchar(255) | NO | MUL | NULL | |
| created_at | timestamp | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
Rollback
$ php artisan migrate:status
+------+------------------------------------------------+
| Ran? | Migration |
+------+------------------------------------------------+
| Y | 2014_10_12_000000_create_users_table |
| Y | 2014_10_12_100000_create_password_resets_table |
+------+------------------------------------------------+
$ php artisan migrate:rollback
Rolled back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_000000_create_users_table
$ php artisan migrate:status
+------+------------------------------------------------+
| Ran? | Migration |
+------+------------------------------------------------+
| N | 2014_10_12_000000_create_users_table |
| N | 2014_10_12_100000_create_password_resets_table |
+------+------------------------------------------------+
Make migration
$ php artisan make:migration create_articles_table --create="articles"
Created Migration: 2017_02_14_184315_create_articles_table
$ less database/migrations/2017_02_14_184315_create_articles_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateArticlesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
// ここから
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamps();
$table->timestamp('published_at')->useCurrent();
// ここまで追加
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
Edit migration
php artisan migrate
mysql> desc articles;
+--------------+------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+-------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | | NULL | |
| body | text | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| published_at | timestamp | NO | | CURRENT_TIMESTAMP | |
| exceprt | text | NO | | NULL | |
+--------------+------------------+------+-----+-------------------+----------------+
7 rows in set (0.00 sec)
参考
http://site.oganity.pw/232/