LoginSignup
0
2

More than 3 years have passed since last update.

Heroku(Laravel)で自作アプリをHerokuにデプロイしようとしたらマイグレーションエラーが起きたので、ClearDB(MySQL5.5)からJawsDB(MySQL8.0)に変更した時の備忘録

Posted at

開発環境

PHP7.3
Laravel5.8
MySQL8.0
Heroku使用時のDBアドオン:ClearDB MySQL5.5

やろうとしたこと

下記をheroku run php artisan migrate

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Facades\Schema;

class CreateChatRoomsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('chat_rooms', function (Blueprint $table) {
            $table->id();
            $table->text('user_info_json')->default(new Expression('(JSON_ARRAY())'));
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('chat_rooms');
    }
}

エラーが出現。

エラーの内容

  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(JSON_ARRAY()),   
  `created_at` timestamp null, `updated_at` timestamp null) defaul' at line 1 (SQL: create table `chat_rooms` (`id` bigint unsigned not null auto_increment primary key, `user_info_json` text not null default (  
  JSON_ARRAY()), `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')                                                                                                                                                                                                                                                                                             
  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(JSON_ARRAY()),   
  `created_at` timestamp null, `updated_at` timestamp null) defaul' at line 1

推測

$table->text('user_info_json')->default(new Expression('(JSON_ARRAY())'));

の部分がSQLのバージョンに対応していないらしい。

ローカル(MySQL8.0)ではエラーは起こらなかったため、本番環境のSQLをバージョンアップすれば通るかも?

ClearDBからJawsDBにアドオンを変更しよう

ClearDB・・・MySQL5.5、5.6、5.7対応
JawsDB・・・5.7及び8.0対応
とのことであったため、ローカル環境(MySQL8.0)と合わせるべくJawsDBにアドオンを変更することに。

JawsDBアドオン取得

heroku addons:create jawsdb:[プラン名] -a [アプリ名] --version=8.0

--version=8.0のようにすることで、バージョンを8.0指定でインストール。

ClearDBの環境変数を削除

JawsDBをインストールしただけの状態なので、まだClearDB使用時の環境変数が登録されている。

DB_DATABASE:           [ClearDBのDATABASE]
DB_HOST:               [ClearDBのHOST]
DB_PASSWORD:           [ClearDBのPASSWORD]
DB_USERNAME:           [ClearDBのUSERNAME]

これらを全て一旦削除

heroku config:unset DB_DATABASE
heroku config:unset DB_HOST
heroku config:unset DB_PASSWORD
heroku config:unset DB_USERNAME

その後

heroku config

すると、ClearDBの環境変数が消えている。

JawsDBの接続情報を確認

heroku config |grep JAWSDB_URL
JAWSDB_URL:       mysql://[DB_USERNAME]:[DB_PASSWORD]@[DB_HOST]/[DB_DATABASE]

もしくはHerokuのマイページからJawsDBアドオンに遷移して確認。

JawsDBの環境変数登録

heroku config:set DB_DATABASE=[DB_DATABASE]
heroku config:set DB_HOST=[DB_HOST]
heroku config:set DB_PASSWORD=[DB_PASSWORD]
heroku config:set DB_USERNAME=[DB_USERNAME]

再度マイグレーション

heroku run php artisan migrate

通った!

0
2
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
0
2