[RUBY] [Rails] Introducing Travis CI and getting stuck in db: create

Introduction

I will write about what I stumbled upon when introducing the automated test tool Travis CI.

environment

・ Ruby 2.6.5 ・ Rails 6.0.3.2 -Mysql Ver 14.14 Distrib 5.6.47

problem

There was a problem that the db: create described in .travis.yml did not pass by all means as shown below.


script:
  - bundle exec rake db:create RAILS_ENV=test
  - bundle exec rake db:migrate RAILS_ENV=test
  - bundle exec rspec

I got an error message like this on the Travis CI screen.

DBcreate.png

Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

After a lot of research, I found that the socket file was missing. It means that the path to the socket file described in database.yml is incorrect. The DB used this time is MySQL.


test:
  <<: *default
  database: (myapp)_test
  adapter: mysql2
  encoding: utf8
  username: root
  password:
  socket: (Description here)

solution

But I couldn't figure out the file structure in the non-local Travis CI, so I decided to check it by myself.

First, the command to find the location of the socket file is:

$ mysql_config --socket

Since this can be understood in the local environment, I added this description because I thought that I could find the location of mysqld.sock on Travis CI by writing it inscript:of .travis.yml. (I don't know if it's the right way ...)


script:
  - mysql_config --socket (← Check the socket file)
  - bundle exec rake db:create RAILS_ENV=test
  - bundle exec rake db:migrate RAILS_ENV=test
  - bundle exec rspec

Then

MySQL.soke.png

What a value is back! This is the description on line 515!

/var/run/mysql/mysqld.sock

If you write this in database.yml, db: create passed successfully!

Proper use for local and Travis CI

On the contrary, if you do this, the local test will not work, so when testing with Travis CI, refer to the socket examined above.

Create database_travis.yml exclusively for TravisCI and write as follows.


test:
  <<: *default
  database: (myapp)_test
  adapter: mysql2
  encoding: utf8
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock

And add the following to .travis.yml


before_script:
  - "cp config/database_travis.yml config/database.yml"

If I rewrite it only at this time, the test worked both locally and with Travis CI. I had a hard time finding the cause of the database error.

buildpass.png

I was able to safely attach this badge to the Readme! !!

Recommended Posts

[Rails] Introducing Travis CI and getting stuck in db: create
Error in rails db: migrate
Introducing Bootstrap and Font-Awesome (Rails)
[Rails] How to execute "rails db: create" etc. in production environment EC2
[Rails] DB settings and operations (mysql), Rubocop in VSCode, useful extensions
Install Rails in the development environment and create a new application
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
Create a new app in Rails
Remove "assets" and "turbolinks" in "Rails6".
CRUD features and MVC in Rails
[Rails] Store only checked items in DB
Beginners create portfolio in Ruby on Rails
A memo to simply create a form using only HTML and CSS in Rails 6