rake db:test:prepare or :clone dont create test database - ruby-on-rails

As topic says, if I do a rake db:test:prepare or rake db:test:clone no test.sqlite3 gets created. Also did a db:migrate before
Terminal dont give any output.
Thats my database.yml
sqlite: &sqlite
adapter: sqlite3
database: db/<%= Rails.env %>.sqlite3
mysql: &mysql
adapter: mysql2
username: root
password:
database: myapp_<%= Rails.env %>
postgresql: &postgresql
adapter: postgresql
username: postgres
password:
database: myapp_<%= Rails.env %>
min_messages: ERROR
defaults: &defaults
pool: 5
timeout: 5000
host: localhost
<<: *<%= ENV['DB'] || "sqlite" %>
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
Any Ideas or suggestions?

Ah me is stupid...
had to give the rails.env, due to my database.yml with the command
RAILS_ENV=test rake db:test:prepare

Related

Unable to create Database in Rails using rake db:create

I created a new rails project and did few configuration changes in database.yml.
Then had to create the database using the command rake db:create inorder to continue developing the application but I get the error below.
Els-MacBook-Pro:eshop el$ rake db:create
warning ../../package.json: No license field
FATAL: role "eshop" does not exist
Couldn't create 'eshop_development' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: FATAL: role "eshop" does not exist
Tasks: TOP => db:create
(See full trace by running task with --trace)
Els-MacBook-Pro:eshop el$
I will also post the contents of my database.yml below:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
host: localhost
database: eshop_development
username: eshop
password: eshop
test:
<<: *default
host: localhost
database: eshop_test
username: eshop
password: eshop
production:
<<: *default
database: eshop_production
username: eshop
password: <%= ENV['ESHOP_DATABASE_PASSWORD'] %>
I will be grateful if someone can help me figure out why I cannot create a db. I have read all types of solutions and none seems to be working. Thanks.
In your database.yaml you need to add a port: 5432, and then Try rails db:create, as rake has been deprecated.
create a role in your postrgesql database named 'eshop'

Rails 6 on Heroku: ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter

Our Rails app has just been upgraded to Rails 6.0 but the deployment to Heroku fails with:
Preparing app for Rails asset pipeline
Running: rake assets:precompile
DEPRECATION WARNING: Including LoggerSilence is deprecated and will be removed in Rails 6.1. Please use `ActiveSupport::LoggerSilence` instead (called from <top (required)> at /tmp/build_b9759d496c72c1085bb8441e3c2159fb/config/application.rb:7)
rake aborted!
ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter
/tmp/build_b9759d496c72c1085bb8441e3c2159fb/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/connection_specification.rb:163:in `spec'
config/database.yml:
default: &default
adapter: postgresql
encoding: utf8
pool: 5
username: <%= ENV['POSTGRESQL_USER'] %>
password:
socket: /tmp/mysql.sock
development:
<<: *default
database: OurApplication_development
test:
<<: *default
database: OurApplication_test
production:
url: <%= ENV['DATABASE_URL'] %>
Using version:
rails (6.0.0)
Ruby (2.6.5)
Does anybody have an idea how to solve this issue?
The problem has been solved by adding the 'adapter' to 'production' in the database.yml. This wasn't necessary when our project was on Rails 4 and 5.
production:
adapter: postgresql
A better solution altogether would just be to use ENV["DATABASE_URL"] to specify the connection details and keep your defaults segment to the actual minimal defaults:
default: &default
adapter: postgresql
encoding: utf8
pool: 5
development:
<<: *default
database: OurApplication_development
test:
<<: *default
database: OurApplication_test
production:
# doing url: is just stupid as thats what rails does anyways
<<: *default
This avoids potential developer wars. You can use DotEnv to load different ENV vars for each environment. Alternatively you can add another hash to to your database.yml and merge it:
default: &default
adapter: postgresql
encoding: utf8
pool: 5
local_settings: &local_settings
# will raise an exception on nil instead of failing silently
username: <%= ENV.fetch('POSTGRESQL_USER') %>
password:
socket: /tmp/mysql.sock
development:
<<: *default
<<: *local_settings
database: OurApplication_development
test:
<<: *default
<<: *local_settings
database: OurApplication_test
production:
# doing url: is just stupid as thats what rails does anyways
<<: *default

rake db:create password authentication fails for user

While running the command rake db:create, the system throws postgresql authentication fails
FATAL: password authentication failed for user "postgres"
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "username"=>"postgres", "password"=>"password", "timeout"=>5000, "host"=>"localhost", "database"=>"rails-sample-guestbook-master_development"}
rake aborted!
database.yml file
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: supranimbus
password: 123456789
timeout: 5000
host: localhost
development:
<<: *default
database: rails-sample-guestbook-master_development
test:
<<: *default
database: rails-sample-guestbook-master_test
production:
<<: *default
database: rails-sample-guestbook-master_production
I guess the problem is setup password on postgresql if you don't remember the default password then go to pgAdmin click to open dashboard and then the left sidebar see the below image
Click the Properties... then will open a modal like see the below image
and click the Definition tab and see the Password field and set the new password, remember this password for postgres username.
That's is for password setup!
Now update your database.yml file like below
default: &default
adapter: postgresql
encoding: unicode
username: postgres
password: 123456 #=> which you set for postgres
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: project_development
Hope to help

How to prevent creation of development.sqlite3 file?

I changed my database.yml file to use postgresql instead of sqlite3.
But when I do rake db:migrate - development.sqlite3 file gets created.
Here's my database.yml file:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
host: localhost
development:
<<: *default
database: scrumban_development
test:
<<: *default
database: scrumban_test
production:
<<: *default
database: scrumban_production
username: scrumban
password: <%= ENV['scrumban_DATABASE_PASSWORD'] %>
Don't include the sqlite3 gem and it should stop creating it.

Can't rake db:migrate

When I'm trying to migrate my database I get an error Mysql2::Error: Table 'database.lorem_ipsum' doesn't exist: SHOW FULL FIELDS FROM ``lorem_ipsum. I've created database before by rake db:migrate
config/database.yml
development:
adapter: mysql2
database: database
username: root
password:
Can you try this ?
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: database
pool: 5
username: root
password:
On your database.yml

Resources