GithubHelp home page GithubHelp logo

rails-test's Introduction

Test App

Rails test app to reproduce this issue

To setup to reproduce the bug, I have a database.yml file generated by rails new, which has a socket setting, but does not has host setting. Then when run the rake task, DATABASE_URL is set to mysql2://mysql.

This test can be run in two easy Docker containers started from a Docker compose file. After clone the app, run

docker-compose build rake

to build the Docker image. Then run

TASKS="db:create" docker-compose run rake

to run the db:create rake task. The first time you run, you may get output like this due to the mysql container is not started quick enough:

Creating network "railstest_default" with the default driver
Creating railstest_mysql_1
#<Mysql2::Error: Can't connect to MySQL server on 'mysql' (111 "Connection refused")>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>nil, "socket"=>"/tmp/mysql.sock", "database"=>"rails-test_development", "host"=>"mysql"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'rails-test_development'
#<Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2 "No such file or directory")>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>nil, "socket"=>"/tmp/mysql.sock", "database"=>"rails-test_test"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'rails-test_test'

But notice that the error message for the first run (development) and the second run (test) are different: For the development database, the error message is

#<Mysql2::Error: Can't connect to MySQL server on 'mysql' (111 "Connection refused")>

while for the test database, the error message is

#<Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2 "No such file or directory")>

The first run it attempts to use the host setting in DATABASE_URL as expected, but the second run it attempts to use the socket connection and bypassed DATABASE_URL.

If you run the same command again

TASKS="db:create" docker-compose run rake

after the database container is ready, you will get:

Created database 'rails-test_development'
#<Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2 "No such file or directory")>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>nil, "socket"=>"/tmp/mysql.sock", "database"=>"rails-test_test"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'rails-test_test'

The development database is created successfully, but the test database was not created although it says otherwise.

You'll get similar result if you run

TASKS="db:drop" docker-compose run rake

or any other database related tasks.

rails-test's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.