LoginSignup
10
8

laravel MySQL tinyInteger型のカラムのコメントを変更

Last updated at Posted at 2020-12-31

目的

  • laravel TinyInteger型のカラムのコメントをマイグレーションファイルで書き換える方法をまとめる

環境

  • ハードウェア環境
項目 情報
OS macOS Catalina(10.15.5)
ハードウェア MacBook Pro (13-inch, 2020, Four Thunderbolt 3 ports)
プロセッサ 2 GHz クアッドコアIntel Core i5
メモリ 32 GB 3733 MHz LPDDR4
グラフィックス Intel Iris Plus Graphics 1536 MB
  • ソフトウェア環境
項目 情報 備考
PHP バージョン 7.4.8 Homebrewを用いてこちらの方法で導入→Mac HomebrewでPHPをインストールする
Laravel バージョン 8.6.0 commposerを用いてこちらの方法で導入→Mac Laravelの環境構築を行う
MySQLバージョン 8.0.19 for osx10.13 on x86_64 Homwbrewを用いてこちらの方法で導入→Mac HomebrewでMySQLをインストールする

情報

  • 筆者はlaravel8にて本記事の内容の検証を実施した。未検証ながら他のバージョンでも本方法で実施する事が可能だと思う。

  • 下記のマイグレーションファイルを実行してusersテーブルのremember_tokenにTinyInt型の「flag」カラムを追加する。

    アプリ名ディレクトリ/database/migrations/2020_12_28_124905_change_users_table.php
    <?php
      
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class ChangeUsersTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('users', function (Blueprint $table) {
                $table->tinyInteger('flag')->after('remember_token')->comment('フラッグ用のカラム');
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::table('users', function (Blueprint $table) {
                $table->dropColmun('flag');
            });
        }
    }
    
  • 上記のマイグレーションファイルをマイグレートした後のusersテーブルのカラム情報を下記に記載する。(下記情報はSQL文show full columns from users;で確認した。)

    Field Type Collation Null Key Default Extra Privileges Comment
    id bigint unsigned NULL NO PRI NULL auto_increment select,insert,update,references
    name varchar(255) utf8mb4_unicode_ci NO NULL select,insert,update,references
    email varchar(255) utf8mb4_unicode_ci NO UNI NULL select,insert,update,references
    email_verified_at timestamp NULL YES NULL select,insert,update,references
    password varchar(255) utf8mb4_unicode_ci NO NULL select,insert,update,references
    remember_token varchar(100) utf8mb4_unicode_ci YES NULL select,insert,update,references
    flag tinyint NULL NO NULL select,insert,update,references フラッグ用のカラム
    created_at timestamp NULL YES NULL select,insert,update,references
    updated_at timestamp NULL YES NULL select,insert,update,references

概要

  1. マイグレーションファイルの作成と記載
  2. マイグレートと確認

詳細

  1. マイグレーションファイルの作成と記載

    1. アプリ名ディレクトリで下記コマンドを実行してマイグレーションファイルを作成する。

      $ php artisan make:migration change_flag_column_to_users_table --table=users
      
    2. 作成されたファイルを開き下記のように追記する。

      アプリ名ディレクトリ/database/migrations/2020_12_28_130526_change_flag_column_to_users_table.php
      <?php
        
      use Illuminate\Database\Migrations\Migration;
      use Illuminate\Database\Schema\Blueprint;
      use Illuminate\Support\Facades\Schema;
      // 下記を追記
      use Illuminate\Support\Facades\DB;
      
      class ChangeFlagColumnToUsersTable extends Migration
      {
          /**
           * Run the migrations.
           *
           * @return void
           */
          public function up()
          {
              Schema::table('users', function (Blueprint $table) {
                  //下記を追記、データ型やnullの制約などの情報も記載を忘れずにする
                  DB::statement('alter table users change column flag flag tinyint not null comment \'フラッグ用のカラム(1:->on 0->off)\'');
              });
          }
      
          /**
           * Reverse the migrations.
           *
           * @return void
           */
          public function down()
          {
              Schema::table('users', function (Blueprint $table) {
                  //下記を追記
                  DB::statement('alter table users change column flag flag tinyint not null comment \'フラッグ用のカラム\'');
              });
          }
      }
      
  2. マイグレートと確認

    1. アプリ名ディレクトリで下記コマンドを実行してマイグレートを実行する。

      $ php artisan migrate
      
    2. MySQLにターミナルからログインし下記SQLを実行する。

      show full columns from users;
      
    3. 下記のようにflagカラムのComment部分が変更された。

      Field Type Collation Null Key Default Extra Privileges Comment
      id bigint unsigned NULL NO PRI NULL auto_increment select,insert,update,references
      name varchar(255) utf8mb4_unicode_ci NO NULL select,insert,update,references
      email varchar(255) utf8mb4_unicode_ci NO UNI NULL select,insert,update,references
      email_verified_at timestamp NULL YES NULL select,insert,update,references
      password varchar(255) utf8mb4_unicode_ci NO NULL select,insert,update,references
      remember_token varchar(100) utf8mb4_unicode_ci YES NULL select,insert,update,references
      flag tinyint NULL NO NULL select,insert,update,references フラッグ用のカラム(1:->on 0->off)
      created_at timestamp NULL YES NULL select,insert,update,references
      updated_at timestamp NULL YES NULL select,insert,update,references

個人メモ

  • TinyInt型はchange()でカラム情報を修正する事ができない。そのため、DBファサードを用いてSQLを実行した。
  • マイグレーションファイルでDBファサードを使用できるようにしてstatementメソッドを用いてカラムコメントを設定するSQLを実行する。
  • コメントを設定するSQLのシングルクオート'はエスケープして文字列として扱われるようにする。
10
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8