
テーブル作成後に、やっぱりカラム名を変更したい!とか
データ型や初期値を変更したい、nullを許可したい!などよくあると思う。
Mysql上で直接編集するのは基本的にはNG。
例えば本番環境にデプロイするときに、その操作が反映されず混乱を招くから。
今回はパッチファイルを作って対応する方法を各種説明。
思いつくたびにこの記事にどんどん追記していく予定。
ここで登場するコマンドは、全てプロジェクトディレクトリ上で叩くこと。(docker環境ならコンテナに入った状態)
ジャンプできる目次
パッチファイル作成コマンドとファイルの内容の書き方
テーブルを新規作成する場合
laravelのプロジェクトディレクトリにいる状態で
php artisan make:migration create_テーブル名_table
テーブル名は複数形の名称にすることが多い。
例えば
php artisan make:migration create_stocks_table
なら、stocksという名前のテーブルを新たに作成する
みたいな感じの名称のパッチファイルが作成される。
作成されたファイルを編集する。
<?php
//プロジェクトディレクトリ\database\migrations\2021_08_09_111150_create_stocks_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStocksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('stocks', function (Blueprint $table) {
//追記
$table->id();
$table->timestamps();
$table->string('name', '100');
$table->string('genre', '100');
$table->string('detail', '500')->nullable();
$table->integer('fee');
$table->string('path', '200')->nullable();
$table->integer('author_id')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('stocks');//追記
}
}
後半の
`public function down()
{
Schema::dropIfExists('stocks');
}`
は、万が一パッチファイル反映前の状態に戻したいってなった時に行う処理を示している。
今回は、stocksテーブルをなかったことにしたいので、
stocksテーブルを丸ごと削除する処理が書かれている。
後からカラムを追加したい場合
php artisan make:migration add_追加したいカラム名_to_追加したいテーブル名_table --table=テーブル名
add_追加したいカラム名_to_追加したいテーブル名_table
の部分は自分が分かりやすい名前ならなんでもいい。
例えば
php artisan make:migration add_status_to_stocks_table --table=stocks
なら、
stockテーブルに
statusカラムを
追加しようとしている。
下記のようなファイルが生成されるので、必要な情報を追記する。
//プロジェクトディレクトリ/database/migrations/2022_10_03_182928_add_status_to_stocks_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddStatusToStocksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('stocks', function (Blueprint $table) {
$table->string('status')->default('publish')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('stocks', function (Blueprint $table) {
$table->dropColumn('status');
});
}
}
この例はstocksテーブルにstatusカラムを追加するパッチファイル。
->default('publish')->nullable()
は、statusカラムのデフォルト値はpublishで、nullも許可するという意味になる。
ちなみにデフォルト値を指定すれば、既存のレコードの中身もちゃんと指定した値で埋まってくれる。
後からカラムを削除したい場合
php artisan make:migration drop_column_other_column --table=customers
php artisan make:migration drop_column_削除したいカラム名_column --table=テーブル名
カラム名_type_to_変更後の型_テーブル名
の部分は自分がわかればなんでもいい。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class DropColumnOtherColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('customers', function (Blueprint $table) {
//追記
$table->dropColumn('other01');
$table->dropColumn('other02');
$table->dropColumn('other03');
$table->dropColumn('other04');
$table->dropColumn('other05');
$table->dropColumn('other06');
$table->dropColumn('other07');
$table->dropColumn('other08');
$table->dropColumn('other09');
$table->dropColumn('other10');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('customers', function (Blueprint $table) {
//追記
$table->string('other01')->nullable();
$table->string('other02')->nullable();
$table->string('other03')->nullable();
$table->string('other04')->nullable();
$table->string('other05')->nullable();
$table->string('other06')->nullable();
$table->string('other07')->nullable();
$table->string('other08')->nullable();
$table->string('other09')->nullable();
$table->string('other10')->nullable();
});
}
}
後からカラムのデータ型を変更したい場合
php artisan make:migration カラム名_type_to_変更後の型_テーブル名 --table=テーブル名
カラム名_type_to_変更後の型_テーブル名
の部分は、自分が分かれば好きな名前でいい。
最後の--table=stocks
は、正確にテーブルの名前を入力する必要がある。
例えば、
php artisan make:migration tags_type_to_json_stocks --table=stocks
のような感じ。
この例では、
stocksテーブル
における
tagsカラム
の型を
json型
に変更しようとしている。
下記のようなファイルが生成されるので、必要な情報を追記する。
//プロジェクトディレクトリ\database\migrations\2022_09_29_163827_tags_type_to_json_stocks.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class TagsTypeToJsonStocks extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('stocks', function (Blueprint $table) {
$table->json('tags')->change();//追記
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('stocks', function (Blueprint $table) {
$table->string('tags','1024')->change();//追記
});
}
}
$table->json('tags')->change();
が、これから変更する内容。
あ、ミスった!元に戻したい!
ってなった場合に行いたい内容を
$table->string('tags','1024')->change();
のように
public function down(){ }
の中に書く
後からデフォルト値やnull可否を変更したい場合
php artisan make:migration change_変更したいカラム_内容_変更前_to_変更後 --table=テーブル名
みたいなコマンドを叩く。
「change_変更したいカラム_内容_変更前_to_変更後
」の部分は分かりやすい名称ならなんでもいい。自由。
例えば、
php artisan make:migration change_status_default_publish_to_inspecting --table=stocks
なら、
stocksテーブル
の
statusカラム
の
デフォルト値を
「puslish」(公開)から
「inspecting」(審査中)にする
という意味合いっぽくなる。
生成されたファイルを編集する
プロジェクトディレクトリ/database/migrations/2022_10_03_212906_change_status_default_publish_to_inspecting.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangeStatusDefaultPublishToInspecting extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('stocks', function (Blueprint $table) {
$table->string('status')->default('inspecting')->nullable()->change();//変更内容
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('stocks', function (Blueprint $table) {
$table->string('status')->default('publish')->nullable()->change();//変更前に戻したくなった時に走らせる処理
});
}
}
反映させるには
php artisan migrate
を叩くとデータベースにパッチファイルに書いた処理の内容が反映される。
ミスった!やっぱりさっきの状態に戻したいって時は?
なんか間違えた!元に戻したい!ってときは、
php artisan migrate:rollback --step=1
と叩くと元に戻せる。