I will write about what I stumbled upon when introducing the automated test tool Travis CI
.
・ Ruby 2.6.5 ・ Rails 6.0.3.2 -Mysql Ver 14.14 Distrib 5.6.47
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.
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)
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
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!
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.
I was able to safely attach this badge to the Readme! !!
Recommended Posts