Larvelのマイグレーションをとりあえず試してみる(続き)
Larvelのマイグレーションをとりあえず試してみる https://yk5656.hatenablog.com/entry/20201213/1607785200
の続き
マイグレーションの実行
現状、migrationsテーブルは、下記のようになっている。
$ mysql -u root laravel -e 'SELECT * FROM migrations;' +----+------------------------------------------------+-------+ | id | migration | batch | +----+------------------------------------------------+-------+ | 1 | 2014_10_12_000000_create_users_table | 1 | | 2 | 2014_10_12_100000_create_password_resets_table | 1 | | 3 | 2019_08_19_000000_create_failed_jobs_table | 1 | +----+------------------------------------------------+-------+
マイグレーションのファイルを作成する。
$ php artisan make:migration hoge_hoge_1 $ ls database/migrations/ ・・・ 2021_02_04_075325_hoge_hoge_1.php
空のマイグレーションファイルができた。
$ cat database/migrations/2021_02_04_075325_hoge_hoge_1.php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class HogeHoge1 extends Migration { /** * Run the migrations. * * @return void */ public function up() { // } /** * Reverse the migrations. * * @return void */ public function down() { // } }
この何もしないマイグレーションファイルをマイグレートしてみる。
$ php artisan migrate
migrationsテーブルに実行結果が追加された。
$ mysql -u root laravel -e 'SELECT * FROM migrations;' +----+------------------------------------------------+-------+ | id | migration | batch | +----+------------------------------------------------+-------+ | 1 | 2014_10_12_000000_create_users_table | 1 | | 2 | 2014_10_12_100000_create_password_resets_table | 1 | | 3 | 2019_08_19_000000_create_failed_jobs_table | 1 | | 4 | 2021_02_04_075325_hoge_hoge_1 | 2 | +----+------------------------------------------------+-------+
テーブルの追加
テーブルを作成するためのマイグレーションファイルを作る場合は、「--create=テーブル名」を付ける。
$ php artisan make:migration hoge_hoge_2 --create=hoge
$ cat database/migrations/2021_02_04_075708_hoge_hoge_2.php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class HogeHoge2 extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('hoge', function (Blueprint $table) { $table->id(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('hoge'); } }
下記のように書き換えて、
$ vi database/migrations/2021_02_04_075708_hoge_hoge_2.php
・・・ Schema::create('hoge', function (Blueprint $table) { $table->id(); $table->integer('price'); $table->string('name', 100)->unique(); $table->text('description')->default('hogehoge'); $table->dateTime('hoge_at')->nullable()->comment('hogehoge'); $table->timestamps(); }); ・・・
マイグレーションを実行
$ php artisan migrate
下記のテーブルが作られた。
$ mysql -u root laravel -e 'SHOW CREATE TABLE hoge' ・・・ | hoge | CREATE TABLE `hoge` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `price` int(11) NOT NULL, `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, `description` text COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'hogehoge', `hoge_at` datetime DEFAULT NULL COMMENT 'hogehoge', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `hoge_name_unique` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci | ・・・
migrationsテーブルにも追加されている。
$ mysql -u root laravel -e 'SELECT * FROM migrations' +----+------------------------------------------------+-------+ | id | migration | batch | +----+------------------------------------------------+-------+ ・・・ | 5 | 2021_02_04_075708_hoge_hoge_2 | 3 | +----+------------------------------------------------+-------+
テーブルの更新
テーブルを更新するためのマイグレーションファイルを作る場合は、「--table=テーブル名」を付ける。
$ php artisan make:migration hoge_hoge_3 --table=hoge
$ cat database/migrations/2021_02_04_082900_hoge_hoge_3.php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class HogeHoge3 extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('hoge', function (Blueprint $table) { // }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('hoge', function (Blueprint $table) { // }); } }
カラムを色々変更・追加してみる。
$ vi database/migrations/2021_02_04_082900_hoge_hoge_3.php
・・・ public function up() { Schema::table('hoge', function (Blueprint $table) { $table->integer('price')->nullable()->change(); $table->string('name', 200)->change(); $table->renameColumn('description', 'description2'); $table->string('hoge_column'); }); } ・・・ public function down() { Schema::table('hoge', function (Blueprint $table) { $table->integer('price')->change(); $table->string('name', 100)->change(); $table->renameColumn('description2', 'description'); $table->dropColumn('hoge_column'); }); } ・・・
マイグレーションを実行したら、なんかエラーがでた。
$ php artisan migrate ・・・ Changing columns for table "hoge" requires Doctrine DBAL. Please install the doctrine/dbal package. ・・・
とりあえずインストールし、
$ composer require doctrine/dbal
再実行。
$ php artisan migrate
テーブルが更新された。
$ mysql -u root laravel -e 'SHOW CREATE TABLE hoge' ・・・ | hoge | CREATE TABLE `hoge` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `price` int(11) DEFAULT NULL, `name` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL, `description2` text COLLATE utf8mb4_unicode_ci NOT NULL, `hoge_at` datetime DEFAULT NULL COMMENT 'hogehoge', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, `hoge_column` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `hoge_name_unique` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci | ・・・
migrationsテーブルにも追加されている。
$ mysql -u root laravel -e 'SELECT * FROM migrations' +----+------------------------------------------------+-------+ | id | migration | batch | +----+------------------------------------------------+-------+ ・・・ | 6 | 2021_02_04_082900_hoge_hoge_3 | 4 | +----+------------------------------------------------+-------+
ロールバック
最後のマイグレーションを取り消してみる。
$ php artisan migrate:rollback
テーブルが元に戻った。
(よく見ると、descriptionのdefault値が消えている...)
$ mysql -u root laravel -e 'SHOW CREATE TABLE hoge' ・・・ | hoge | CREATE TABLE `hoge` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `price` int(11) DEFAULT NULL, `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, `description` text COLLATE utf8mb4_unicode_ci NOT NULL, `hoge_at` datetime DEFAULT NULL COMMENT 'hogehoge', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `hoge_name_unique` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci | ・・・
migrationsテーブルからも消えている。
$ mysql -u root laravel -e 'SELECT * FROM migrations' +----+------------------------------------------------+-------+ | id | migration | batch | +----+------------------------------------------------+-------+ | 1 | 2014_10_12_000000_create_users_table | 1 | | 2 | 2014_10_12_100000_create_password_resets_table | 1 | | 3 | 2019_08_19_000000_create_failed_jobs_table | 1 | | 4 | 2021_02_04_075325_hoge_hoge_1 | 2 | | 5 | 2021_02_04_075708_hoge_hoge_2 | 3 | +----+------------------------------------------------+-------+