2

I got something like this when i hit rake db:migrate Im using figaro in my app, its my first time with this gem any mysql so I cant get what excatly is wrong. Thanks in advance :)

Mysql2::Error: No database selected: CREATE TABLE schema_migrations (version varchar(255) PRIMARY KEY) ENGINE=InnoDB

database.yml

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: <%= ENV["DB_USER"] %>
  password: <%= ENV["DB_PASSWORD"] %>

development:
  <<: *default
  database: <%= ENV["DB_DATABASE_DEV"] %>


test:
  <<: *default
  database: <%= ENV["DB_DATABSE_TEST"] %>


production:
  <<: *default
  database: <%= ENV['DB_DATABASE_PRODUCTION'] %>

application.yml

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: password

development:
  <<: *default
  database: dev

test:
  <<: *default
  database: test_db
Daniel
  • 153
  • 2
  • 12

4 Answers4

2

I don't have enough point to ask in comment, but it seems you're trying to run migration without creating a schema first.

Run rails db:create to create the schema first. For example, if you don't have the a database named "dev" in your MySQL, this command will do so.

rails db:create

To set specific environment

rails db:create RAILS_ENV=test

Then you run canrails db:migrate.

To destroy a schema if you need one:

rails db:drop

You're Using ENV

Though it might not be related, have you set up your ENV properly before running the command?

For Windows, you can add those, for example, DB_USER to system environment

Just press Start and type system environment >> Environment Variables... >> New...

Variable Name: DB_USER
Variable Value: your_username

Note: You might have to restart your system for the environment to work.

Or if you don't have time, simply in your terminal you're running rails s:

C:\Sites\ProjectName> set DB_USER=your_username
C:\Sites\ProjectName> set DB_PASSWORD=your_username
C:\Sites\ProjectName> rails s

The same steps for Linux, but I don't know the specific command. Some prefer to use .env file.

Mohd.Zafranudin
  • 364
  • 4
  • 12
0

I've run into this trouble a couple days ago, and it's very annoying because Rails tried to create dev and test db's when in dev environment. Nothing worked.

I ended up hard-coding database: into database.yml, and let Figaro manage credentials.

0

I runned the same issue today.

Ensure you have dotenv-rails gem installed and that "DB_DATABASE_DEV" has a value in your .env file.

Also, make sure your database has been created.

0

It happens when your database.yml does not include database: some_database part or when database name is empty. In your case you can use one of few options:

Option 1: run migrations with database name env variable included

DB_DATABASE_DEV=your_dev_db_name rails db:<task>

Option 2: use hard-coded db name in your database.yml file

# database.yml
development:
  <<: *default
  database: my_development_database

Option 3: make sure that env variables are correctly loaded

To do so in linux, simply write in terminal:

echo ${DB_DATABASE_DEV}

On windows terminal you need to write this:

echo %DB_DATABASE_DEV%

If you get no content then this env variable is not set